-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathStringLibTests.cs
More file actions
288 lines (247 loc) · 6.25 KB
/
Copy pathStringLibTests.cs
File metadata and controls
288 lines (247 loc) · 6.25 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace MoonSharp.Interpreter.Tests.EndToEnd
{
[TestFixture]
public class StringLibTests
{
[Test]
public void String_GMatch_1()
{
string script = @"
t = '';
for word in string.gmatch('Hello Lua user', '%a+') do
t = t .. word;
end
return (t);
";
DynValue res = Script.RunString(script);
Assert.AreEqual(DataType.String, res.Type);
Assert.AreEqual("HelloLuauser", res.String);
}
[Test]
public void String_GMatch_2()
{
string script = @"
s = 'Ррррр Нннннн Сссссс'
words = {}
for word in s:gmatch('%w+') do table.insert(words, word) end
return words
";
DynValue res = Script.RunString(script);
Assert.AreEqual(3, res.Table.Length);
Assert.AreEqual("Ррррр", res.Table.Get(1).String);
Assert.AreEqual("Нннннн", res.Table.Get(2).String);
Assert.AreEqual("Сссссс", res.Table.Get(3).String);
}
[Test]
public void String_Find_1()
{
string script = @"return string.find('Hello Lua user', 'Lua');";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, 7, 9);
}
[Test]
public void String_Find_2()
{
string script = @"return string.find('Hello Lua user', 'banana');";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, null);
}
[Test]
public void String_Find_3()
{
string script = @"return string.find('Hello Lua user', 'Lua', 1);";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, 7, 9);
}
[Test]
public void String_Find_4()
{
string script = @"return string.find('Hello Lua user', 'Lua', 8);";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, null);
}
[Test]
public void String_Find_5()
{
string script = @"return string.find('Hello Lua user', 'e', -5);";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, 13, 13);
}
[Test]
public void String_Find_6()
{
string script = @"return string.find('Hello Lua user', '%su');";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, 10, 11);
}
[Test]
public void String_Find_7()
{
string script = @"return string.find('Hello Lua user', '%su', 1);";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, 10, 11);
}
[Test]
public void String_Find_8()
{
string script = @"return string.find('Hello Lua user', '%su', 1, true);";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, null);
}
[Test]
public void String_Find_9()
{
string script = @"
s = 'Deadline is 30/05/1999, firm'
date = '%d%d/%d%d/%d%d%d%d';
return s:sub(s:find(date));
";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, "30/05/1999");
}
[Test]
public void String_Find_10()
{
string script = @"
s = 'Deadline is 30/05/1999, firm'
date = '%f[%S]%d%d/%d%d/%d%d%d%d';
return s:sub(s:find(date));
";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, "30/05/1999");
}
[Test]
public void String_Find_11()
{
string script = @"
s = 'Deadline is 30/05/1999, firm'
date = '%f[%s]%d%d/%d%d/%d%d%d%d';
return s:find(date);
";
DynValue res = Script.RunString(script);
Assert.IsTrue(res.IsNil());
}
[Test]
public void String_Format_1()
{
string script = @"
d = 5; m = 11; y = 1990
return string.format('%02d/%02d/%04d', d, m, y)
";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, "05/11/1990");
}
[Test]
public void String_GSub_1()
{
string script = @"
s = string.gsub('hello world', '(%w+)', '%1 %1')
return s, s == 'hello hello world world'
";
DynValue res = Script.RunString(script);
Assert.AreEqual(res.Tuple[0].String, "hello hello world world");
Assert.AreEqual(res.Tuple[1].Boolean, true);
}
[Test]
public void PrintTest1()
{
string script = @"
print('ciao', 1);
";
string printed = null;
Script S = new Script();
DynValue main = S.LoadString(script);
S.Options.DebugPrint = s =>
{
printed = s;
};
S.Call(main);
Assert.AreEqual("ciao\t1", printed);
}
[Test]
public void PrintTest2()
{
string script = @"
t = {};
m = {};
function m.__tostring()
return 'ciao';
end
setmetatable(t, m);
print(t, 1);
";
string printed = null;
Script S = new Script();
DynValue main = S.LoadString(script);
S.Options.DebugPrint = s =>
{
printed = s;
};
S.Call(main);
Assert.AreEqual("ciao\t1", printed);
}
[Test]
public void ToStringTest()
{
string script = @"
t = {}
mt = {}
a = nil
function mt.__tostring () a = 'yup' end
setmetatable(t, mt)
return tostring(t), a;
";
DynValue res = Script.RunString(script);
Utils.DynAssert(res, DataType.Void, "yup");
}
[Test]
[ExpectedException(typeof(ScriptRuntimeException))]
public void String_GSub_2()
{
string script = @"
string.gsub('hello world', '%w+', '%e')
";
DynValue res = Script.RunString(script);
}
[Test]
public void String_GSub_3()
{
Script S = new Script();
S.Globals["a"] = @" 'C:\temp\test.lua:68: bad argument #1 to 'date' (invalid conversion specifier '%Ja')'
doesn't match '^[^:]+:%d+: bad argument #1 to 'date' %(invalid conversion specifier '%%Ja'%)'";
string script = @"
string.gsub(a, '\n', '\n #')
";
DynValue res = S.DoString(script);
}
[Test]
public void String_GSub_4()
{
string script = @"
return string.gsub('Ррррр Нннннн Сссссс','%S+', 'Z')
";
DynValue res = Script.RunString(script);
Assert.AreEqual("Z Z Z", res.Tuple[0].String);
}
[Test]
public void String_Match_1()
{
string s = @"test.lua:185: field 'day' missing in date table";
string p = @"^[^:]+:%d+: field 'day' missing in date table";
TestMatch(s, p, true);
}
private void TestMatch(string s, string p, bool expected)
{
Script S = new Script(CoreModules.String);
S.Globals["s"] = s;
S.Globals["p"] = p;
DynValue res = S.DoString("return string.match(s, p)");
Assert.AreEqual(expected, !res.IsNil());
}
}
}