Skip to content

Commit 57b23da

Browse files
bvennerscheeseng
authored andcommitted
Added an apply method to Prettifier that takes two Any arguments and returns
a PrettyPair, which I also just added. PrettyPair has two prettifier objects, which were found to be unequal, so the idea is that this new apply method on Prettifier will use the differences to help decide how to prettify them. For example in strings we could put the []'s in the right spots. Or for long strings we can shorten them and highlight just the part (or at least the first part) that is different. And there's also a hint string in PrettyPair, that can be used to give info about the difference.
1 parent d461f18 commit 57b23da

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

scalactic-test/src/test/scala/org/scalactic/PrettifierSpec.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ class PrettifierSpec extends FunSpec with Matchers {
5454
myLittlePretty(()) should be ("<(), the Unit value>")
5555
myLittlePretty(List("1", "2", "3")) should be ("List(\"1\", \"2\", \"3\")")
5656
}
57+
it("should by default offer an apply method that takes two args that returns a PrettyPair whose left and right are prettified by the one-arg apply and hint is None.") {
58+
59+
case class Yell(secret: String)
60+
val myLittlePretty =
61+
new Prettifier {
62+
def apply(o: Any) =
63+
o match {
64+
case Yell(secret) => secret.toUpperCase + "!!!"
65+
case _ => Prettifier.default(o)
66+
}
67+
}
68+
myLittlePretty(Yell("I like fruit loops"), Yell("I like raisin bran")) shouldBe PrettyPair("I LIKE FRUIT LOOPS!!!", "I LIKE RAISIN BRAN!!!", None)
69+
}
5770
}
5871

5972
describe("the basic Prettifier") {

scalactic/src/main/scala/org/scalactic/Prettifier.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,13 @@ trait Prettifier extends Serializable { // I removed the extends (Any => String)
126126
* Prettifies the passed object.
127127
*/
128128
def apply(o: Any): String
129+
130+
def apply(left: Any, right: Any): PrettyPair =
131+
PrettyPair(
132+
apply(left),
133+
apply(right),
134+
None
135+
)
129136
}
130137

131138
/**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2001-2016 Artima, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.scalactic
17+
18+
case class PrettyPair(
19+
left: String,
20+
right: String,
21+
hint: Option[String]
22+
)
23+
24+
25+

0 commit comments

Comments
 (0)