diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-21 08:38:41 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-03-21 09:03:37 +0100 |
commit | 66292a6a85cf22cae31520ea4d08c76f544e338a (patch) | |
tree | 3a0c53434a4008359d8d7c9f4bffc93f32b7a9c4 /tests | |
parent | 2cc93b924a21030fcd6f5dc96f287bbf13f987f4 (diff) | |
download | fengari-66292a6a85cf22cae31520ea4d08c76f544e338a.tar.gz fengari-66292a6a85cf22cae31520ea4d08c76f544e338a.tar.bz2 fengari-66292a6a85cf22cae31520ea4d08c76f544e338a.zip |
string.gsub tests
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lstrlib.js | 189 |
1 files changed, 189 insertions, 0 deletions
diff --git a/tests/lstrlib.js b/tests/lstrlib.js index ce77bdc..12df173 100644 --- a/tests/lstrlib.js +++ b/tests/lstrlib.js @@ -677,4 +677,193 @@ test('string.gmatch', function (t) { "Lua", "Correct element(s) on the stack" ); +}); + + +test('string.gsub', function (t) { + let luaCode = ` + return string.gsub("hello world", "(%w+)", "%1 %1") + `, 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), + "hello hello world world", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (number)', function (t) { + let luaCode = ` + return string.gsub("hello world", "%w+", "%0 %0", 1) + `, 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), + "hello hello world", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (pattern)', function (t) { + let luaCode = ` + return string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") + `, 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), + "world hello Lua from", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); +}); + + +test('string.gsub (function)', function (t) { + let luaCode = ` + return string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) + return load(s)() + end) + `, 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), + "4+5 = 9", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 1, + "Correct element(s) on the stack" + ); +}); + + + +test('string.gsub (table)', function (t) { + let luaCode = ` + local t = {name="lua", version="5.3"} + return string.gsub("$name-$version.tar.gz", "%$(%w+)", t) + `, 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), + "lua-5.3.tar.gz", + "Correct element(s) on the stack" + ); + + t.strictEqual( + lapi.lua_tointeger(L, -1), + 2, + "Correct element(s) on the stack" + ); });
\ No newline at end of file |