-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.lua
More file actions
236 lines (228 loc) · 7.96 KB
/
test.lua
File metadata and controls
236 lines (228 loc) · 7.96 KB
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
local lu = dofile('../../src/hclua/vendor/luaunit/luaunit.lua')
local runtime=dofile('../../src/hclua/runtime/runtime.lua')
runtime.path='../../src/hclua/'
local rt=runtime.Runtime:new()
local line=rt:requireModule('lib/line/line.lua')
function TestLine()
local li=line.Line:new()
local word=line.Word:new()
word.Text='test'
word.Color='Red'
local short=word:toShort()
local word2=line.Word:new()
word2.Text='test2'
word2.Color='Green'
local short2=word2:toShort()
li:appendWord(word):appendWord(word2)
lu.assertEquals(li.Text,'testtest2')
lu.assertEquals(li:toShort(),short..short2)
end
function TestParse()
local word=line.Word:new()
word.Text='test'
local li=line.Line:new()
li:appendWord(word)
local li2=line.parseLine(li:toShort())
lu.assertEquals(#li2.Words,1)
lu.assertEquals(li2.Words[1].Text,word.Text)
-- 样式
word=line.Word:new()
lu.assertNotIsTrue(line.parseLine(word:toShort())==nil)
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Bold,false)
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Underlined,false)
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Blinking,false)
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Inverse,false)
word.Bold=true
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Bold,true)
word.Underlined=true
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Underlined,true)
word.Blinking=true
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Blinking,true)
word.Inverse=true
lu.assertEquals(line.parseLine(word:toShort()).Words[1].Inverse,true)
-- 转义
word=line.Word:new()
word.Text='#test##'
li=line.Line:new():appendWord(word)
lu.assertEquals(word:toShort(),li:toShort())
li2=line.parseLine(li:toShort())
lu.assertEquals(li2.Words[1].Text,word.Text)
-- 无效
lu.assertEquals(line.parseLine('#3'),nil)
lu.assertEquals(line.parseLine('#01'),nil)
lu.assertEquals(line.parseLine('#1xxxxxx'),nil)
lu.assertEquals(line.parseLine('###'),nil)
lu.assertEquals(line.parseLine('abc'),nil)
lu.assertEquals(line.parseLine('#0AA0a#'),nil)
end
function TestShort()
local word=line.Word:new()
local word2=line.Word:new()
word.Bold=true
word.Color='Red'
word.Text='text'
word2.Bold=true
word2.Color='Red'
word2.Text='text2'
lu.assertEquals(word:getShortStyle(),word2:getShortStyle())
word2.Inverse=true
lu.assertNotIsTrue(word:getShortStyle(),word2:getShortStyle())
end
function TestCopy()
local word=line.Word:new()
word.Bold=true
word.Underlined=true
word.Blinking=true
word.Inverse=true
word.Color='Red'
word.Text='text'
local word2=word:copyStyle()
lu.assertEquals(word2.Text,'')
lu.assertEquals(word2:getShortStyle(),word:getShortStyle())
local word3=word:copyStyle('test3')
lu.assertEquals(word3.Text,'test3')
lu.assertEquals(word3:getShortStyle(),word:getShortStyle())
end
function TestSlice()
local li=line.Line:new()
local li2
local word=line.Word:new()
word.Text='a'
word.Color='Red'
local word2=line.Word:new()
word2.Text='bc'
word2.Color='Green'
local word3=line.Word:new()
word3.Text='def'
word3.Color='Blue'
local word4=line.Word:new()
word4.Text='ghij'
word4.Color='White'
li:appendWord(word)
:appendWord(word2)
:appendWord(word3)
:appendWord(word4)
-- 无效参数
li2=li:slice(0)
lu.assertEquals(li2,nil)
li2=li:slice(1,0)
lu.assertEquals(li2,nil)
-- 默认擦数
li2=li:slice()
lu.assertEquals(li2:toShort(),word:copyStyle('a'):toShort())
li2=li:slice(1,1)
lu.assertEquals(li2:toShort(),word:copyStyle('a'):toShort())
li2=li:slice(2)
lu.assertEquals(li2:toShort(),word2:copyStyle('b'):toShort())
li2=li:slice(2,1)
lu.assertEquals(li2:toShort(),word2:copyStyle('b'):toShort())
-- 超长
li2=li:slice(1,99)
lu.assertEquals(li2:toShort(),li:toShort())
li2=li:slice(1,#li.Text)
lu.assertEquals(li2:toShort(),li:toShort())
li2=li:slice(2,99)
lu.assertEquals(word:toShort()..li2:toShort(),li:toShort())
li2=li:slice(5,99)
lu.assertEquals(li2:toShort(),word3:copyStyle('ef'):toShort()..word4:toShort())
-- 开头切到正好
li2=li:slice(1,3)
lu.assertEquals(li2:toShort(),word:toShort()..word2:toShort())
-- 开头切到当中
li2=li:slice(1,4)
lu.assertEquals(li2:toShort(),word:toShort()..word2:toShort()..word3:copyStyle('d'):toShort())
--正好切到正好
li2=li:slice(2,5)
lu.assertEquals(li2:toShort(),word2:toShort()..word3:toShort())
--正好到当中
li2=li:slice(2,7)
lu.assertEquals(li2:toShort(),word2:toShort()..word3:toShort()..word4:copyStyle('gh'):toShort())
-- 正好到结尾
li2=li:slice(2,#li.Text-1)
lu.assertEquals(li2:toShort(),word2:toShort()..word3:toShort()..word4:toShort())
-- 当中到正好
li2=li:slice(3,4)
lu.assertEquals(li2:toShort(),word2:copyStyle('c'):toShort()..word3:toShort())
-- 当中到当中
li2=li:slice(3,3)
lu.assertEquals(li2:toShort(),word2:copyStyle('c'):toShort()..word3:copyStyle('de'):toShort())
-- 当中到结尾
li2=li:slice(3,#li.Text-2)
lu.assertEquals(li2:toShort(),word2:copyStyle('c'):toShort()..word3:toShort()..word4:toShort())
-- 部分截取
li2=li:slice(8,2)
lu.assertEquals(li2:toShort(),word4:copyStyle('hi'):toShort())
end
-- 测试同样式单词合并
function TestAppend()
local li=line.Line:new()
local word=line.Word:new()
word.Text='a'
word.Color='Red'
local word2=line.Word:new()
word2.Text='b'
word2.Color='Green'
word2.Bold=true
local word3=word2:copyStyle('c')
li:appendWord(word):appendWord(word2):appendWord(word3)
lu.assertEquals(li.Text,'abc')
lu.assertEquals(#li.Words,2)
lu.assertEquals(li.Words[1]:toShort(),word:toShort())
lu.assertEquals(li.Words[2].Text,'bc')
lu.assertEquals(li.Words[2]:getShortStyle(),word2:getShortStyle())
end
function TestShortColor()
local word=line.Word:new()
word.Text='a'
word.Color='Red'
word.Background='Green'
word.Bold=true
local short=word:toShort()
lu.assertEquals(string.sub(short,1,1),'#')
lu.assertEquals(string.sub(short,2,2),'0')
local wordparsed=line.parseLine(short).Words[1]
lu.assertEquals(word.Text,wordparsed.Text)
lu.assertEquals(word.Color,wordparsed.Color)
lu.assertEquals(word.Background,wordparsed.Background)
lu.assertEquals(word.Bold,wordparsed.Bold)
word=line.Word:new()
word.Text='a'
word.Color='#333333'
word.Background='Green'
word.Bold=true
short=word:toShort()
lu.assertEquals(string.sub(short,1,1),'#')
lu.assertEquals(string.sub(short,2,2),'1')
wordparsed=line.parseLine(short).Words[1]
lu.assertEquals(word.Text,wordparsed.Text)
lu.assertEquals(word.Color,wordparsed.Color)
lu.assertEquals(word.Background,wordparsed.Background)
lu.assertEquals(word.Bold,wordparsed.Bold)
word=line.Word:new()
word.Text='a'
word.Color='Red'
word.Background='#666666'
word.Bold=true
short=word:toShort()
lu.assertEquals(string.sub(short,1,1),'#')
lu.assertEquals(string.sub(short,2,2),'1')
wordparsed=line.parseLine(short).Words[1]
lu.assertEquals(word.Text,wordparsed.Text)
lu.assertEquals(word.Color,wordparsed.Color)
lu.assertEquals(word.Background,wordparsed.Background)
lu.assertEquals(word.Bold,wordparsed.Bold)
word=line.Word:new()
word.Text='a'
word.Color='#333333'
word.Background='#666666'
word.Bold=true
short=word:toShort()
lu.assertEquals(string.sub(short,1,1),'#')
lu.assertEquals(string.sub(short,2,2),'1')
wordparsed=line.parseLine(short).Words[1]
lu.assertEquals(word.Text,wordparsed.Text)
lu.assertEquals(word.Color,wordparsed.Color)
lu.assertEquals(word.Background,wordparsed.Background)
lu.assertEquals(word.Bold,wordparsed.Bold)
end
os.exit( lu.LuaUnit.run() )