forked from DFHack/dfhack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.lua
More file actions
168 lines (141 loc) · 5.63 KB
/
Copy pathstring.lua
File metadata and controls
168 lines (141 loc) · 5.63 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
-- tests string functions added by dfhack.lua
config.target = 'core'
function test.startswith()
expect.true_(('abcd'):startswith(''))
expect.true_(('abcd'):startswith('abc'))
expect.false_(('abcd'):startswith('bcd'))
expect.false_(('abcd'):startswith('abcde'))
expect.true_((''):startswith(''))
expect.false_((''):startswith('a'))
expect.false_(('str'):startswith('.'),
'ensure we match literals, not patterns')
end
function test.endswith()
expect.true_(('abcd'):endswith(''))
expect.true_(('abcd'):endswith('bcd'))
expect.false_(('abcd'):endswith('abc'))
expect.false_(('abcd'):endswith('zabcd'))
expect.true_((''):endswith(''))
expect.false_((''):endswith('a'))
expect.false_(('str'):endswith('.'),
'ensure we match literals, not patterns')
end
function test.split()
expect.table_eq({'hello','world'}, ('hello world'):split())
expect.table_eq({'hello','','world'}, ('hello world'):split())
expect.table_eq({'hello','world'}, ('hello world'):split(' +'))
expect.table_eq({'hello','world'}, ('hello.world'):split('.', true),
'ensure literal interpretation when plain is true')
expect.table_eq({'', '', '', ''}, ('abc'):split('.'),
'ensure pattern interpretation when plain is false')
-- we don't actually care what this returns, just that it does return
expect.true_(('hello world'):split('.*'), 'ensure no infinite loop')
expect.table_eq({'hello ', ' world'}, ('hello , world'):split(','),
'ensure spaces are kept when they are not the delimiter')
expect.table_eq({'hello'}, ('hello'):split(), 'no delimiter')
end
function test.trim()
expect.eq('hello', ('hello'):trim())
expect.eq('hello', (' hello'):trim())
expect.eq('hello', ('hello '):trim())
expect.eq('hello', (' hello '):trim())
expect.eq('', (''):trim())
expect.eq('', (' '):trim())
expect.eq('', (' \t \n \v '):trim())
expect.eq('hel lo', (' hel lo '):trim(), 'keep interior spaces')
expect.eq('hel \n lo', (' hel \n lo '):trim(),
'keep interior spaces across newlines')
end
function test.wrap()
expect.eq('', (''):wrap())
expect.eq('', (' '):wrap())
expect.eq('\n', (' '):wrap(3))
expect.eq('\n', (' '):wrap(3))
expect.eq('', (''):wrap(3, {keep_trailing_spaces=true}))
expect.eq(' ', (' '):wrap(3, {keep_trailing_spaces=true}))
expect.eq(' \n ', (' '):wrap(3, {keep_trailing_spaces=true}))
expect.eq(' \n \n ', (' '):wrap(3, {keep_trailing_spaces=true}))
expect.eq('hello world', ('hello world'):wrap(20))
expect.eq('hello world', ('hello world'):wrap(20))
expect.eq(' hello world', (' hello world '):wrap(20))
expect.eq(' hello world', (' hello world '):wrap(20))
expect.eq('hello world\nhow are you?',('hello world how are you?'):wrap(12))
expect.eq('hello\nworld', ('hello world'):wrap(5))
expect.eq('hello\nworld\n\n', ('hello world\n'):wrap(5))
expect.eq('hello\nworld', ('hello world'):wrap(5))
expect.eq('\nhello\nworld', (' hello world '):wrap(5))
expect.eq('hello\nworld', ('hello world'):wrap(8))
expect.eq('hel\nlo\nwor\nld', ('hello world'):wrap(3))
expect.eq('hel\nloo\nwor\nldo', ('helloo worldo'):wrap(3))
expect.eq('hel\nloo\n \nwor\nldo', ('helloo worldo'):wrap(3, {keep_trailing_spaces=true}))
expect.table_eq({'hel', 'lo', 'wor', 'ld'}, ('hello world'):wrap(3, {return_as_table=true}))
expect.error_match('expected width > 0', function() ('somestr'):wrap(0) end)
-- extended tests based on gui/journal
local journal_opts = {return_as_table=true, keep_trailing_spaces=true, keep_original_newlines=true}
expect.table_eq({
'this ',
'is a ',
'simple ',
'text'
}, ('this is a simple text'):wrap(7, journal_opts), 'simple text')
expect.table_eq({
'this ',
'is ',
'a ',
'simple ',
'text'
}, ('this is a simple text'):wrap(7, journal_opts), 'keep endline spaces')
expect.table_eq({
' this ',
'is a ',
'simple ',
'text'
}, (' this is a simple text'):wrap(7, journal_opts), 'keep text leading spaces')
expect.table_eq({
'this ',
'is a ',
'simple ',
'text '
}, ('this is a simple text '):wrap(7, journal_opts), 'keep text trailing spaces')
expect.table_eq({
'thiswor',
'distool',
'ong is ',
'a ',
'simple ',
'text'
}, ('thiswordistoolong is a simple text'):wrap(7, journal_opts),
'cut words longer than max width')
expect.table_eq({
'this ',
'is a ',
'sim\n',
'ple ',
'text\n',
''
}, ('this is a sim\nple text\n'):wrap(7, journal_opts),
'take into account existing new line')
expect.table_eq({
'this ',
'is a ',
'sim\n',
'\n',
'\n',
'ple ',
'text'
}, ('this is a sim\n\n\nple text'):wrap(7, journal_opts),
'take into account existing new lines')
end
function test.escape_pattern()
-- no change expected
expect.eq('', (''):escape_pattern())
expect.eq(' ', (' '):escape_pattern())
expect.eq('abc', ('abc'):escape_pattern())
expect.eq('a,b', ('a,b'):escape_pattern())
expect.eq('"a,b"', ('"a,b"'):escape_pattern())
-- excape regex chars
expect.eq('iz for me%?', ('iz for me?'):escape_pattern())
expect.eq('%.%*', ('.*'):escape_pattern())
expect.eq('%( %) %. %% %+ %- %* %? %[ %] %^ %$',
('( ) . % + - * ? [ ] ^ $'):escape_pattern())
end