Skip to content

Commit cb5c036

Browse files
committed
Merge branch 'pretty-differ-3' of https://github.com/cheeseng/scalatest into cheeseng-pretty-differ-3
2 parents 39f5524 + 24d439f commit cb5c036

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1656
-109
lines changed

project/GenScalacticJS.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ object GenScalacticJS {
8282
def genScala(targetDir: File, version: String, scalaVersion: String): Seq[File] =
8383
copyDir("scalactic/src/main/scala/org/scalactic", "org/scalactic", targetDir, List.empty) ++
8484
copyDir("scalactic/src/main/scala/org/scalactic/exceptions", "org/scalactic/exceptions", targetDir, List.empty) ++
85-
copyDir("scalactic/src/main/scala/org/scalactic/source", "org/scalactic/source", targetDir, List.empty) ++
86-
copyDir("scalactic/src/main/scala/org/scalactic/anyvals", "org/scalactic/anyvals", targetDir, List.empty) ++
85+
copyDir("scalactic/src/main/scala/org/scalactic/source", "org/scalactic/source", targetDir, List("ObjectMeta.scala")) ++
86+
copyDir("scalactic/src/main/scala/org/scalactic/anyvals", "org/scalactic/anyvals", targetDir, List.empty) ++
8787
GenVersions.genScalacticVersions(new File(targetDir, "org/scalactic"), version, scalaVersion)
8888

8989
def genMacroScala(targetDir: File, version: String, scalaVersion: String): Seq[File] =

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

+604
Large diffs are not rendered by default.

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

+14
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ 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+
val left = Yell("I like fruit loops")
69+
myLittlePretty(left, Yell("I like raisin bran")) shouldBe PrettyPair("I LIKE FRUIT LOOPS!!!", "I LIKE RAISIN BRAN!!!", Some(Differ.simpleClassName(left) + "(secret: \"I like [fruit loops]\" -> \"I like [raisin bran]\")"))
70+
}
5771
}
5872

5973
describe("the basic Prettifier") {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.source
17+
18+
import org.scalatest._
19+
20+
class ObjectMetaSpec extends FunSpec with Matchers {
21+
22+
case class Person(name: String, age: Int) {
23+
val otherField = "test other field"
24+
}
25+
26+
describe("ObjectMeta") {
27+
28+
it("should extract case class attribute names correctly") {
29+
ObjectMeta(Person("test", 33)).fieldNames should contain theSameElementsAs Set("name", "age", "otherField")
30+
}
31+
32+
it("should extract dynamically field value correctly") {
33+
val meta = ObjectMeta(Person("test", 33))
34+
meta.value("name") shouldBe "test"
35+
meta.value("age") shouldBe 33
36+
meta.value("otherField") shouldBe "test other field"
37+
}
38+
39+
it("should throw IllegalArgumentException when invalid attribute name is used to retrieve value") {
40+
val meta = ObjectMeta(Person("test", 33))
41+
val e = intercept[IllegalArgumentException] {
42+
meta.value("invalid")
43+
}
44+
e.getMessage shouldBe "'invalid' is not attribute for this instance."
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.source
17+
18+
trait ObjectMeta {
19+
20+
def fieldNames: scala.collection.immutable.IndexedSeq[String]
21+
22+
def hasField(name: String): Boolean = fieldNames.contains(name)
23+
24+
def value(name: String): Any
25+
26+
def typeName(name: String): String
27+
28+
def shortTypeName(name: String): String
29+
30+
}
31+
32+
object ObjectMeta {
33+
34+
def apply(v: Any): ObjectMeta = {
35+
36+
def filterDollarNumberAtTheEnd(value: String): String = {
37+
val lastIdxOfDollar = value.lastIndexOf('$')
38+
if (lastIdxOfDollar > 0) {
39+
val afterDollar = value.substring(lastIdxOfDollar + 1)
40+
try {
41+
afterDollar.toInt
42+
value.substring(0, lastIdxOfDollar)
43+
}
44+
catch {
45+
case t: Throwable => value
46+
}
47+
}
48+
else
49+
value
50+
}
51+
52+
val dict = v.asInstanceOf[scala.scalajs.js.Dictionary[Any]].flatMap { case (key, value) =>
53+
val decodedKey =
54+
scala.reflect.NameTransformer.decode(key).
55+
replaceAllLiterally("$$und", "_").
56+
replaceAllLiterally("$mcI", "").
57+
replaceAllLiterally("$sp", "").
58+
replaceAllLiterally("$f", "")
59+
60+
Some((filterDollarNumberAtTheEnd(decodedKey), value))
61+
}
62+
63+
new ObjectMeta {
64+
65+
lazy val fieldNames = dict.keys.toVector.filter(_ != "$$outer")
66+
67+
def value(name: String): Any = {
68+
dict.get(name) match {
69+
case Some(value) => value
70+
case None => throw new IllegalArgumentException("'" + name + "' is not attribute for this instance.")
71+
}
72+
}
73+
74+
def typeName(name: String): String = {
75+
dict.get(name) match {
76+
case Some(value) => value.getClass.getName
77+
case None => throw new IllegalArgumentException("'" + name + "' is not attribute for this instance.")
78+
}
79+
}
80+
81+
def shortTypeName(name: String): String = {
82+
dict.get(name) match {
83+
case Some(value) => value.getClass.getSimpleName
84+
case None => throw new IllegalArgumentException("'" + name + "' is not attribute for this instance.")
85+
}
86+
}
87+
}
88+
}
89+
90+
}

scalactic/src/main/scala/org/scalactic/Bool.scala

+19
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ trait Bool {
3434
private def makeString(raw: String, args: Array[Any]): String =
3535
Resources.formatString(raw, args.map(prettifier.apply))
3636

37+
lazy val analysis: scala.collection.immutable.IndexedSeq[String] = Vector.empty
38+
3739
/**
3840
* Construct and return failure message, by applying arguments returned from <code>failureMessageArgs</code> to
3941
* raw message returned from <code>rawFailureMessage</code>
@@ -657,6 +659,23 @@ private[scalactic] class BinaryMacroBool(left: Any, operator: String, right: Any
657659
def this(left: Any, operator: String, right: Any, bool: Bool, prettifier: Prettifier) =
658660
this(left, operator, right, bool.value, prettifier)
659661

662+
private lazy val prettyPair: PrettyPair = {
663+
val leftValue =
664+
left match {
665+
case aEqualizer: org.scalactic.TripleEqualsSupport#Equalizer[_] => aEqualizer.leftSide
666+
case aEqualizer: org.scalactic.TripleEqualsSupport#CheckingEqualizer[_] => aEqualizer.leftSide
667+
case _ => left
668+
}
669+
prettifier.apply(leftValue, right)
670+
}
671+
672+
override lazy val analysis: scala.collection.immutable.IndexedSeq[String] = {
673+
operator match {
674+
case "==" | "===" => prettyPair.analysis.map(Vector(_)).getOrElse(Vector.empty)
675+
case _ => Vector.empty
676+
}
677+
}
678+
660679
/**
661680
* the <code>Boolean</code> value of this <code>Bool</code>.
662681
*/

0 commit comments

Comments
 (0)