summaryrefslogtreecommitdiff
path: root/tests/lstrlib.js
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-20 14:21:21 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-20 15:53:03 +0100
commit645437b8e0846635f886520bb679a79ec1849c09 (patch)
treefc62ebbf1305c9bc2900e48363bea44eda6d4c6b /tests/lstrlib.js
parent16612bb93644e83afde482b75725fe1ce902d02a (diff)
downloadfengari-645437b8e0846635f886520bb679a79ec1849c09.tar.gz
fengari-645437b8e0846635f886520bb679a79ec1849c09.tar.bz2
fengari-645437b8e0846635f886520bb679a79ec1849c09.zip
string.match/find test
Diffstat (limited to 'tests/lstrlib.js')
-rw-r--r--tests/lstrlib.js123
1 files changed, 123 insertions, 0 deletions
diff --git a/tests/lstrlib.js b/tests/lstrlib.js
index 6364c7f..aac3851 100644
--- a/tests/lstrlib.js
+++ b/tests/lstrlib.js
@@ -498,4 +498,127 @@ test('string.pack/unpack/packsize', function (t) {
lapi.lua_toboolean(L, -1),
"Correct element(s) on the stack"
);
+});
+
+
+test('string.find without pattern', function (t) {
+ let luaCode = `
+ return string.find("hello to you", " to ")
+ `, L;
+
+ t.plan(4);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, luaCode);
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tointeger(L, -2),
+ 6,
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tointeger(L, -1),
+ 9,
+ "Correct element(s) on the stack"
+ );
+});
+
+
+test('string.match', function (t) {
+ let luaCode = `
+ return string.match("foo: 123 bar: 456", "(%a+):%s*(%d+)")
+ `, L;
+
+ t.plan(4);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, luaCode);
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -2),
+ "foo",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "123",
+ "Correct element(s) on the stack"
+ );
+});
+
+
+test('string.find', function (t) {
+ let luaCode = `
+ return string.find("foo: 123 bar: 456", "(%a+):%s*(%d+)")
+ `, L;
+
+ t.plan(6);
+
+ t.doesNotThrow(function () {
+
+ L = lauxlib.luaL_newstate();
+
+ linit.luaL_openlibs(L);
+
+ lauxlib.luaL_loadstring(L, luaCode);
+
+ }, "Lua program loaded without error");
+
+ t.doesNotThrow(function () {
+
+ lapi.lua_call(L, 0, -1);
+
+ }, "Lua program ran without error");
+
+ t.strictEqual(
+ lapi.lua_tointeger(L, -4),
+ 1,
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tointeger(L, -3),
+ 8,
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -2),
+ "foo",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "123",
+ "Correct element(s) on the stack"
+ );
}); \ No newline at end of file