]>
Commit | Line | Data |
---|---|---|
8a04c1e7 DL |
1 | |
2 | -- Positive testing | |
3 | ||
4535b611 | 4 | function foo(a, b) |
8a04c1e7 DL |
5 | a = a + 1 |
6 | b = b + 1 | |
4535b611 DL |
7 | return { |
8 | a = a, | |
9 | b = b, | |
10 | } | |
11 | end | |
12 | ||
8a04c1e7 DL |
13 | function bar(a, b) |
14 | a = a + 1 | |
15 | b = b + 1 | |
16 | c = 303 | |
17 | return { | |
18 | b = b, | |
19 | c = c, | |
20 | } | |
21 | end | |
22 | ||
4535b611 | 23 | function fact(n) |
8a04c1e7 DL |
24 | -- outer function must return a table |
25 | -- inner functions can be used to recurse or as helpers | |
4535b611 DL |
26 | function helper(m) |
27 | if m == 0 then | |
28 | return 1 | |
29 | else | |
30 | return m * helper(m - 1) | |
31 | end | |
32 | end | |
33 | return { | |
5090d724 | 34 | ans = helper(n) |
4535b611 DL |
35 | } |
36 | end | |
7948c5d2 | 37 | |
8a04c1e7 DL |
38 | -- Negative testing |
39 | ||
7948c5d2 DL |
40 | function bad_return1() |
41 | end | |
42 | ||
43 | function bad_return2() | |
8a04c1e7 | 44 | return 123 |
7948c5d2 DL |
45 | end |
46 | ||
47 | function bad_return3() | |
8a04c1e7 DL |
48 | return {} |
49 | end | |
50 | ||
51 | function bad_return4() | |
7948c5d2 DL |
52 | error("Something bad!") |
53 | end | |
54 |