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