question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Openness for Caller Argument Expression in Assert failure messages

See original GitHub issue

Description

With .Net 6 Caller Argument Expression was added, this could be used in testfx to provide the expression which failed the check. Doing this could provider consumers more out of the box detail about what caused the assert to fail without having to manually provide messages.

Sample Code change

#if !NET6_0_OR_GREATER
        public static void IsNull(object value)
        {
            IsNull(value, string.Empty, null);
        }
#endif

        public static void IsNull(
            object value,
#if !NET6_0_OR_GREATER
            string message)
#else
            [CallerArgumentExpression("value")]string message = "")
#endif
        {
            IsNull(value, message, null);
        }

Work required

  • Add .Net 6 as a built/packaged runtime
  • Modify Assert code to make the flows without a message removed in .Net 6 so that code like Assert.IsNull(myParam) execute passing myParam as the message.

Change in behavior

Currently a failing call to Assert.IsNull without a message will just return Assert.IsNull failed. This would change to Assert.IsNull failed. myParam which could be considered a breaking change and a result a no-go.

AB#1588958

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
CameronBellNZXcommented, Nov 29, 2022

Ahh I see, thanks for the clarification

1reaction
Evangelinkcommented, Nov 29, 2022

If your method has some argument marked with the CallerArgumentExpression attribute there is no problem for the method to be tested. The goal of this ticket is to rewrite MSTest assertions methods to be using this attribute so that if the user provides no custom message we can provide a better default message (being the caller expression).

Read more comments on GitHub >

github_iconTop Results From Across the Web

Caller argument expression - C# 10.0 draft feature ...
This feature specification describes attributes that can be used to enable the compiler to convert a caller argument expression into text.
Read more >
Caller Argument Expressions – A Look at New Language ...
In this post we will discuss all relevant aspects of Caller Argument Expressions and where they're helping. Have fun and be amazed!
Read more >
CallerArgumentExpression Attribute In C# 10
Let's write a simple Assert method using the CallerArgumentExpression attribute and use it in the Exists method. void Assert(bool condition, [ ...
Read more >
C# Make Assert Print Expression Being Asserted
I am just transitioning to C# but in C++ this could be done using a macro like this: #define ASSERT(Expr) Debug.Assert(Expr, "Assertion "...
Read more >
How C# 10.0 and .NET 6.0 improve ArgumentExceptions
Assert method being used here just takes a bool , so at runtime all it can do is tell you that something failed;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found