2 ;; Other tests not in the reference
4 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
8 (test-ps-js plus-is-not-commutative
9 (setf x
(+ "before" x
"after"))
10 "x = 'before' + x + 'after';")
12 (test-ps-js plus-works-if-first
13 (setf x
(+ x
"middle" "after"))
14 "x += 'middle' + 'after';")
16 (test-ps-js setf-side-effects
22 (setf x
(+ 2 (side-effect) x
5))))
25 function sideEffect() {
29 x = 2 + sideEffect() + x + 5;")
30 ;; Parenscript used to optimize to much:
32 ;; function sideEffect() {
36 ;; x += 2 + sideEffect() + 5;
38 ;; Which is 20, not 14
41 (test-ps-js dot-notation-bug
42 (.match
(+ "" x
) "foo")
43 "('' + x).match('foo')")
45 (test-ps-js method-call-op-form
(.to-string
(+ "" x
)) "('' + x).toString()")
46 (test-ps-js method-call-number
(.to-string
10) "( 10 ).toString()")
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"))))
51 "( { toString : function () { return 'it works'; } } ).toString()")
53 (test-ps-js method-call-variable
57 (test-ps-js method-call-array
58 (.to-string
(list 10 20))
59 "[ 10, 20 ].toString()")
60 (test-ps-js method-call-fn-call
63 (test-ps-js method-call-lambda-fn
64 (.to-string
(lambda () (alert 10)))
65 "( function () { alert(10); } ).toString()")
66 (test-ps-js method-call-lambda-call
67 (.to-string
((lambda (x) (return x
)) 10))
68 "(function (x) { return x; }) (10).toString()")
70 (test no-whitespace-before-dot
71 (let* ((str (compile-script '(.to-string
((lambda (x) (return x
)) 10))))
72 (dot-pos (position #\. str
:test
#'char
=))
73 (char-before (elt str
(1- dot-pos
)))
75 (is (char= char-before a-parenthesis
))))
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"
86 ('<div id=\"777\" style=\"'
87 + ('border:1pxsssssssssss;font-size:x-small;height:' + 2 * 200 + ';width:'
89 + '\"></div>')") ;";This line should start with a plus character.
92 (test-ps-js simple-slot-value
93 (let ((foo (create :a
1)))
94 (alert (slot-value foo
'a
)))
100 (test-ps-js buggy-slot-value
101 (let ((foo (create :a
1))
103 (alert (slot-value foo slot-name
)))
107 alert(foo[slotName]);
108 }"); Last line was alert(foo.slotName) before bug-fix.
110 (test-ps-js buggy-slot-value-two
111 (slot-value foo
(get-slot-name))
112 "foo[getSlotName()]")
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
121 (switch (aref blorg i
)
124 (default (alert "default clause")))
126 case 1: alert('one');
127 case 2: alert('two');
128 default: alert('default clause');
131 (test-ps-js lisp-like-case
135 (default (alert "default clause")))
143 default: alert('default clause');
147 (test-ps-js even-lispier-case
149 ((1 2) (alert "Below three"))
151 (t (alert "Something else")))
155 alert('Below three');
160 default: alert('Something else');
163 (test-ps-js otherwise-case
166 (otherwise (alert "default clause")))
171 default: alert('default clause');
174 (test escape-sequences-in-string
175 (let ((escapes `((#\\ .
#\\)
177 (#\f .
,(code-char 12))
178 ("u000B" .
,(code-char #x000b
));;Vertical tab, too uncommon to bother with
181 (#\' .
#\');;Double quote need not be quoted because parenscript strings are single quoted
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
= (compile-script `(let ((x ,(format nil
"hello~ahi" lisp-char
)))))
188 for wanted
= (format nil
"{
189 var x = 'hello\\~ahi';
191 do
(is (string= generated wanted
)))))
193 (test-ps-js complicated-symbol-name1
197 (test-ps-js complicated-symbol-name2
201 (test-ps-js slot-value-setf
202 (setf (slot-value x 'y) (+ (+ a 3) 4))
203 "x.y = (a + 3) + 4;")
205 (test-ps-js slot-value-conditional1
206 (slot-value (if zoo foo bar) 'x)
207 "(zoo ? foo : bar).x")
209 (test-ps-js slot-value-conditional2
210 (slot-value (if (not zoo) foo bar) 'x)
211 "(!zoo ? foo : bar).x")
213 (test script-star-eval1
214 (is (string= "x = 1; y = 2;" (normalize-js-code (script* '(setf x 1) '(setf y 2))))))
216 (test script-star-eval2
217 (is (string= "x = 1;" (normalize-js-code (script* '(setf x 1))))))
219 (test-ps-js slot-value-null1
223 (test-ps-js slot-value-null2
224 (slot-value foo 'nil)
227 (test-ps-js unquoted-nil
231 (test-ps-js list-with-single-nil
235 (test-ps-js quoted-nil
240 (ps (defsetf baz (x y) (newval) `(set-baz ,x ,y ,newval)))
241 (is (string= "var PS_GS_1 = 3; var PS_GS_2 = 2; var PS_GS_3 = 1; setBaz(PS_GS_3, PS_GS_2, PS_GS_1);"
242 (normalize-js-code (let ((ps::*gen-script-name-counter* 0))
243 (ps (setf (baz 1 2) 3)))))))
245 (test-ps-js defun-optional1
246 (defun test-opt (&optional x) (return (if x "yes" "no")))
247 "function testOpt(x) {
248 x = undefined === x && null || x;
249 return x ? 'yes' : 'no';