bugfix commutative plus and minus
[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 (test-ps-js plus-is-not-commutative
41 (setf x (+ "before" x "after"))
42 "x = 'before' + x + 'after'")
43
44 (test-ps-js plus-works-if-first
45 (setf x (+ x "middle" "after"))
46 "x += 'middle' + 'after'")
47
48 (test-ps-js setf-side-effects
49 (progn
50 (let ((x 10))
51 (defun side-effect()
52 (setf x 4)
53 (return 3))
54 (setf x (+ 2 (side-effect) x 5))))
55 "
56 var x = 10;
57 function sideEffect() {
58 x = 4;
59 return 3;
60 };
61 x = 2 + sideEffect() + x + 5;")
62 ;; Parenscript used to optimize to much:
63 ;; var x = 10;
64 ;; function sideEffect() {
65 ;; x = 4;
66 ;; return 3;
67 ;; };
68 ;; x += 2 + sideEffect() + 5;
69 ;;
70 ;; Which is 20, not 14
71
72
73 (test-ps-js dot-notation-bug
74 (.match (+ "" x) "foo")
75 "('' + x).match('foo')")
76
77 ;; A problem with long nested operator, when the statement spanned several rows
78 ;; the rows would not be joined together correctly.
79 (test-ps-js bug-dwim-join
80 (alert (html ((:div :id 777
81 :style (css-inline :border "1pxsssssssssss"
82 :font-size "x-small"
83 :height (* 2 200)
84 :width (* 2 300))))))
85 "alert
86 ('<div id=\"777\" style=\"'
87 + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
88 + 2 * 300)
89 + '\"></div>')") ;";This line should start with a plus character.
90
91
92 (test-ps-js simple-slot-value
93 (let ((foo (create :a 1)))
94 (alert (slot-value foo 'a)))
95 "{
96 var foo = { a : 1 };
97 alert(foo.a);
98 }")
99
100 (test-ps-js buggy-slot-value
101 (let ((foo (create :a 1))
102 (slot-name "a"))
103 (alert (slot-value foo slot-name)))
104 "{
105 var foo = { a : 1 };
106 var slotName = 'a';
107 alert(foo[slotName]);
108 }"); Last line was alert(foo.slotName) before bug-fix.
109
110 (test-ps-js buggy-slot-value-two
111 (slot-value foo (get-slot-name))
112 "foo[getSlotName()]")
113
114 (test-ps-js old-case-is-now-switch
115 ;; Switch was "case" before, but that was very non-lispish.
116 ;; For example, this code makes three messages and not one
117 ;; which may have been expected. This is because a switch
118 ;; statment must have a break statement for it to return
119 ;; after the alert. Otherwise it continues on the next
120 ;; clause.
121 (switch (aref blorg i)
122 (1 (alert "one"))
123 (2 (alert "two"))
124 (default (alert "default clause")))
125 "switch (blorg[i]) {
126 case 1: alert('one');
127 case 2: alert('two');
128 default: alert('default clause');
129 }")
130
131 (test-ps-js lisp-like-case
132 (case (aref blorg i)
133 (1 (alert "one"))
134 (2 (alert "two"))
135 (default (alert "default clause")))
136 "switch (blorg[i]) {
137 case 1:
138 alert('one');
139 break;
140 case 2:
141 alert('two');
142 break;
143 default: alert('default clause');
144 }")
145
146
147 (test-ps-js even-lispier-case
148 (case (aref blorg i)
149 ((1 2) (alert "Below three"))
150 (3 (alert "Three"))
151 (t (alert "Something else")))
152 "switch (blorg[i]) {
153 case 1: ;
154 case 2:
155 alert('Below three');
156 break;
157 case 3:
158 alert('Three');
159 break;
160 default: alert('Something else');
161 }")
162
163 (test-ps-js otherwise-case
164 (case (aref blorg i)
165 (1 (alert "one"))
166 (otherwise (alert "default clause")))
167 "switch (blorg[i]) {
168 case 1:
169 alert('one');
170 break;
171 default: alert('default clause');
172 }")
173
174 (test escape-sequences-in-string
175 (let ((escapes `((#\\ . #\\)
176 (#\b . #\Backspace)
177 (#\f . #\Form)
178 ("u000b" . ,(code-char #x000b));;Vertical tab, too uncommon to bother with
179 (#\n . #\Newline)
180 (#\r . #\Return)
181 (#\' . #\');;Double quote need not be quoted because parenscript strings are single quoted
182 (#\t . #\Tab)
183 ("u001f" . ,(code-char #x001f));; character below 32
184 ("u0080" . ,(code-char 128)) ;;Character over 127. Actually valid, parenscript escapes them to be sure.
185 ("uabcd" . ,(code-char #xabcd)))));; Really above ascii.
186 (loop for (js-escape . lisp-char) in escapes
187 for generated = (js-to-string `(let ((x , (format nil "hello~ahi" lisp-char)))))
188 for wanted = (format nil "{
189 var x = 'hello\\~ahi';
190 }" js-escape)
191 do (is (string= generated wanted)))))