aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Giannangeli <giann@users.noreply.github.com>2017-03-21 09:09:16 +0100
committerGitHub <noreply@github.com>2017-03-21 09:09:16 +0100
commit932875c87ab3f2ed2b676a98f273392661535f65 (patch)
tree505f463ecdd0b4be4f0ce3e6446183774d406783 /tests
parent9ac1d380dd1da894c6317845ea3234124663714b (diff)
parent6bf508f234654b1b0c7afa04797601d14c150934 (diff)
downloadfengari-932875c87ab3f2ed2b676a98f273392661535f65.tar.gz
fengari-932875c87ab3f2ed2b676a98f273392661535f65.tar.bz2
fengari-932875c87ab3f2ed2b676a98f273392661535f65.zip
Merge pull request #8 from giann/feature/match
Feature/match
Diffstat (limited to 'tests')
-rw-r--r--tests/lstrlib.js368
1 files changed, 368 insertions, 0 deletions
diff --git a/tests/lstrlib.js b/tests/lstrlib.js
index 6364c7f..12df173 100644
--- a/tests/lstrlib.js
+++ b/tests/lstrlib.js
@@ -498,4 +498,372 @@ 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"
+ );
+});
+
+
+test('string.gmatch', function (t) {
+ let luaCode = `
+ local s = "hello world from Lua"
+ local t = {}
+
+ for w in string.gmatch(s, "%a+") do
+ table.insert(t, w)
+ end
+
+ return table.unpack(t)
+ `, 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_tostring(L, -4),
+ "hello",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -3),
+ "world",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -2),
+ "from",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "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