Commit | Line | Data |
---|---|---|
171bbab3 | 1 | (in-package :ps-test) |
e2932347 LC |
2 | ;; Other tests not in the reference |
3 | ||
4 | (eval-when (:compile-toplevel :load-toplevel :execute) | |
5 | (def-suite ps-tests)) | |
6 | (in-suite ps-tests) | |
7 | ||
8 | (test-ps-js plus-is-not-commutative | |
9 | (setf x (+ "before" x "after")) | |
72332f2a | 10 | "x = 'before' + x + 'after';") |
e2932347 LC |
11 | |
12 | (test-ps-js plus-works-if-first | |
13 | (setf x (+ x "middle" "after")) | |
72332f2a | 14 | "x += 'middle' + 'after';") |
e2932347 LC |
15 | |
16 | (test-ps-js setf-side-effects | |
17 | (progn | |
18 | (let ((x 10)) | |
19 | (defun side-effect() | |
20 | (setf x 4) | |
21 | (return 3)) | |
22 | (setf x (+ 2 (side-effect) x 5)))) | |
23 | " | |
24 | var x = 10; | |
25 | function sideEffect() { | |
26 | x = 4; | |
27 | return 3; | |
28 | }; | |
29 | x = 2 + sideEffect() + x + 5;") | |
30 | ;; Parenscript used to optimize to much: | |
31 | ;; var x = 10; | |
32 | ;; function sideEffect() { | |
33 | ;; x = 4; | |
34 | ;; return 3; | |
35 | ;; }; | |
36 | ;; x += 2 + sideEffect() + 5; | |
37 | ;; | |
38 | ;; Which is 20, not 14 | |
39 | ||
40 | ||
41 | (test-ps-js dot-notation-bug | |
42 | (.match (+ "" x) "foo") | |
43 | "('' + x).match('foo')") | |
44 | ||
45 | (test-ps-js method-call-op-form (.to-string (+ "" x)) "('' + x).toString()") | |
cf460f93 | 46 | (test-ps-js method-call-number (.to-string 10) "( 10 ).toString()") |
e2932347 LC |
47 | (test-ps-js method-call-string (.to-string "hi") "'hi'.toString()") |
48 | (test-ps-js method-call-lit-object | |
49 | (.to-string (create :to-string (lambda () | |
50 | (return "it works")))) | |
cf460f93 | 51 | "( { toString : function () { return 'it works'; } } ).toString()") |
e2932347 LC |
52 | |
53 | (test-ps-js method-call-variable | |
54 | (.to-string x) | |
55 | "x.toString()") | |
56 | ||
57 | (test-ps-js method-call-array | |
58 | (.to-string (list 10 20)) | |
cf460f93 | 59 | "[ 10, 20 ].toString()") |
e2932347 LC |
60 | (test-ps-js method-call-fn-call |
61 | (.to-string (foo)) | |
62 | "foo().toString()") | |
63 | (test-ps-js method-call-lambda-fn | |
64 | (.to-string (lambda () (alert 10))) | |
cf460f93 | 65 | "( function () { alert(10); } ).toString()") |
e2932347 LC |
66 | (test-ps-js method-call-lambda-call |
67 | (.to-string ((lambda (x) (return x)) 10)) | |
cf460f93 | 68 | "(function (x) { return x; }) (10).toString()") |
e2932347 LC |
69 | |
70 | (test no-whitespace-before-dot | |
905f534e | 71 | (let* ((str (compile-script '(.to-string ((lambda (x) (return x)) 10)))) |
e2932347 LC |
72 | (dot-pos (position #\. str :test #'char=)) |
73 | (char-before (elt str (1- dot-pos))) | |
74 | (a-parenthesis #\))) | |
75 | (is (char= char-before a-parenthesis)))) | |
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")) | |
e0f0d152 | 166 | (otherwise (alert "default clause"))) |
e2932347 LC |
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 | |
905f534e | 175 | (let ((escapes `((#\\ . #\\) |
e2932347 LC |
176 | (#\b . #\Backspace) |
177 | (#\f . ,(code-char 12)) | |
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 | |
905f534e | 187 | for generated = (compile-script `(let ((x ,(format nil "hello~ahi" lisp-char))))) |
9da682ca | 188 | for wanted = (format nil "{ |
e2932347 LC |
189 | var x = 'hello\\~ahi'; |
190 | }" js-escape) | |
9da682ca RD |
191 | do (is (string= generated wanted))))) |
192 | ||
e0f0d152 VS |
193 | (test-ps-js complicated-symbol-name1 |
194 | grid-rows[foo].bar | |
195 | "gridRows[foo].bar") | |
196 | ||
197 | (test-ps-js complicated-symbol-name2 | |
198 | *grid-rows*[foo].bar | |
199 | "GRIDROWS[foo].bar") | |
cf460f93 VS |
200 | |
201 | (test-ps-js slot-value-setf | |
202 | (setf (slot-value x 'y) (+ (+ a 3) 4)) | |
72332f2a | 203 | "x.y = (a + 3) + 4;") |
eb0befe2 VS |
204 | |
205 | (test-ps-js slot-value-conditional1 | |
206 | (slot-value (if zoo foo bar) 'x) | |
207 | "(zoo ? foo : bar).x") | |
208 | ||
209 | (test-ps-js slot-value-conditional2 | |
210 | (slot-value (if (not zoo) foo bar) 'x) | |
211 | "(!zoo ? foo : bar).x") | |
212 | ||
245d0fbd | 213 | (test script-star-eval1 |
905f534e | 214 | (is (string= "x = 1; y = 2;" (normalize-js-code (script* '(setf x 1) '(setf y 2)))))) |
245d0fbd VS |
215 | |
216 | (test script-star-eval2 | |
905f534e | 217 | (is (string= "x = 1;" (normalize-js-code (script* '(setf x 1)))))) |
62b4a867 VS |
218 | |
219 | (test-ps-js slot-value-null1 | |
220 | (slot-value foo nil) | |
221 | "foo") | |
222 | ||
223 | (test-ps-js slot-value-null2 | |
224 | (slot-value foo 'nil) | |
225 | "foo") | |
226 | ||
46f794a4 RD |
227 | (test-ps-js unquoted-nil |
228 | nil | |
229 | "null") | |
230 | ||
231 | (test-ps-js list-with-single-nil | |
232 | (array 'nil) | |
233 | "[null]") | |
234 | ||
62b4a867 VS |
235 | (test-ps-js quoted-nil |
236 | 'nil | |
72332f2a VS |
237 | "null") |
238 | ||
239 | (test defsetf1 | |
240 | (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval))) | |
cdf9ab0e | 241 | (is (string= "var PS_GS_2 = 1; var PS_GS_3 = 2; var PS_GS_1 = 3; setBaz(PS_GS_2, PS_GS_3, PS_GS_1);" |
905f534e | 242 | (normalize-js-code (let ((ps::*gen-script-name-counter* 0)) |
72332f2a | 243 | (ps (setf (baz 1 2) 3))))))) |
d989d711 | 244 | |
750651b0 VS |
245 | (test defsetf-short |
246 | (ps (defsetf baz set-baz "blah")) | |
247 | (is (string= "setBaz(1, 2, 3, 'foo');" (normalize-js-code (ps (setf (baz 1 2 3) "foo")))))) | |
248 | ||
dbb7017b VS |
249 | (test defun-setf1 |
250 | (is (and (string= (normalize-js-code (ps:ps (defun (setf some-thing) (new-val i1 i2) | |
251 | (setf (aref *some-thing* i1 i2) new-val)))) | |
252 | "null; function __setf_someThing(newVal, i1, i2) { SOMETHING[i1][i2] = newVal; };") | |
253 | (string= (let ((ps::*gen-script-name-counter* 0)) (normalize-js-code (ps:ps (setf (some-thing 1 2) "foo")))) | |
254 | "var PS_GS_2 = 1; var PS_GS_3 = 2; var PS_GS_1 = 'foo'; __setf_someThing(PS_GS_1, PS_GS_2, PS_GS_3);")))) | |
255 | ||
d989d711 VS |
256 | (test-ps-js defun-optional1 |
257 | (defun test-opt (&optional x) (return (if x "yes" "no"))) | |
258 | "function testOpt(x) { | |
259 | x = undefined === x && null || x; | |
260 | return x ? 'yes' : 'no'; | |
a2434734 VS |
261 | }") |
262 | ||
263 | (test-ps-js return-nothing | |
264 | (return) | |
265 | "return null") | |
dbb7017b VS |
266 | |
267 | (test-ps-js set-timeout | |
268 | (do-set-timeout (10) (alert "foo")) | |
269 | "setTimeout (function () { alert('foo'); }, 10)") | |
270 | ||
271 |