-
Notifications
You must be signed in to change notification settings - Fork 9
/
DoctorPrettyTests.swift
240 lines (214 loc) · 7.33 KB
/
DoctorPrettyTests.swift
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
import XCTest
@testable import DoctorPretty
import Operadics
// Most tests ported from https://github.com/minad/wl-pprint-annotated/blob/master/test/WLPPrintTests.hs
class DoctorPrettyTests: XCTestCase {
func testSlow() {
measure {
let doc = (1...30).map { _ in
(1...30).map { _ in "foo" }.map(Doc.text).fillSep()
}.vcat()
_ = doc.renderPrettyDefault()
}
}
func assertPretty(pageWidth: Width, str: String, doc: Doc, file: StaticString = #file, line: UInt = #line) {
XCTAssertEqual(doc.renderPretty(ribbonFrac: 1.0, pageWidth: pageWidth).displayString(), str, file: file, line: line)
}
func testSimpleConstructors() {
assertPretty(pageWidth: 80, str: "", doc: Doc.zero)
assertPretty(pageWidth: 80, str: "a", doc: Doc.char("a"))
assertPretty(pageWidth: 80, str: "text...", doc: Doc.text("text..."))
assertPretty(pageWidth: 80, str: "\n", doc: Doc.hardline)
}
func testFlatAltConstructor() {
assertPretty(pageWidth: 80, str: "x", doc: .flatAlt(primary: .text("x"), whenFlattened: .text("y")))
assertPretty(pageWidth: 80, str: "y", doc: Doc.flatAlt(primary: .text("x"), whenFlattened: .text("y")).flattened)
}
func testCat() {
assertPretty(pageWidth: 80, str: "some code", doc: .text("some") <> Doc.space <> .text("code"))
}
func testNest() {
assertPretty(
pageWidth: 80,
str: "foo bar",
doc: .text("foo") <+> .nest(2, .text("bar"))
)
assertPretty(
pageWidth: 80,
str: "foo\n bar",
doc: .text("foo") <> .nest(2, .line <> .text("bar"))
)
}
func testUnion() {
assertPretty(pageWidth: 80, str: "foo bar",
doc: .text("foo") <%> .text("bar"))
assertPretty(pageWidth: 5, str: "foo\nbar",
doc: .text("foo") <%> .text("bar"))
}
func testFuncConstructors() {
assertPretty(pageWidth: 80, str: "foo 4",
doc: .text("foo") <+> .column { .text("\($0)") })
assertPretty(pageWidth: 80, str: "foo 2",
doc: .text("foo") <+> .nest(2, .nesting { .text("\($0)") }))
assertPretty(pageWidth: 21, str: "foo 21",
doc: .text("foo") <+> .nest(2, .columns { .text("\($0!)") }))
XCTAssertEqual("foo 40",
(.text("foo") <+> .ribbon { .text("\($0!)") }).renderPrettyDefault().displayString())
}
func testHang() {
let words = "the hang combinator indents these words !".components(separatedBy: " ").map{ Doc.text($0) }
assertPretty(
pageWidth: 20,
str: ["the hang combinator",
" indents these",
" words !" ].joined(separator: "\n"),
doc: words.fillSep().hang(4)
)
}
func testAlign() {
assertPretty(
pageWidth: 20,
str: ["hi nice",
" world" ].joined(separator: "\n"),
doc: .text("hi") <+> ((.text("nice") <> .linebreak <> .text("world"))).align()
)
}
func testPunctuate() {
assertPretty(
pageWidth: 80,
str: "a,b,c",
doc: ["a", "b", "c"]
.map(Doc.char)
.punctuate(with: Doc.comma)
.cat()
)
}
func testEncloseSep() {
let list1 = ["foo", "bar", "baz"]
.map(Doc.text)
.list(indent: 4)
let doc = .text("let x =") <%> list1
assertPretty(
pageWidth: 80,
str: "let x = [foo, bar, baz]",
doc: doc
)
assertPretty(
pageWidth: 10,
str: ["let x = [",
" foo,",
" bar,",
" baz",
"]"].joined(separator: "\n"),
doc: doc
)
let list2 = [list1].list(indent: 4)
let list3 = [list2].list(indent: 4)
let docBigger = .text("let x =") <%> list3
assertPretty(
pageWidth: 20,
str: ["let x = [",
" [",
" [",
" foo,",
" bar,",
" baz",
" ]",
" ]",
"]"].joined(separator: "\n"),
doc: docBigger
)
}
func testSwiftExample() {
let Indentation = 4
let funName: Doc = .text("aLongFunction")
let args: Doc = [
("foo", "String"),
("bar", "Int"),
("baz", "Long")
].map{ (name, typ) in
.text(name) <> .text(":") <%> .text(typ)
}.tupled(indent: Indentation)
let retType: Doc = ["String", "Int", "Long"]
.map(Doc.text)
.tupled(indent: Indentation)
let retValue: Doc = ["foo", "bar", "baz"]
.map(Doc.text)
.tupled(indent: Indentation)
let retExpr: Doc = .text("return") <%> retValue
let body: Doc = [
.text("sideEffect()"),
retExpr
].vsep()
let funcBody: Doc = [
Doc.lbrace, body.indent(Indentation), Doc.rbrace
].vsep()
let doc: Doc =
.text("func") <> Doc.space <> funName <> args <> Doc.space <> .text("->") <> Doc.space <> retType <%> funcBody
assertPretty(pageWidth: 120, str: [
"func aLongFunction(foo: String, bar: Int, baz: Long) -> (String, Int, Long) {",
" sideEffect()",
" return (foo, bar, baz)",
"}"
].joined(separator: "\n"), doc: doc)
assertPretty(pageWidth: 40, str: [
"func aLongFunction(",
" foo: String, bar: Int, baz: Long",
") -> (String, Int, Long) {",
" sideEffect()",
" return (foo, bar, baz)",
"}"
].joined(separator: "\n"), doc: doc)
assertPretty(pageWidth: 20, str: [
"func aLongFunction(",
" foo: String,",
" bar: Int,",
" baz: Long",
") -> (",
" String,",
" Int,",
" Long",
") {",
" sideEffect()",
" return (",
" foo,",
" bar,",
" baz",
" )",
"}"
].joined(separator: "\n"), doc: doc)
}
func testLargeDocument() {
let doc = (1...65)
.map { _ in "foo" }
.map(Doc.text)
.fillSep()
XCTAssertEqual(
"""
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo foo foo foo foo foo foo foo
foo
""",
doc.renderPretty(ribbonFrac: 0.4, pageWidth: 80).displayString()
)
}
static var allTests = [
("testSimpleConstructors", testSimpleConstructors),
("testFlatAltConstructor", testFlatAltConstructor),
("testCat", testCat),
("testNest", testNest),
("testUnion", testUnion),
("testFuncConstructors", testFuncConstructors),
("testHang", testHang),
("testAlign", testAlign),
("testPunctuate", testPunctuate),
("testEncloseSep", testEncloseSep),
("testSwiftExample", testSwiftExample)
]
}