Debian Codegraph @

debian/tests/interpreter

raw
1 #!/bin/sh
2
3 # This script is a smoke test for the basic functionality of the lua interpreter.
4
5 set -e
6
7 # Create a test lua file from a string of commands, run the lua interpreter on
8 # it, then compare the printed output to a given string.
9 #
10 # $1 - A string of lua commands to run that will be placed in test.lua
11 # $2 - The correct output that should be printed by the interpreter
12 test_lua_program() {
13 rm -f test.lua
14 printf "%s" "$1" > test.lua
15 test_output=$(lua test.lua)
16
17 if [ "$test_output" != "$2" ]; then
18 echo "Error: expected output of $2 but received $test_output"
19 exit 1
20 fi
21 }
22
23 # Check basic math and order of operations.
24 math_test="print(4 * (6 + 5) - 7)"
25 math_test_output="37"
26 test_lua_program "$math_test" "$math_test_output"
27
28 # Check variable assignment and string concatination.
29 var_test="x=10
30 y = 5
31 name = 'World!'
32 print('Hello ' .. name .. ' - ' .. x .. y)
33 print(x + y)"
34 var_test_output="Hello World! - 105
35 15"
36 test_lua_program "$var_test" "$var_test_output"
37
38 # Check recursion, function calls, loops, and branches.
39 recursion_test="
40 function print_dots(num)
41 for i=1,num do io.write('.') end
42 io.write('\n')
43 if num > 0 then print_dots(num - 1) end
44 end
45 print_dots(5)"
46 recursion_test_output=".....
47 ....
48 ...
49 ..
50 ."
51 test_lua_program "$recursion_test" "$recursion_test_output"
52
53 rm -f test.lua