proper escape sequences in strings
[clinton/parenscript.git] / t / test.lisp
1 (in-package :js-test)
2
3 ;; Testcases for parenscript
4
5 (defun trim-whitespace(str)
6 (string-trim '(#\Space #\Tab #\Newline) str))
7
8 (defun same-space-between-statements(code)
9 (cl-ppcre:regex-replace-all "\\s*;\\s*" code (concatenate 'string (list #\; #\Newline))))
10
11 (defun no-indentation(code)
12 (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "^\\s*" :multi-line-mode t) code ""))
13
14 (defun no-trailing-spaces(code)
15 (cl-ppcre:regex-replace-all (cl-ppcre:create-scanner "\\s*$" :multi-line-mode t) code ""))
16
17 (defun normalize-js-code(str)
18 (trim-whitespace (no-indentation (no-trailing-spaces (same-space-between-statements str)))))
19
20 (defmacro test-ps-js (testname parenscript javascript)
21 `(test ,testname ()
22 (setf js::*var-counter* 0)
23 ;; is-macro expands its argument again when reporting failures, so
24 ;; the reported temporary js-variables get wrong if we don't evalute first.
25 (let ((generated-code (js-to-string ',parenscript))
26 (js-code ,javascript))
27 (is (string= (normalize-js-code generated-code)
28 (normalize-js-code js-code))))))
29
30 (defun run-tests()
31 (format t "Running reference tests:~&")
32 (run! 'ref-tests)
33 (format t "Running other tests:~&")
34 (run! 'ps-tests))
35
36 ;;---------------------------------------------------------------------------
37 (def-suite ps-tests)
38 (in-suite ps-tests)
39
40 ;; A problem with long nested operator, when the statement spanned several rows
41 ;; the rows would not be joined together correctly.
42 (test-ps-js bug-dwim-join
43 (alert (html ((:div :id 777
44 :style (css-inline :border "1pxsssssssssss"
45 :font-size "x-small"
46 :height (* 2 200)
47 :width (* 2 300))))))
48 "alert
49 ('<div id=\"777\" style=\"'
50 + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
51 + 2 * 300)
52 + '\"></div>')") ;";This line should start with a plus character.
53
54
55 (test-ps-js simple-slot-value
56 (let ((foo (create :a 1)))
57 (alert (slot-value foo 'a)))
58 "{
59 var foo = { a : 1 };
60 alert(foo.a);
61 }")
62
63 (test-ps-js buggy-slot-value
64 (let ((foo (create :a 1))
65 (slot-name "a"))
66 (alert (slot-value foo slot-name)))
67 "{
68 var foo = { a : 1 };
69 var slotName = 'a';
70 alert(foo[slotName]);
71 }"); Last line was alert(foo.slotName) before bug-fix.
72
73 (test-ps-js buggy-slot-value-two
74 (slot-value foo (get-slot-name))
75 "foo[getSlotName()]")
76
77 (test-ps-js old-case-is-now-switch
78 ;; Switch was "case" before, but that was very non-lispish.
79 ;; For example, this code makes three messages and not one
80 ;; which may have been expected. This is because a switch
81 ;; statment must have a break statement for it to return
82 ;; after the alert. Otherwise it continues on the next
83 ;; clause.
84 (switch (aref blorg i)
85 (1 (alert "one"))
86 (2 (alert "two"))
87 (default (alert "default clause")))
88 "switch (blorg[i]) {
89 case 1: alert('one');
90 case 2: alert('two');
91 default: alert('default clause');
92 }")
93
94 (test-ps-js lisp-like-case
95 (case (aref blorg i)
96 (1 (alert "one"))
97 (2 (alert "two"))
98 (default (alert "default clause")))
99 "switch (blorg[i]) {
100 case 1:
101 alert('one');
102 break;
103 case 2:
104 alert('two');
105 break;
106 default: alert('default clause');
107 }")
108
109
110 (test-ps-js even-lispier-case
111 (case (aref blorg i)
112 ((1 2) (alert "Below three"))
113 (3 (alert "Three"))
114 (t (alert "Something else")))
115 "switch (blorg[i]) {
116 case 1: ;
117 case 2:
118 alert('Below three');
119 break;
120 case 3:
121 alert('Three');
122 break;
123 default: alert('Something else');
124 }")
125
126 (test-ps-js otherwise-case
127 (case (aref blorg i)
128 (1 (alert "one"))
129 (otherwise (alert "default clause")))
130 "switch (blorg[i]) {
131 case 1:
132 alert('one');
133 break;
134 default: alert('default clause');
135 }")
136
137 (test escape-sequences-in-string
138 (let ((escapes `((#\\ . #\\)
139 (#\b . #\Backspace)
140 (#\f . #\Form)
141 ("u000B" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
142 (#\n . #\Newline)
143 (#\r . #\Return)
144 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
145 (#\t . #\Tab)
146 ("u001F" . ,(code-char #x001f));; character below 32
147 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
148 ("uABCD" . ,(code-char #xabcd)))));; Really above ascii.
149 (loop for (js-escape . lisp-char) in escapes
150 for generated = (js-to-string `(let ((x , (format nil "hello~ahi" lisp-char)))))
151 for wanted = (format nil "{
152 var x = 'hello\\~ahi';
153 }" js-escape)
154 do (is (string= generated wanted)))))