Current state of mal for Clojure West lighting talk.
[jackhill/mal.git] / bash / tests / reader.sh
1 #!/bin/bash
2
3 INTERACTIVE=
4
5 source tests/common.sh
6 source reader.sh
7
8 echo "Testing read of constants/strings"
9 assert_eq $LINENO 2 "READ_STR '2'; number_pr_str \$r"
10 assert_eq $LINENO 12345 "READ_STR '12345'; number_pr_str \$r"
11 assert_eq $LINENO 12345 "READ_STR '12345 \"abc\"'; number_pr_str \$r"
12 assert_eq $LINENO 'abc' "READ_STR '\"abc\"'; number_pr_str \$r"
13 assert_eq $LINENO 'a string (with parens)' "READ_STR '\"a string (with parens)\"'; number_pr_str \$r"
14
15 echo "Testing read of symbols"
16 assert $LINENO "READ_STR 'abc'; _symbol? \$r"
17 assert_eq $LINENO 'abc' "READ_STR 'abc'; symbol_pr_str \$r"
18 assert_eq $LINENO '.' "READ_STR '.'; symbol_pr_str \$r"
19
20 raw_val () {
21 r="${ANON["${1}"]}"
22 }
23
24 echo "Testing READ_STR of strings"
25 assert_eq $LINENO 'a string' "READ_STR '\"a string\"'; raw_val \$r"
26 assert_eq $LINENO 'a string (with parens)' "READ_STR '\"a string (with parens)\"'; raw_val \$r"
27 assert_eq $LINENO 'a string' "READ_STR '\"a string\"()'; raw_val \$r"
28 assert_eq $LINENO 'a string' "READ_STR '\"a string\"123'; raw_val \$r"
29 assert_eq $LINENO 'a string' "READ_STR '\"a string\"abc'; raw_val \$r"
30 assert_eq $LINENO '' "READ_STR '\"\"'; raw_val \$r"
31 assert_eq $LINENO 'abc ' "READ_STR '\"abc \"'; raw_val \$r"
32 assert_eq $LINENO ' abc' "READ_STR '\" abc\"'; raw_val \$r"
33 assert_eq $LINENO '$abc' "READ_STR '\"\$abc\"'; raw_val \$r"
34 assert_eq $LINENO 'abc$()' "READ_STR '\"abc\$()\"'; raw_val \$r"
35 # TODO: fix parsing of escaped characters
36 #assert_eq $LINENO '"xyz"' "READ_STR '\"\\\"xyz\\\"\"'; raw_val \$r"
37
38 echo "Testing READ_STR of lists"
39 assert_eq $LINENO 2 "READ_STR '(2 3)'; _count \$r"
40 assert_eq $LINENO 2 "READ_STR '(2 3)'; first \$r; number_pr_str \$r"
41 assert_eq $LINENO 3 "READ_STR '(2 3)'; rest \$r; first \$r; number_pr_str \$r"
42
43 READ_STR "(+ 1 2 \"str1\" \"string (with parens) and 'single quotes'\")"
44 L="${r}"
45 assert_eq $LINENO 5 "_count \$r"
46 assert_eq $LINENO 'str1' "_nth ${L} 3; raw_val \$r"
47 assert_eq $LINENO "string (with parens) and 'single quotes'" "_nth ${L} 4; raw_val \$r"
48 assert_eq $LINENO '(2 3)' "READ_STR '(2 3)'; list_pr_str \$r"
49 assert_eq $LINENO '(2 3 "string (with parens)")' "READ_STR '(2 3 \"string (with parens)\")'; list_pr_str \$r yes"
50
51
52 echo "Testing READ_STR of vectors"
53 assert_eq $LINENO 2 "READ_STR '[2 3]'; _count \$r"
54 assert_eq $LINENO 2 "READ_STR '[2 3]'; first \$r; number_pr_str \$r"
55 assert_eq $LINENO 3 "READ_STR '[2 3]'; rest \$r; first \$r; number_pr_str \$r"
56
57 READ_STR "[+ 1 2 \"str1\" \"string (with parens) and 'single quotes'\"]"
58 L="${r}"
59 assert_eq $LINENO 5 "_count \$r"
60 assert_eq $LINENO 'str1' "_nth ${L} 3; raw_val \$r"
61 assert_eq $LINENO "string (with parens) and 'single quotes'" "_nth ${L} 4; raw_val \$r"
62 assert_eq $LINENO '[2 3]' "READ_STR '[2 3]'; vector_pr_str \$r yes"
63 assert_eq $LINENO '[2 3 "string (with parens)"]' "READ_STR '[2 3 \"string (with parens)\"]'; vector_pr_str \$r yes"
64
65
66 echo "Testing READ_STR of quote/quasiquote"
67 assert_eq $LINENO 'quote' "READ_STR \"'1\"; _nth \$r 0; raw_val \$r"
68 assert_eq $LINENO 1 "READ_STR \"'1\"; _nth \$r 1; raw_val \$r"
69 assert_eq $LINENO 'quote' "READ_STR \"'(1 2 3)\"; _nth \$r 0; raw_val \$r"
70 assert_eq $LINENO 3 "READ_STR \"'(1 2 3)\"; _nth \$r 1; _nth \$r 2; raw_val \$r"
71
72 assert_eq $LINENO 'quasiquote' "READ_STR \"\\\`1\"; _nth \$r 0; raw_val \$r"
73 assert_eq $LINENO 1 "READ_STR \"\\\`1\"; _nth \$r 1; raw_val \$r"
74 assert_eq $LINENO 'quasiquote' "READ_STR \"\\\`(1 2 3)\"; _nth \$r 0; raw_val \$r"
75 assert_eq $LINENO 3 "READ_STR \"\\\`(1 2 3)\"; _nth \$r 1; _nth \$r 2; raw_val \$r"
76
77 assert_eq $LINENO 'unquote' "READ_STR \"~1\"; _nth \$r 0; raw_val \$r"
78 assert_eq $LINENO 1 "READ_STR \"~1\"; _nth \$r 1; raw_val \$r"
79 assert_eq $LINENO 'unquote' "READ_STR \"~(1 2 3)\"; _nth \$r 0; raw_val \$r"
80 assert_eq $LINENO 3 "READ_STR \"~(1 2 3)\"; _nth \$r 1; _nth \$r 2; raw_val \$r"
81
82 assert_eq $LINENO 'splice-unquote' "READ_STR \"~@1\"; _nth \$r 0; raw_val \$r"
83 assert_eq $LINENO 1 "READ_STR \"~@1\"; _nth \$r 1; raw_val \$r"
84 assert_eq $LINENO 'splice-unquote' "READ_STR \"~@(1 2 3)\"; _nth \$r 0; raw_val \$r"
85 assert_eq $LINENO 3 "READ_STR \"~@(1 2 3)\"; _nth \$r 1; _nth \$r 2; raw_val \$r"
86
87
88 echo "All tests completed"