Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQPad: Assert() prints "Fail:" and continues instead of breaking

Tags:

c#

linqpad

In LINQPad, Debug.Assert() and Trace.Assert() from the System.Diagnostics namespace don't work as expected.

Instead of breaking - i.e. popping up a message box or whatever else happens to be configured for <trace> - they simply print "Fail:" or "Fehler:" to the output window and let the program continue on its merry way. In other words, instead of a noisy, unignorable explosion there are only near invisible micro-farts that intersperse "Fail:" into the textual output.

Is there any way of getting Assert() to revert to the default behaviour under LINQPad?

Alternatively, is there a simple way of mocking a Trace.Assert() so that failure results in a noisy explosion (i.e. exception, message box, whatever) and the source of the failure is pinpointed in the form of a line number or similar?

My unit tests are based on Trace.Assert(), which makes them totally useless if the function cannot break the program at the point of failure.

In Visual Studio everything works normally but not in LINQPad. I haven't fiddled with any system-wide settings but I did install Mono at some point in time. I went through all settings in LINQPad with a fine comb but to no avail. The LINQPad version is 4.57.02 (non-free), running on Windows 7 Pro (German) and Windows 8.1 Pro (English).

I googled high and wide but the only thing I could find is the topic Trace.Assert not breaking, neither showing the message box here on Stack Overflow. Its title certainly looks promising but the answers therein don't.

P.S.: I tried mocking by adding

class Trace
{
    public static void Assert (bool condition)
    {
        if (!condition)
            throw new Exception("FAIL!");
    }
}

to the LINQ script and setting a breakpoint on the throw statement. Clicking on the relevant call stack entry in LINQPad's debugger takes me to the relevant source line, so this sort of works. However, I couldn't find an equivalent to FoxPro's SET DEBUG ON for invoking the debugger directly from the source code instead of having to set a breakpoint on the throw...

like image 935
DarthGizka Avatar asked Oct 17 '25 17:10

DarthGizka


1 Answers

To answer the second half of your question, i.e., how do you get the LINQPad debugger to break when an exception is thrown, the answer is to click either of the 'Bug' icons on the toolbar.

Click Break when exception is unhandled (red bug) to make your query break whenever an unhandled exception is thrown.

Click Break when exception is thrown (blue bug) to make your query break whenever an exception is thrown, whether or not it is handled.

You can get LINQPad to make either setting the default by right-clicking and choosing Set as default. (Doing so forces the debugger to always attach to your queries, which incurs a small performance cost.)

As to why Debug.Assert(false) doesn't display a dialog, this is because this behavior hasn't been implemented in LINQPad. You could implement this easily as an extension, by adding the following code to My Extensions:

public class NoisyTracer : TextWriterTraceListener
{
    public static void Install()
    {
        Trace.Listeners.Clear();
        Trace.Listeners.Add (new NoisyTracer (Console.Out));
    }

    public NoisyTracer (TextWriter writer) : base (writer) { }

    public override void Fail (string message, string detailMessage)
    {
        base.Fail (message, detailMessage);
        throw new Exception ("Trace failure: " + message);
    }
}

Then, to enable, write your query as follows:

NoisyTracer.Install();
Debug.Assert (false);
like image 85
Joe Albahari Avatar answered Oct 20 '25 07:10

Joe Albahari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!