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 Differ Support #2221

Merged

Conversation

cheeseng
Copy link
Contributor

Added overridable differ to Prettifier trait, and added functions to create prettifier instance with supplied Differ.

…create prettifier instance with supplied Differ.
@cla-bot cla-bot bot added the cla-signed label Jan 11, 2023
@bvenners bvenners merged commit 4c06b89 into scalatest:3.2.x-new Jan 30, 2023
@charpov
Copy link

charpov commented May 17, 2023

How does one use a custom Differ? It looks like the Differ type is not public...

@cheeseng
Copy link
Contributor Author

@charpov I think you are right, @charpov at the moment perhaps the only way is to have your custom Differ use org.scalactic package.

@bvenners Shall we make Differ public in next 3.2 release?

@charpov
Copy link

charpov commented May 18, 2023

Actually, this works:

   val prettifier = Prettifier(Prettifier.default, (l,r,p) => PrettyPair("","",None))

It may not be the most convenient (and Differ might as well be a type alias for a function in that case)

@charpov
Copy link

charpov commented Apr 12, 2024

I don't know if this is the right place for this discussion (given the "merged" nature of the issue), but I still think the design could be improved.

I can now use a truncated prettifier do deal with large collections:

given defaultPrettifier: Prettifier = Prettifier.truncateAt(SizeLimit(32))

Then, in a test suite that uses large lists and large random trees, I further customize the prettifier:

given Prettifier = o =>
   o.asMatchable match
      case _: Tree[?] | _: Forest[?] => o.toString.short(512)
      case _                         => defaultPrettifier(o)

Now, I noticed that while assertions of the form assertResult(l2)(l1) don't, assert(l1 == l2) generates an "analysis". On large lists, this analysis still produces undesirable strings. It is trying to limit things, but uses a test:

case tp: TruncatingPrettifier => ...

and my prettifier is not a TruncatingPrettifier anymore.

It seems I can turn off this analysis with:

given Prettifier with
   def apply(o: Any): String = o.asMatchable match
      case _: Tree[?] | _: Forest[?] => o.toString.short(512)
      case _                         => defaultPrettifier(o)

   override def apply(left: Any, right: Any): PrettyPair =
      PrettyPair(apply(left), apply(right), None)

but that's a bit radical.

So, my questions:

  • Is there a way to turn "analysis" on and off? The fact that assert has it but assertResult doesn't is confusing.
  • Would it make sense for the Prettifier type to implement a limit (defaulting to None) and use it in Differ instead of checking that the type is TruncatingPrettifier? I could then specify a limit in my custom prettifier and rely on the existing behavior of the Differ.difference methods.

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

Successfully merging this pull request may close these issues.

None yet

3 participants