Skip to content

Custom fansi formatting for error messages #190

@bwbecker

Description

@bwbecker

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions