aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-16 15:37:09 +0100
committerBenoit Giannangeli <benoit.giannangeli@boursorama.fr>2017-03-16 15:37:09 +0100
commit91924d0b77999c9f1fd700ec60fc31c38f7d27b4 (patch)
treed70d4d2fd997ab36ce7b2dddd5db3aa9ad8c9b6e /tests
parent4161f6756dadef6cd5a5c2e1e75804122e892949 (diff)
downloadfengari-91924d0b77999c9f1fd700ec60fc31c38f7d27b4.tar.gz
fengari-91924d0b77999c9f1fd700ec60fc31c38f7d27b4.tar.bz2
fengari-91924d0b77999c9f1fd700ec60fc31c38f7d27b4.zip
string.sub
Diffstat (limited to 'tests')
-rw-r--r--tests/lstrlib.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/tests/lstrlib.js b/tests/lstrlib.js
index 0475a42..4df4283 100644
--- a/tests/lstrlib.js
+++ b/tests/lstrlib.js
@@ -284,6 +284,7 @@ test('string.format', function (t) {
);
});
+
test('string.format', function (t) {
let luaCode = `
return string.format("%q", 'a string with "quotes" and \\n new line')
@@ -312,4 +313,105 @@ test('string.format', function (t) {
'"a string with \\"quotes\\" and \\\n new line"',
"Correct element(s) on the stack"
);
+});
+
+
+test('string.sub', function (t) {
+ let luaCode = `
+ return string.sub("123456789",2,4), -- "234"
+ string.sub("123456789",7), -- "789"
+ string.sub("123456789",7,6), -- ""
+ string.sub("123456789",7,7), -- "7"
+ string.sub("123456789",0,0), -- ""
+ string.sub("123456789",-10,10), -- "123456789"
+ string.sub("123456789",1,9), -- "123456789"
+ string.sub("123456789",-10,-20), -- ""
+ string.sub("123456789",-1), -- "9"
+ string.sub("123456789",-4), -- "6789"
+ string.sub("123456789",-6, -4) -- "456"
+ `, L;
+
+ t.plan(13);
+
+ 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, -11),
+ "234",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -10),
+ "789",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -9),
+ "",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -8),
+ "7",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -7),
+ "",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -6),
+ "123456789",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -5),
+ "123456789",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -4),
+ "",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -3),
+ "9",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -2),
+ "6789",
+ "Correct element(s) on the stack"
+ );
+
+ t.strictEqual(
+ lapi.lua_tostring(L, -1),
+ "456",
+ "Correct element(s) on the stack"
+ );
}); \ No newline at end of file