Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom fansi formatting for error messages #190

Open
bwbecker opened this issue Jan 18, 2019 · 0 comments
Open

Custom fansi formatting for error messages #190

bwbecker opened this issue Jan 18, 2019 · 0 comments

Comments

@bwbecker
Copy link

I'd like to write a custom assertion that compares two strings for equality, highlighting their differences. For example, writing

      val s = "My relatively long string with a difference in the middle"
      "verify" - {
        verify(s).equals("My relatively long string With a difference in the middle")
      }

to produce '...long string [w]ith a differ...' != '...long string [W]ith a differ...' is not hard with uTest out of the box.

However, I'd really like the [w] and [W] to be set in red using the fansi library. I can do that now by passing a fansi-formatted string to the exception AND setting exceptionMsgColor = Attrs.Empty. However, that means that all the rest of the error messages are uncoloured.

I propose adding a marker trait, FansiErrorMarker to the utest. In formatException, test for that trait and, if present, print the message unchanged. Otherwise apply the usual exception message colour:

            current.getMessage match {
              case null => exCls
              case nonNull =>
                current match {
                  case e: FansiErrorMarker ⇒ ufansi.Str.join(exCls, ": ", nonNull)
                  case e ⇒ ufansi.Str.join(exCls, ": ", exceptionMsgColor(nonNull))
                }
            },

Then users can throw new AssertionError(fansiMsg) with FansiErrorMarker with their fansi-formatted message in a backwards compatible way.

Would the maintainers welcome a pull request with these changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant