--- /dev/null
+-- Test various cond api to verify consistency.\r
+-- string compared to string (eg. 'string1' == 'string2') compares by value\r
+-- string compared to number (eg. 'string' == 5) compares its string length\r
+-- string compared to any type with 'lt(<) gt(>) ge(>=) le(<=)' operators are compared to their legnth (even if both values are strings).\r
+-- number compared to number works just like you'd expect :)\r
+\r
+-- only print failed tests\r
+local FAILED_ONLY = true;\r
+\r
+local ops = {"==", "!=", ">=", "<=", ">", "<"};\r
+local tests = {\r
+ -- cmp value, expected results for each operator, MAPPINGS[idx] \r
+ { {"1", "1"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal integers' },\r
+ { {"1", "2"}, {2, 1, 2, 1, 2, 1}, 'test 2 non equal integers' },\r
+ { {"1.000001", "1.000001"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal float' },\r
+ { {"1.000001", "1.000002"}, {2, 1, 2, 1, 2, 1}, 'test 2 non equal float' },\r
+ { {"'hello'", "'hello'"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal quoted strings' },\r
+ { {"hello", "hello"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal unquoted strings' },\r
+ { {"hello", "HELLO"}, {2, 1, 1, 1, 2, 2}, 'test 2 non equal unquoted strings' },\r
+ { {"hello", "5"}, {1, 2, 1, 1, 2, 2}, 'test lenght of unquoted string with equal number' },\r
+ { {"'hello'", "5"}, {1, 2, 1, 1, 2, 2}, 'test length of quoted string with equal number' },\r
+ { {"' hello'", "5"}, {2, 1, 1, 2, 1, 2}, 'test length of quoted string includes preceding space' },\r
+ { {" hello", "5"}, {1, 2, 1, 1, 2, 2}, 'test length of unquoted string excludes preceding space' },\r
+ { {"'hello'", "6"}, {2, 1, 2, 1, 2, 1}, 'test length of quoted string is against non equal number' },\r
+ { {"'01'", "01"}, {2, 1, 1, 2, 1, 2}, 'test number quoted (as string) against number' },\r
+ { {"''", "''"}, {1, 2, 1, 1, 2, 2}, 'test quoted empty strings' },\r
+ { {"' '", "''"}, {2, 1, 1, 2, 1, 2}, 'test quoted space against empty string' },\r
+ { {"", " "}, {3, 3, 3, 3, 3, 3}, 'test unquoted empty values returns ERR' },\r
+ \r
+ { {"'Isn\\'t it \"great\"?!\\t'", "'Isn\\'t it \"great\"?!\\t'"}, {1, 2, 1, 1, 2, 2}, 'test quoted string with special escaped chars' },\r
+ { {"'Isn't it \"great\"?!\\t'", "'Isn't it \"great\"?!\\t'"}, {3, 3, 3, 3, 3, 3}, 'test quoted string with unescaped single quote returns ERR' },\r
+};\r
+\r
+stream:write("Testing cond api\n");\r
+\r
+local commands = {\r
+ -- command, description, truth val, false val, err val\r
+ {" ? true : false", "command with spaces", {"true", "false", "-ERR"}},\r
+ {" ? true:false", "command without spaces", {"true", "false", "-ERR"}},\r
+ {" ? true :", "command with missing ternary false value", {"true", "", "-ERR"}},\r
+ {"?true:false", "command with no spaces between values", {"-ERR", "-ERR", "-ERR"}},\r
+}\r
+\r
+local num_tests=0;\r
+local num_passed=0;\r
+\r
+local api = freeswitch.API();\r
+\r
+-- do for each command\r
+for _, cmd in pairs(commands) do\r
+ for ti, test in pairs(tests) do\r
+ if (not FAILED_ONLY) then\r
+ stream:write(string.format("\nTesting #[%d]: `%s` (%s)\n", ti, test[3], cmd[2]));\r
+ end\r
+ for i , op in pairs(ops) do\r
+ command = "cond " .. test[1][1] .. " " .. op .. " " .. test[1][2] .. cmd[1];\r
+ reply = api:executeString(command);\r
+ expected = cmd[3][test[2][i]];\r
+ if (reply ~= nil) then\r
+ passed = (reply == expected);\r
+ if (passed) then\r
+ num_passed=num_passed+1;\r
+ pass_text = "PASSED";\r
+ else\r
+ pass_text = "FAILED"\r
+ end\r
+ -- print runned test\r
+ if (not FAILED_ONLY or not passed) then\r
+ stream:write(string.format("%s:\tTest #[%d]: [%s (%s)] \t--- expected: [%s], actual: [%s]\n", pass_text, ti, command, cmd[2], expected, reply));\r
+ end\r
+ else\r
+ stream:write("FAILED!\t" .. command .. "\n");\r
+ end\r
+ num_tests=num_tests+1;\r
+ end\r
+ end\r
+end\r
+\r
+stream:write(string.format("\nRAN: [%d], PASSED: [%d], FAILED: [%d]", num_tests, num_passed, num_tests-num_passed));\r