This repository was archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongString.lua
More file actions
264 lines (245 loc) · 8.07 KB
/
LongString.lua
File metadata and controls
264 lines (245 loc) · 8.07 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
--
-- lua-TestLongString : <http://fperrad.github.com/lua-TestLongString/>
--
local pairs = pairs
local tostring = tostring
local type = type
local _G = _G
local math = math
local string = string
local tb = require 'Test.Builder':new()
_ENV = nil
local m = {}
-- Maximum string length displayed in diagnostics
m.max = 50
-- Amount of context provided when starting displaying a string in the middle
m.context = 10
-- should we show LCSS context ?
m.LCSS = true
-- what a end of line is
m.EOL = "\n"
local function display (str, offset)
local fmt = '"%s"'
if str:len() > m.max then
offset = offset or 1
if m.context then
offset = offset - m.context
if offset < 1 then
offset = 1
end
else
offset = 1
end
if offset == 1 then
fmt = '"%s"...'
else
fmt = '..."%s"...'
end
str = str:sub(offset, offset + m.max - 1)
end
str = str:gsub( '.',
function (ch)
local val = ch:byte()
if val < 32 or val > 127 then
return '\\' .. string.format( '%03d', val )
else
return ch
end
end )
return string.format( fmt, str )
end
local function common_prefix_length (str1, str2)
local i = 1
while true do
local c1 = str1:sub(i,i)
local c2 = str2:sub(i,i)
if not c1 or not c2 or c1 ~= c2 then
return i
end
i = i + 1
end
end
local function line_column (str, max)
local init = 1
local line = 1
while true do
local pos, posn = str:find(m.EOL, init)
if not pos or pos >= max then
break
end
init = posn + 1
line = line + 1
end
return line, max - init + 1
end
function m.is_string(got, expected, name)
if type(got) ~= 'string' then
tb:ok(false, name)
tb:diag("got value isn't a string : " .. tostring(got))
elseif type(expected) ~= 'string' then
tb:ok(false, name)
tb:diag("expected value isn't a string : " .. tostring(expected))
else
local pass = got == expected
tb:ok(pass, name)
if not pass then
local common_prefix = common_prefix_length(got, expected)
local line, column = line_column(got, common_prefix)
tb:diag(" got: " .. display(got, common_prefix)
.. "\n length: " .. got:len()
.. "\n expected: " .. display(expected, common_prefix)
.. "\n length: " .. expected:len()
.. "\n strings begin to differ at char " .. common_prefix .. " (line "
.. line .. " column "
.. column .. ")")
end
end
end
function m.is_string_nows(got, expected, name)
if type(got) ~= 'string' then
tb:ok(false, name)
tb:diag("got value isn't a string : " .. tostring(got))
elseif type(expected) ~= 'string' then
tb:ok(false, name)
tb:diag("expected value isn't a string : " .. tostring(expected))
else
local got_nows = got:gsub( "%s+", '' )
local expected_nows = expected:gsub( "%s+", '' )
local pass = got_nows == expected_nows
tb:ok(pass, name)
if not pass then
local common_prefix = common_prefix_length(got_nows, expected_nows)
tb:diag("after whitespace removal:"
.. "\n got: " .. display(got_nows, common_prefix)
.. "\n length: " .. got_nows:len()
.. "\n expected: " .. display(expected_nows, common_prefix)
.. "\n length: " .. expected_nows:len()
.. "\n strings begin to differ at char " .. common_prefix)
end
end
end
function m.like_string(got, pattern, name)
if type(got) ~= 'string' then
tb:ok(false, name)
tb:diag("got value isn't a string : " .. tostring(got))
elseif type(pattern) ~= 'string' then
tb:ok(false, name)
tb:diag("pattern isn't a string : " .. tostring(pattern))
else
local pass = got:match(pattern)
tb:ok(pass, name)
if not pass then
tb:diag(" got: " .. display(got)
.. "\n length: " .. got:len()
.. "\n doesn't match '" .. pattern .. "'")
end
end
end
function m.unlike_string(got, pattern, name)
if type(got) ~= 'string' then
tb:ok(false, name)
tb:diag("got value isn't a string : " .. tostring(got))
elseif type(pattern) ~= 'string' then
tb:ok(false, name)
tb:diag("pattern isn't a string : " .. tostring(pattern))
else
local pass = not got:match(pattern)
tb:ok(pass, name)
if not pass then
tb:diag(" got: " .. display(got)
.. "\n length: " .. got:len()
.. "\n matches '" .. pattern .. "'")
end
end
end
local function lcss (S, T)
local L = {}
local offset = 1
local length = 0
for i = 1, S:len() do
for j = 1, T:len() do
if S:byte(i) == T:byte(j) then
if i == 1 or j == 1 then
L[i] = L[i] or {}
L[i][j] = 1
else
L[i-1] = L[i-1] or {}
L[i] = L[i] or {}
L[i][j] = (L[i-1][j-1] or 0) + 1
end
if L[i][j] > length then
length = L[i][j]
offset = i - length + 1
end
end
end
end
return offset, length
end
function m.contains_string(str, substring, name)
if type(str) ~= 'string' then
tb:ok(false, name)
tb:diag("String to look in isn't a string")
elseif type(substring) ~= 'string' then
tb:ok(false, name)
tb:diag("String to look for isn't a string")
else
local pass = str:find(substring, 1, true)
tb:ok(pass, name)
if not pass then
tb:diag(" searched: " .. display(str)
.. "\n can't find: " .. display(substring))
if m.LCSS then
local off, len = lcss(str, substring)
local l = str:sub(off, off + len - 1)
tb:diag(" LCSS: " .. display(l))
if len < m.max then
local available = math.ceil((m.max - len) / 2)
local begin = off - 2 * available
if begin < 1 then
begin = off - available
if begin < 1 then
begin = 1
end
end
local ctx = str:sub(begin, begin + m.max)
tb:diag("LCSS context: " .. display(ctx))
end
end
end
end
end
function m.lacks_string(str, substring, name)
if type(str) ~= 'string' then
tb:ok(false, name)
tb:diag("String to look in isn't a string")
elseif type(substring) ~= 'string' then
tb:ok(false, name)
tb:diag("String to look for isn't a string")
else
local idx = str:find(substring, 1, true)
local pass = not idx
tb:ok(pass, name)
if not pass then
local line, column = line_column(str, idx)
tb:diag(" searched: " .. display(str)
.. "\n and found: " .. display(substring)
.. "\n at position: " .. idx .. " (line "
.. line .. " column "
.. column .. ")")
end
end
end
for k, v in pairs(m) do -- injection
if type(v) == 'function' then
_G[k] = v
end
end
m._VERSION = "0.2.0"
m._DESCRIPTION = "lua-TestLongString : an extension for testing long string"
m._COPYRIGHT = "Copyright (c) 2009-2012 Francois Perrad"
return m
--
-- This library is licensed under the terms of the MIT/X11 license,
-- like Lua itself.
--