/
PrettifierSpec.scala
419 lines (405 loc) · 16.6 KB
/
PrettifierSpec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
* Copyright 2001-2013 Artima, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.scalactic
import org.scalatest._
import SharedHelpers.{javaList, javaSortedMap}
import scala.collection.mutable.WrappedArray
import scala.util.Success
// SKIP-SCALATESTJS,NATIVE-START
import scala.xml.NodeSeq
// SKIP-SCALATESTJS,NATIVE-END
class PrettifierSpec extends funspec.AnyFunSpec with matchers.should.Matchers {
describe("A Prettifier") {
it("should convert an Any to a String") {
val f =
new Prettifier {
def apply(o: Any) = o.toString
}
f("hi") should be ("hi")
f(List("hi")) should be ("List(hi)")
}
it("can be composed with another Prettifier") {
case class Yell(secret: String)
val myLittlePretty =
new Prettifier {
def apply(o: Any) =
o match {
case Yell(secret) => secret.toUpperCase + "!!!"
case _ => Prettifier.default(o)
}
}
myLittlePretty(Yell("I like fruit loops")) should be ("I LIKE FRUIT LOOPS!!!")
myLittlePretty("hi") should be ("\"hi\"")
myLittlePretty('h') should be ("'h'")
myLittlePretty(Array(1, 2, 3)) should be ("Array(1, 2, 3)")
myLittlePretty(WrappedArray.make(Array(1, 2, 3))) should be ("Array(1, 2, 3)")
myLittlePretty(null) should be ("null")
myLittlePretty(()) should be ("<(), the Unit value>")
myLittlePretty(List("1", "2", "3")) should be ("List(\"1\", \"2\", \"3\")")
}
// SKIP-DOTTY-START
// no runtime reflection in Dotty
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.") {
case class Yell(secret: String)
val myLittlePretty =
new Prettifier {
def apply(o: Any) =
o match {
case Yell(secret) => secret.toUpperCase + "!!!"
case _ => Prettifier.default(o)
}
}
val left = Yell("I like fruit loops")
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]\")"))
}
// SKIP-DOTTY-END
}
describe("the basic Prettifier") {
it("should put double quotes around strings") {
Prettifier.basic("hi") should be ("\"hi\"")
}
it("should put single quotes around chars") {
Prettifier.basic('h') should be ("'h'")
}
it("should pretty print arrays") {
Prettifier.basic(Array(1, 2, 3)) should be ("Array(1, 2, 3)")
}
it("should pretty print wrapped arrays") {
Prettifier.basic(WrappedArray.make(Array(1, 2, 3))) should be ("Array(1, 2, 3)")
}
it("should pretty print string arrays") {
Prettifier.basic(Array("1", "2", "3")) should be ("Array(1, 2, 3)")
}
it("should pretty print nested string arrays") {
Prettifier.basic(Array(Array("1", "2", "3"))) should be ("Array(Array(1, 2, 3))")
}
it("should pretty print wrapped string arrays") {
Prettifier.basic(WrappedArray.make(Array("1", "2", "3"))) should be ("Array(1, 2, 3)")
}
it("should show null as \"null\"") {
Prettifier.basic(null) should be ("null")
}
it("should clarify the Unit value") {
Prettifier.basic(()) should be ("<(), the Unit value>")
}
it("should just call toString on anything not specially treated") {
Prettifier.basic(List("1", "2", "3")) should be ("List(1, 2, 3)")
}
it("should pretty print GenTraversable") {
Prettifier.basic(List(1, 2, 3)) should be ("List(1, 2, 3)")
}
it("should pretty print string GenTraversable") {
Prettifier.basic(List("1", "2", "3")) should be ("List(1, 2, 3)")
}
it("should pretty print nested string GenTraversable") {
Prettifier.basic(List(List("1", "2", "3"))) should be ("List(List(1, 2, 3))")
}
it("should pretty print Some(Int)") {
Prettifier.basic(Some(8)) should be ("Some(8)")
}
it("should pretty print Some(String)") {
Prettifier.basic(Some("8")) should be ("Some(8)")
}
it("should pretty print nested Some(String)") {
Prettifier.basic(Some(Some("8"))) should be ("Some(Some(8))")
}
it("should pretty print Success(Int)") {
Prettifier.basic(Success(8)) should be ("Success(8)")
}
it("should pretty print Success(String)") {
Prettifier.basic(Success("8")) should be ("Success(8)")
}
it("should pretty print nested Success(String)") {
Prettifier.basic(Success(Success("8"))) should be ("Success(Success(8))")
}
it("should pretty print Left(Int)") {
Prettifier.basic(Left(8)) should be ("Left(8)")
}
it("should pretty print Left(String)") {
Prettifier.basic(Left("8")) should be ("Left(8)")
}
it("should pretty print nested Left(String)") {
Prettifier.basic(Left(Left("8"))) should be ("Left(Left(8))")
}
it("should pretty print Right(Int)") {
Prettifier.basic(Right(8)) should be ("Right(8)")
}
it("should pretty print Right(String)") {
Prettifier.basic(Right("8")) should be ("Right(8)")
}
it("should pretty print nested Right(String)") {
Prettifier.basic(Right(Right("8"))) should be ("Right(Right(8))")
}
it("should pretty print Good(Int)") {
Prettifier.basic(Good(8)) should be ("Good(8)")
}
it("should pretty print Good(String)") {
Prettifier.basic(Good("8")) should be ("Good(8)")
}
it("should pretty print nested Good(String)") {
Prettifier.basic(Good(Good("8"))) should be ("Good(Good(8))")
}
it("should pretty print Bad(Int)") {
Prettifier.basic(Bad(8)) should be ("Bad(8)")
}
it("should pretty print Bad(String)") {
Prettifier.basic(Bad("8")) should be ("Bad(8)")
}
it("should pretty print nested Bad(String)") {
Prettifier.basic(Bad(Bad("8"))) should be ("Bad(Bad(8))")
}
it("should pretty print One(Int)") {
Prettifier.basic(One(8)) should be ("One(8)")
}
it("should pretty print One(String)") {
Prettifier.basic(One("8")) should be ("One(8)")
}
it("should pretty print nested One(String)") {
Prettifier.basic(One(One("8"))) should be ("One(One(8))")
}
it("should pretty print Many(Int)") {
Prettifier.basic(Many(1, 2, 3)) should be ("Many(1, 2, 3)")
}
it("should pretty print Many(String)") {
Prettifier.basic(Many("1", "2", "3")) should be ("Many(1, 2, 3)")
}
it("should pretty print nested Many(String)") {
Prettifier.basic(Many(Many("1", "2", "3"), Many("7", "8", "9"))) should be ("Many(Many(1, 2, 3), Many(7, 8, 9))")
}
// SKIP-SCALATESTJS,NATIVE-START
it("should pretty print Java List") {
Prettifier.basic(javaList(1, 2, 3)) should be ("[1, 2, 3]")
}
it("should pretty print string Java List") {
Prettifier.basic(javaList("1", "2", "3")) should be ("[1, 2, 3]")
}
it("should pretty print nested string Java List") {
Prettifier.basic(javaList(javaList("1", "2", "3"))) should be ("[[1, 2, 3]]")
}
it("should pretty print Java Map") {
Prettifier.basic(javaSortedMap(Entry(1, 2), Entry(2, 3), Entry(3, 8))) should be ("{1=2, 2=3, 3=8}")
}
it("should pretty print string Java Map") {
Prettifier.basic(javaSortedMap(Entry(1, "one"), Entry(2, "two"), Entry(3, "three"))) should be ("{1=one, 2=two, 3=three}")
}
it("should pretty print nested string Java Map") {
Prettifier.basic(javaSortedMap(Entry("akey", javaSortedMap(Entry(1, "one"), Entry(2, "two"), Entry(3, "three"))))) should be ("{akey={1=one, 2=two, 3=three}}")
}
// SKIP-SCALATESTJS,NATIVE-END
}
describe("the default Prettifier") {
it("should put double quotes around strings") {
Prettifier.default("hi") should be ("\"hi\"")
}
it("should put double quotes around scala.collection.immutable.StringOps") {
Prettifier.default(new scala.collection.immutable.StringOps("hi")) should be ("\"hi\"")
}
it("should put single quotes around chars") {
Prettifier.default('h') should be ("'h'")
}
it("should pretty print arrays") {
Prettifier.default(Array(1, 2, 3)) should be ("Array(1, 2, 3)")
}
it("should pretty print wrapped arrays") {
Prettifier.default(WrappedArray.make(Array(1, 2, 3))) should be ("Array(1, 2, 3)")
}
it("should pretty print string arrays") {
Prettifier.default(Array("1", "2", "3")) should be ("Array(\"1\", \"2\", \"3\")")
}
it("should pretty print nested string arrays") {
Prettifier.default(Array(Array("1", "2", "3"))) should be ("Array(Array(\"1\", \"2\", \"3\"))")
}
it("should pretty print wrapped string arrays") {
Prettifier.default(WrappedArray.make(Array("1", "2", "3"))) should be ("Array(\"1\", \"2\", \"3\")")
}
it("should pretty print array ops") {
Prettifier.default(ArrayHelper.arrayOpsOfInt(Array(1, 2, 3))) should be ("Array(1, 2, 3)")
}
it("should show null as \"null\"") {
Prettifier.default(null) should be ("null")
}
it("should clarify the Unit value") {
Prettifier.default(()) should be ("<(), the Unit value>")
}
it("should just call toString on anything not specially treated") {
Prettifier.default(List("1", "2", "3")) should be ("List(\"1\", \"2\", \"3\")")
}
it("should pretty print GenTraversable") {
Prettifier.default(List(1, 2, 3)) should be ("List(1, 2, 3)")
}
it("should pretty print string GenTraversable") {
Prettifier.default(List("1", "2", "3")) should be ("List(\"1\", \"2\", \"3\")")
}
it("should pretty print nested string GenTraversable") {
Prettifier.default(List(List("1", "2", "3"))) should be ("List(List(\"1\", \"2\", \"3\"))")
}
it("should pretty print Some(Int)") {
Prettifier.default(Some(8)) should be ("Some(8)")
}
it("should pretty print Some(String)") {
Prettifier.default(Some("8")) should be ("Some(\"8\")")
}
it("should pretty print nested Some(String)") {
Prettifier.default(Some(Some("8"))) should be ("Some(Some(\"8\"))")
}
it("should pretty print Success(Int)") {
Prettifier.default(Success(8)) should be ("Success(8)")
}
it("should pretty print Success(String)") {
Prettifier.default(Success("8")) should be ("Success(\"8\")")
}
it("should pretty print nested Success(String)") {
Prettifier.default(Success(Success("8"))) should be ("Success(Success(\"8\"))")
}
it("should pretty print Left(Int)") {
Prettifier.default(Left(8)) should be ("Left(8)")
}
it("should pretty print Left(String)") {
Prettifier.default(Left("8")) should be ("Left(\"8\")")
}
it("should pretty print nested Left(String)") {
Prettifier.default(Left(Left("8"))) should be ("Left(Left(\"8\"))")
}
it("should pretty print Right(Int)") {
Prettifier.default(Right(8)) should be ("Right(8)")
}
it("should pretty print Right(String)") {
Prettifier.default(Right("8")) should be ("Right(\"8\")")
}
it("should pretty print nested Right(String)") {
Prettifier.default(Right(Right("8"))) should be ("Right(Right(\"8\"))")
}
it("should pretty print Good(Int)") {
Prettifier.default(Good(8)) should be ("Good(8)")
}
it("should pretty print Good(String)") {
Prettifier.default(Good("8")) should be ("Good(\"8\")")
}
it("should pretty print nested Good(String)") {
Prettifier.default(Good(Good("8"))) should be ("Good(Good(\"8\"))")
}
it("should pretty print Bad(Int)") {
Prettifier.default(Bad(8)) should be ("Bad(8)")
}
it("should pretty print Bad(String)") {
Prettifier.default(Bad("8")) should be ("Bad(\"8\")")
}
it("should pretty print nested Bad(String)") {
Prettifier.default(Bad(Bad("8"))) should be ("Bad(Bad(\"8\"))")
}
it("should pretty print One(Int)") {
Prettifier.default(One(8)) should be ("One(8)")
}
it("should pretty print One(String)") {
Prettifier.default(One("8")) should be ("One(\"8\")")
}
it("should pretty print nested One(String)") {
Prettifier.default(One(One("8"))) should be ("One(One(\"8\"))")
}
it("should pretty print Many(Int)") {
Prettifier.default(Many(1, 2, 3)) should be ("Many(1, 2, 3)")
}
it("should pretty print Many(String)") {
Prettifier.default(Many("1", "2", "3")) should be ("Many(\"1\", \"2\", \"3\")")
}
it("should pretty print nested Many(String)") {
Prettifier.default(Many(Many("1", "2", "3"), Many("7", "8", "9"))) should be ("Many(Many(\"1\", \"2\", \"3\"), Many(\"7\", \"8\", \"9\"))")
}
// SKIP-SCALATESTJS,NATIVE-START
it("should pretty print Java List") {
Prettifier.default(javaList(1, 2, 3)) should be ("[1, 2, 3]")
}
it("should pretty print string Java List") {
Prettifier.default(javaList("1", "2", "3")) should be ("[\"1\", \"2\", \"3\"]")
}
it("should pretty print nested string Java List") {
Prettifier.default(javaList(javaList("1", "2", "3"))) should be ("[[\"1\", \"2\", \"3\"]]")
}
it("should pretty print Java Map") {
Prettifier.default(javaSortedMap(Entry(1, 2), Entry(2, 3), Entry(3, 8))) should be ("{1=2, 2=3, 3=8}")
}
it("should pretty print string Java Map") {
Prettifier.default(javaSortedMap(Entry(1, "one"), Entry(2, "two"), Entry(3, "three"))) should be ("{1=\"one\", 2=\"two\", 3=\"three\"}")
}
it("should pretty print nested string Java Map") {
Prettifier.default(javaSortedMap(Entry("akey", javaSortedMap(Entry(1, "one"), Entry(2, "two"), Entry(3, "three"))))) should be ("{\"akey\"={1=\"one\", 2=\"two\", 3=\"three\"}}")
}
case class CaseClazzWithArray(data: Array[Int])
it("should pretty print data inside a case class") {
Prettifier.default(CaseClazzWithArray(Array(1,2,3))) should be ("CaseClazzWithArray(Array(1, 2, 3))")
}
it("should pretty print xml <a></a>") {
Prettifier.default(<a></a>) should be ("<a></a>")
}
it("should pretty print xml <a/>") {
Prettifier.default(<a/>) should be ("<a/>")
}
it("should pretty print xml <a><b/></a>") {
Prettifier.default(<a><b/></a>) should be ("<a><b/></a>")
}
it("should pretty print xml <a/><b/>") {
Prettifier.default(<a/><b/>) should be ("<a/><b/>")
}
it("should pretty print xml.NodeSeq <a/><b/>") {
val ab: NodeSeq = <a/><b/>;
Prettifier.default(ab) should be ("<a/><b/>")
}
// SKIP-SCALATESTJS,NATIVE-END
// SKIP-DOTTY-START
it("should handle runaway recursion gracefully, if not necessarily quickly") {
/*
You'd think no one would do this, but:
scala> val me = <a></a>
me: scala.xml.Elem = <a></a>
scala> val you = me.iterator.next
you: scala.xml.Node = <a></a>
scala> me eq you
res0: Boolean = true
*/
class Fred extends scala.collection.Seq[Fred] { thisFred =>
//override def toIterator: Iterator[Fred] = iterator
def iterator: Iterator[Fred] =
new Iterator[Fred] {
private var hasNextElement: Boolean = true
def next(): Fred = {
if (hasNextElement) {
hasNextElement = false
thisFred
}
else throw new NoSuchElementException
}
def hasNext: Boolean = hasNextElement
}
def apply(idx: Int): Fred = if (idx == 0) thisFred else throw new NoSuchElementException
def length: Int = 1
override def toString = "It's Fred all the way down"
}
Prettifier.default(new Fred) shouldBe "It's Fred all the way down"
}
// SKIP-DOTTY-END
it("should truncate collection when used with Prettifier.truncateAt") {
val col = List(1, 2, 3)
val prettifier = Prettifier.truncateAt(SizeLimit(2))
prettifier(col) shouldBe "List(1, 2, ...)"
}
case class CaseClazz(data: List[Int])
it("should truncate collection inside of a case class when used with Prettifier.truncateAt") {
val caseClass = CaseClazz(List(1, 2, 3))
val prettifier = Prettifier.truncateAt(SizeLimit(2))
prettifier(caseClass) shouldBe "CaseClazz(List(1, 2, ...))"
}
}
}