2
0

test_cond_api.lua 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. -- Test various cond api to verify consistency.
  2. -- string compared to string (eg. 'string1' == 'string2') compares by value
  3. -- string compared to number (eg. 'string' == 5) compares its string length
  4. -- string compared to any type with 'lt(<) gt(>) ge(>=) le(<=)' operators are compared to their legnth (even if both values are strings).
  5. -- number compared to number works just like you'd expect :)
  6. -- only print failed tests
  7. local FAILED_ONLY = true;
  8. local ops = {"==", "!=", ">=", "<=", ">", "<"};
  9. local tests = {
  10. -- cmp value, expected results for each operator, MAPPINGS[idx]
  11. { {"1", "1"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal integers' },
  12. { {"1", "2"}, {2, 1, 2, 1, 2, 1}, 'test 2 non equal integers' },
  13. { {"1.000001", "1.000001"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal float' },
  14. { {"1.000001", "1.000002"}, {2, 1, 2, 1, 2, 1}, 'test 2 non equal float' },
  15. { {"'hello'", "'hello'"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal quoted strings' },
  16. { {"hello", "hello"}, {1, 2, 1, 1, 2, 2}, 'test 2 equal unquoted strings' },
  17. { {"hello", "HELLO"}, {2, 1, 1, 1, 2, 2}, 'test 2 non equal unquoted strings' },
  18. { {"hello", "5"}, {1, 2, 1, 1, 2, 2}, 'test lenght of unquoted string with equal number' },
  19. { {"'hello'", "5"}, {1, 2, 1, 1, 2, 2}, 'test length of quoted string with equal number' },
  20. { {"' hello'", "5"}, {2, 1, 1, 2, 1, 2}, 'test length of quoted string includes preceding space' },
  21. { {" hello", "5"}, {1, 2, 1, 1, 2, 2}, 'test length of unquoted string excludes preceding space' },
  22. { {"'hello'", "6"}, {2, 1, 2, 1, 2, 1}, 'test length of quoted string is against non equal number' },
  23. { {"'01'", "01"}, {2, 1, 1, 2, 1, 2}, 'test number quoted (as string) against number' },
  24. { {"''", "''"}, {1, 2, 1, 1, 2, 2}, 'test quoted empty strings' },
  25. { {"' '", "''"}, {2, 1, 1, 2, 1, 2}, 'test quoted space against empty string' },
  26. { {"", " "}, {3, 3, 3, 3, 3, 3}, 'test unquoted empty values returns ERR' },
  27. { {"'Isn\\'t it \"great\"?!\\t'", "'Isn\\'t it \"great\"?!\\t'"}, {1, 2, 1, 1, 2, 2}, 'test quoted string with special escaped chars' },
  28. { {"'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' },
  29. };
  30. stream:write("Testing cond api\n");
  31. local commands = {
  32. -- command, description, truth val, false val, err val
  33. {" ? true : false", "command with spaces", {"true", "false", "-ERR"}},
  34. {" ? true:false", "command without spaces", {"true", "false", "-ERR"}},
  35. {" ? true :", "command with missing ternary false value", {"true", "", "-ERR"}},
  36. {"?true:false", "command with no spaces between values", {"-ERR", "-ERR", "-ERR"}},
  37. }
  38. local num_tests=0;
  39. local num_passed=0;
  40. local api = freeswitch.API();
  41. -- do for each command
  42. for _, cmd in pairs(commands) do
  43. for ti, test in pairs(tests) do
  44. if (not FAILED_ONLY) then
  45. stream:write(string.format("\nTesting #[%d]: `%s` (%s)\n", ti, test[3], cmd[2]));
  46. end
  47. for i , op in pairs(ops) do
  48. command = "cond " .. test[1][1] .. " " .. op .. " " .. test[1][2] .. cmd[1];
  49. reply = api:executeString(command);
  50. expected = cmd[3][test[2][i]];
  51. if (reply ~= nil) then
  52. passed = (reply == expected);
  53. if (passed) then
  54. num_passed=num_passed+1;
  55. pass_text = "PASSED";
  56. else
  57. pass_text = "FAILED"
  58. end
  59. -- print runned test
  60. if (not FAILED_ONLY or not passed) then
  61. stream:write(string.format("%s:\tTest #[%d]: [%s (%s)] \t--- expected: [%s], actual: [%s]\n", pass_text, ti, command, cmd[2], expected, reply));
  62. end
  63. else
  64. stream:write("FAILED!\t" .. command .. "\n");
  65. end
  66. num_tests=num_tests+1;
  67. end
  68. end
  69. end
  70. stream:write(string.format("\nRAN: [%d], PASSED: [%d], FAILED: [%d]", num_tests, num_passed, num_tests-num_passed));