/
Printers.scala
214 lines (203 loc) · 6.2 KB
/
Printers.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
// Adaptation of https://github.com/lihaoyi/PPrint/blob/e6a918c259ed7ae1998bbf58c360334a3f0157ca/pprint/src/pprint/Walker.scala
package munit.diff.console
import munit.diff.{EmptyPrinter, Printable, NextPrintable, Printer}
import scala.annotation.switch
import munit.diff.internal.Compat
object Printers {
val indentStep = 2
/** Pretty-prints the value in a format that's optimized for producing diffs */
def print(any: Any, printer: Printer = EmptyPrinter): String = {
var height = printer.height
val out = new StringBuilder()
def loop(a: Any, indent: Int): Unit = {
height -= 1
if (height < 0) {
out.append("...")
return
}
val nextIndent = indent + indentStep
val isDone = printer.print(a, out, indent)
if (!isDone) {
a match {
case null => out.append("null")
case x: Printable => x.print(out, indent)
case x: NextPrintable => x.print(out, indent, loop)
case x: Char =>
out.append('\'')
if (x == '\'') out.append("\\'")
else printChar(x, out)
out.append('\'')
case x: Byte => out.append(x.toString())
case x: Short => out.append(x.toString())
case x: Int => out.append(x.toString())
case x: Long => out.append(x.toString())
case x: Float => out.append(x.toString())
case x: Double => out.append(x.toString())
case x: String => printString(x, out, printer)
case None =>
out.append("None")
case Nil =>
out.append("Nil")
case x: Map[_, _] =>
printApply(
Compat.collectionClassName(x),
x.iterator,
out,
indent,
nextIndent
) { case (key, value) =>
loop(key, nextIndent)
out.append(" -> ")
loop(value, nextIndent)
}
case x: Iterable[_] =>
printApply(
Compat.collectionClassName(x),
x.iterator,
out,
indent,
nextIndent
) { value => loop(value, nextIndent) }
case x: Array[_] =>
printApply(
"Array",
x.iterator,
out,
indent,
nextIndent
) { value => loop(value, nextIndent) }
case it: Iterator[_] =>
if (it.isEmpty) out.append("empty iterator")
else out.append("non-empty iterator")
case p: Product =>
val elementNames = Compat.productElementNames(p)
val infiniteElementNames = Iterator.continually {
if (elementNames.hasNext) elementNames.next()
else ""
}
printApply(
p.productPrefix,
p.productIterator.zip(infiniteElementNames),
out,
indent,
nextIndent
) { case (value, key) =>
if (key.nonEmpty) {
out.append(key).append(" = ")
}
loop(value, nextIndent)
}
case _ =>
out.append(a.toString())
}
}
}
loop(any, indent = 0)
munit.diff.console.AnsiColors.filterAnsi(out.toString())
}
private[munit] def printApply[T](
prefix: String,
it: Iterator[T],
out: StringBuilder,
indent: Int,
nextIndent: Int,
open: String = "(",
close: String = ")",
comma: String = ","
)(fn: T => Unit): Unit = {
out.append(prefix)
out.append(open)
if (it.hasNext) {
printNewline(out, nextIndent)
while (it.hasNext) {
val value = it.next()
fn(value)
if (it.hasNext) {
out.append(comma)
printNewline(out, nextIndent)
} else {
printNewline(out, indent)
}
}
}
out.append(close)
}
private def printNewline(out: StringBuilder, indent: Int): Unit = {
out.append("\n")
var i = 0
while (i < indent) {
out.append(' ')
i += 1
}
}
private def printString(
string: String,
out: StringBuilder,
printer: Printer
): Unit = {
val isMultiline = printer.isMultiline(string)
if (isMultiline) {
out.append('"')
out.append('"')
out.append('"')
out.append(string)
out.append('"')
out.append('"')
out.append('"')
} else {
out.append('"')
var i = 0
while (i < string.length()) {
printChar(string.charAt(i), out)
i += 1
}
out.append('"')
}
}
/**
* Pretty-prints this string with non-visible characters escaped.
*
* The exact definition of "non-visible" is fuzzy and is subject to change.
* The original motivation for this method was to fix
* https://github.com/scalameta/munit/issues/258 related to escaping \r in
* test names.
*
* The spirit of this method is to preserve "visible" characters like emojis
* and double quotes and escape "non-visible" characters like newlines and
* ANSI escape codes. A non-goal of this method is to make the output
* copy-pasteable back into source code unlike the `printChar` method, which
* escapes for example double-quote characters.
*/
def escapeNonVisible(string: String): String = {
val out = new StringBuilder()
var i = 0
while (i < string.length()) {
val ch = string.charAt(i)
ch match {
case '"' | '\'' => out.append(ch)
case _ => printChar(ch, out, isEscapeUnicode = false)
}
i += 1
}
out.toString()
}
private def printChar(
c: Char,
sb: StringBuilder,
isEscapeUnicode: Boolean = true
): Unit =
(c: @switch) match {
case '"' => sb.append("\\\"")
case '\\' => sb.append("\\\\")
case '\b' => sb.append("\\b")
case '\f' => sb.append("\\f")
case '\n' => sb.append("\\n")
case '\r' => sb.append("\\r")
case '\t' => sb.append("\\t")
case c =>
val isNonReadableAscii = c < ' ' || (c > '~' && isEscapeUnicode)
if (isNonReadableAscii && !Character.isLetter(c))
sb.append("\\u%04x".format(c.toInt))
else sb.append(c)
}
}