-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathserializer_spec.rb
149 lines (129 loc) · 3.72 KB
/
serializer_spec.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
gem 'minitest'
require 'minitest/autorun'
require 'ruby2js'
class TestSerializer < Ruby2JS::Serializer
attr_accessor :lines
end
describe 'serializer tests' do
before do
@serializer = TestSerializer.new
@serializer.enable_vertical_whitespace
end
describe 'put' do
it 'should put a token' do
@serializer.put 'hi'
_(@serializer.lines).must_equal [['hi']]
end
it 'should put a token with a newline' do
@serializer.put "hi\n"
_(@serializer.lines).must_equal [['hi'], []]
end
it 'should handle multiple newlines' do
@serializer.put "\nhi\n"
_(@serializer.lines).must_equal [[''], ['hi'], []]
end
end
describe 'puts' do
it 'should puts a token' do
@serializer.puts 'hi'
_(@serializer.lines).must_equal [['hi'], []]
end
it 'should embedded multiple newlines' do
@serializer.puts "\nhi"
_(@serializer.lines).must_equal [[''], ['hi'], []]
end
end
describe 'sput' do
it 'should sput a token' do
@serializer.sput 'hi'
_(@serializer.lines).must_equal [[], ['hi']]
end
it 'should embedded multiple newlines' do
@serializer.sput "hi\n"
_(@serializer.lines).must_equal [[], ['hi'], []]
end
end
describe 'output location' do
it 'should track output location' do
_(@serializer.output_location).must_equal [0,0]
@serializer.put 'a'
@serializer.put 'b'
_(@serializer.output_location).must_equal [0,2]
@serializer.puts 'c'
@serializer.put 'd'
@serializer.put 'e'
_(@serializer.output_location).must_equal [1,2]
end
end
describe 'capture' do
it 'should capture tokens' do
@serializer.put 'a'
text = @serializer.capture do
@serializer.put 'b'
@serializer.puts 'c'
@serializer.put 'd'
@serializer.put 'e'
end
_(@serializer.lines).must_equal [['a']]
_(text).must_equal "bc\nde"
end
end
describe 'wrap' do
it "shouldn't wrap short lines" do
@serializer.put 'if (condition) '
@serializer.wrap do
@serializer.put 'statement'
end
_(@serializer.lines).must_equal [['if (condition) ', 'statement']]
end
it "should wrap long lines" do
@serializer.put 'if (condition-condition-condition-condition-condition) '
@serializer.wrap do
@serializer.put 'statement-statement-statement-statement-statement'
end
_(@serializer.lines).must_equal [
["if (condition-condition-condition-condition-condition) ", "{"],
["statement-statement-statement-statement-statement"],
["}"]
]
end
end
describe 'compact' do
it "should compact short lines" do
@serializer.compact do
@serializer.puts '['
@serializer.put 'token'
@serializer.sput ']'
end
_(@serializer.lines).must_equal [["[", "token", "]"]]
end
it "shouldn't compact long lines" do
@serializer.compact do
@serializer.puts '['
29.times { @serializer.put 'token, '}
@serializer.put 'token'
@serializer.sput ']'
end
_(@serializer.lines).must_equal [["["], ["token, "]*29 + ["token"], ["]"]]
end
it "shouldn't compact comments" do
@serializer.compact do
@serializer.puts '['
@serializer.put '// comment'
@serializer.sput ']'
end
_(@serializer.lines).must_equal [["["], ["// comment"], ["]"]]
end
end
describe 'serialize' do
it 'should serialize tokens' do
@serializer.enable_vertical_whitespace
@serializer.put 'a'
@serializer.put 'b'
@serializer.puts 'c'
@serializer.put 'd'
@serializer.put 'e'
_(@serializer.to_s).must_equal "abc\nde"
end
end
end