-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prettier_print_test.rb
219 lines (174 loc) · 5.24 KB
/
prettier_print_test.rb
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
# frozen_string_literal: true
require "test_helper"
require "pp"
class PrettierPrint
class PrettierPrintTest < Test::Unit::TestCase
test "Align#pretty_print" do
assert_equal "align0([])\n", PP.pp(Align.new(indent: 0), +"")
end
test "Breakable#pretty_print" do
assert_equal "breakable\n", PP.pp(Breakable.new, +"")
end
test "Breakable#pretty_print force=true" do
assert_equal "breakable(force=true)\n",
PP.pp(Breakable.new(force: true), +"")
end
test "Breakable#pretty_print indent=false" do
assert_equal "breakable(indent=false)\n",
PP.pp(Breakable.new(indent: false), +"")
end
test "BreakParent#pretty_print" do
assert_equal "break-parent\n", PP.pp(BreakParent.new, +"")
end
test "Group#pretty_print" do
assert_equal "group([])\n", PP.pp(Group.new(0), +"")
end
test "Group#pretty_print break" do
assert_equal "breakGroup([])\n", PP.pp(Group.new(0).tap(&:break), +"")
end
test "IfBreak#pretty_print" do
assert_equal "if-break([], [])\n", PP.pp(IfBreak.new, +"")
end
test "Indent#pretty_print" do
assert_equal "indent([])\n", PP.pp(Indent.new, +"")
end
test "LineSuffix#pretty_print" do
assert_equal "line-suffix([])\n", PP.pp(LineSuffix.new, +"")
end
test "Text#pretty_print" do
assert_equal "text([])\n", PP.pp(Text.new, +"")
end
test "Trim#pretty_print" do
assert_equal "trim\n", PP.pp(Trim.new, +"")
end
test "Buffer::StrinfBuffer#trim!" do
buffer = Buffer::StringBuffer.new
buffer << "......"
buffer << +"... "
buffer << " "
buffer << "\t\t\t"
buffer << "\t \t "
buffer.trim!
assert_equal ".........", buffer.output
end
test "Buffer::ArrayBuffer#trim!" do
buffer = Buffer::ArrayBuffer.new
buffer << "......"
buffer << +"... "
buffer << " "
buffer << "\t\t\t"
buffer << "\t \t "
buffer.trim!
assert_equal ".........", buffer.output.join
end
test "PrettierPrint#nest" do
result =
PrettierPrint.format do |q|
q.nest(4) do
q.breakable(force: true)
q.text("content")
end
end
assert_equal "\n content", result
end
test "PrettierPrint#breakable(indent: false)" do
result =
PrettierPrint.format do |q|
q.nest(4) do
q.breakable_return
q.text("content")
end
end
assert_equal "\ncontent", result
end
test "PrettierPrint#break_parent" do
result =
PrettierPrint.format do |q|
q.if_break { q.text("break") }.if_flat { q.text("flat") }
q.break_parent
end
assert_equal "break", result
end
test "PrettierPrint#if_break" do
result =
PrettierPrint.format do |q|
q.if_break { q.text("break") }.if_flat { q.text("flat") }
end
assert_equal "flat", result
end
test "PrettierPrint#if_flat" do
result = PrettierPrint.format { |q| q.if_flat { q.text("flat") } }
assert_equal "flat", result
end
test "PrettierPrint#indent" do
result =
PrettierPrint.format do |q|
q.indent do
q.breakable_force
q.text("content")
end
end
assert_equal "\n content", result
end
test "PrettierPrint#line_suffix" do
result =
PrettierPrint.format do |q|
q.line_suffix { q.text(" # suffix") }
q.text("content")
q.breakable_force
end
assert_equal "content # suffix\n", result
end
test "PrettierPrint#line_suffix without a break" do
result = PrettierPrint.format { |q| q.line_suffix { q.text("# suffix") } }
assert_equal "# suffix", result
end
test "PrettierPrint#trim" do
result =
PrettierPrint.format do |q|
q.indent do
q.breakable_force
q.text("first content")
q.breakable_force
q.trim
q.text("second content")
end
end
assert_equal "\n first content\nsecond content", result
end
test "PrettierPrint pushing strings" do
result = PrettierPrint.format { |q| q.target << "content" }
assert_equal "content", result
end
test "PrettierPrint pushing unknown objects" do
q = PrettierPrint.new([])
q.target << Object.new
q.target << "content"
q.flush
q.output.shift
assert_equal "content", q.output.join
end
test "PrettierPrint#last_position string" do
q = PrettierPrint.new
assert_equal 5, q.last_position(".....")
end
test "PrettierPrint#last_position group" do
q = PrettierPrint.new
q.text(".....")
assert_equal 5, q.last_position(q.groups.first)
end
test "PrettierPrint#last_position breakable" do
q = PrettierPrint.new
q.text(".....")
q.breakable
q.text("...")
assert_equal 3, q.last_position(q.groups.first)
end
test "PrettierPrint#last_position if break" do
q = PrettierPrint.new
q.text(".....")
q.if_break { q.text("...") }
assert_equal 8, q.last_position(q.groups.first)
end
end
end