reference-tests.lisp 10.8 KB
Newer Older
Red Daly's avatar
Red Daly committed
1
(in-package :ps-test)
Henrik Hjelte's avatar
Henrik Hjelte committed
2 3 4 5
;; Tests of everything in the reference.
;; File is generated automatically from the text in reference.lisp by
;; the function make-reference-tests-dot-lisp in ref2test.lisp
;; so do not edit this file.
6 7
(eval-when (:compile-toplevel :load-toplevel :execute)
  (def-suite ref-tests))
Henrik Hjelte's avatar
Henrik Hjelte committed
8 9
(in-suite ref-tests)

10 11
(test-ps-js statements-and-expressions-1
  (+ i (if 1 2 3))
Henrik Hjelte's avatar
Henrik Hjelte committed
12 13
  "i + (1 ? 2 : 3)")

14 15
(test-ps-js statements-and-expressions-2
  (if 1 2 3)
Henrik Hjelte's avatar
Henrik Hjelte committed
16 17 18 19 20 21
  "if (1) {
  2;
} else {
  3;
}")

22
(test-ps-js symbol-conversion-1
23 24
  !?#@%
  "bangwhathashatpercent")
Henrik Hjelte's avatar
Henrik Hjelte committed
25

26 27
(test-ps-js symbol-conversion-2
  bla-foo-bar
Henrik Hjelte's avatar
Henrik Hjelte committed
28 29
  "blaFooBar")

30 31
(test-ps-js symbol-conversion-3
  *array
Henrik Hjelte's avatar
Henrik Hjelte committed
32 33
  "Array")

34 35
(test-ps-js symbol-conversion-6
  *global-array*
Henrik Hjelte's avatar
Henrik Hjelte committed
36 37
  "GLOBALARRAY")

38 39
(test-ps-js symbol-conversion-7
  *global-array*.length
Henrik Hjelte's avatar
Henrik Hjelte committed
40 41
  "GLOBALARRAY.length")

42 43
(test-ps-js number-literals-1
  1
Henrik Hjelte's avatar
Henrik Hjelte committed
44 45
  "1")

46 47
(test-ps-js number-literals-2
  123.123
Henrik Hjelte's avatar
Henrik Hjelte committed
48 49
  "123.123")

50 51
(test-ps-js number-literals-3
  #x10
Henrik Hjelte's avatar
Henrik Hjelte committed
52 53
  "16")

54 55
(test-ps-js string-literals-1
  "foobar"
Henrik Hjelte's avatar
Henrik Hjelte committed
56 57
  "'foobar'")

58 59
(test-ps-js string-literals-2
  "bratzel bub"
Henrik Hjelte's avatar
Henrik Hjelte committed
60 61
  "'bratzel bub'")

62 63
(test-ps-js array-literals-1
  (array)
Henrik Hjelte's avatar
Henrik Hjelte committed
64
  "[  ]")
Henrik Hjelte's avatar
Henrik Hjelte committed
65

66 67
(test-ps-js array-literals-2
  (array 1 2 3)
Henrik Hjelte's avatar
Henrik Hjelte committed
68 69
  "[ 1, 2, 3 ]")

70
(test-ps-js array-literals-3
Henrik Hjelte's avatar
Henrik Hjelte committed
71
  (array (array 2 3)
72
       (array "foobar" "bratzel bub"))
Henrik Hjelte's avatar
Henrik Hjelte committed
73 74
  "[ [ 2, 3 ], [ 'foobar', 'bratzel bub' ] ]")

75 76
(test-ps-js array-literals-4
  (make-array)
Henrik Hjelte's avatar
Henrik Hjelte committed
77 78
  "new Array()")

79 80
(test-ps-js array-literals-5
  (make-array 1 2 3)
Henrik Hjelte's avatar
Henrik Hjelte committed
81 82
  "new Array(1, 2, 3)")

83
(test-ps-js array-literals-6
Henrik Hjelte's avatar
Henrik Hjelte committed
84 85
  (make-array
 (make-array 2 3)
86
 (make-array "foobar" "bratzel bub"))
Henrik Hjelte's avatar
Henrik Hjelte committed
87 88
  "new Array(new Array(2, 3), new Array('foobar', 'bratzel bub'))")

89 90 91
(test-ps-js object-literals-1
  (create :foo "bar" :blorg 1)
  "{ foo : 'bar',
Henrik Hjelte's avatar
Henrik Hjelte committed
92 93
  blorg : 1 }")

94
(test-ps-js object-literals-2
Henrik Hjelte's avatar
Henrik Hjelte committed
95 96
  (create :foo "hihi"
        :blorg (array 1 2 3)
97 98 99
        :another-object (create :schtrunz 1))
  "{ foo : 'hihi',
  blorg : [ 1, 2, 3 ],
Henrik Hjelte's avatar
Henrik Hjelte committed
100 101
  anotherObject : { schtrunz : 1 } }")

102 103
(test-ps-js object-literals-3
  (slot-value an-object 'foo)
Henrik Hjelte's avatar
Henrik Hjelte committed
104 105
  "anObject.foo")

106 107
(test-ps-js object-literals-4
  an-object.foo
Henrik Hjelte's avatar
Henrik Hjelte committed
108 109
  "anObject.foo")

110
(test-ps-js object-literals-5
Henrik Hjelte's avatar
Henrik Hjelte committed
111
  (with-slots (a b c) this
112
  (+ a b c))
113
  "this.a + this.b + this.c;")
Henrik Hjelte's avatar
Henrik Hjelte committed
114

115
(test-ps-js regular-expression-literals-1
Henrik Hjelte's avatar
Henrik Hjelte committed
116 117 118 119
  (regex "foobar")
  "/foobar/")

(test-ps-js regular-expression-literals-2
120
  (regex "/foobar/i")
Henrik Hjelte's avatar
Henrik Hjelte committed
121 122
  "/foobar/i")

123 124
(test-ps-js literal-symbols-1
  T
Henrik Hjelte's avatar
Henrik Hjelte committed
125 126
  "true")

127 128
(test-ps-js literal-symbols-2
  FALSE
Henrik Hjelte's avatar
Henrik Hjelte committed
129 130
  "false")

131 132
(test-ps-js literal-symbols-3
  NIL
Henrik Hjelte's avatar
Henrik Hjelte committed
133 134
  "null")

135 136
(test-ps-js literal-symbols-4
  UNDEFINED
Henrik Hjelte's avatar
Henrik Hjelte committed
137 138
  "undefined")

139 140
(test-ps-js literal-symbols-5
  THIS
Henrik Hjelte's avatar
Henrik Hjelte committed
141 142
  "this")

143 144
(test-ps-js variables-1
  variable
Henrik Hjelte's avatar
Henrik Hjelte committed
145 146
  "variable")

147 148
(test-ps-js variables-2
  a-variable
Henrik Hjelte's avatar
Henrik Hjelte committed
149 150
  "aVariable")

151 152
(test-ps-js variables-3
  *math
Henrik Hjelte's avatar
Henrik Hjelte committed
153 154
  "Math")

155 156
(test-ps-js variables-4
  *math.floor
Henrik Hjelte's avatar
Henrik Hjelte committed
157 158
  "Math.floor")

159 160
(test-ps-js function-calls-and-method-calls-1
  (blorg 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
161 162
  "blorg(1, 2)")

163 164
(test-ps-js function-calls-and-method-calls-2
  (foobar (blorg 1 2) (blabla 3 4) (array 2 3 4))
Henrik Hjelte's avatar
Henrik Hjelte committed
165 166
  "foobar(blorg(1, 2), blabla(3, 4), [ 2, 3, 4 ])")

167 168
(test-ps-js function-calls-and-method-calls-3
  ((aref foo i) 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
169 170
  "foo[i](1, 2)")

171 172
(test-ps-js function-calls-and-method-calls-4
  (.blorg this 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
173 174
  "this.blorg(1, 2)")

175 176
(test-ps-js function-calls-and-method-calls-5
  (this.blorg 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
177 178
  "this.blorg(1, 2)")

179 180
(test-ps-js function-calls-and-method-calls-6
  (.blorg (aref foobar 1) NIL T)
Henrik Hjelte's avatar
Henrik Hjelte committed
181 182
  "foobar[1].blorg(null, true)")

183 184
(test-ps-js operator-expressions-1
  (* 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
185 186
  "1 * 2")

187 188
(test-ps-js operator-expressions-2
  (= 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
189 190
  "1 == 2")

191 192
(test-ps-js operator-expressions-3
  (eql 1 2)
Henrik Hjelte's avatar
Henrik Hjelte committed
193 194
  "1 == 2")

195 196
(test-ps-js operator-expressions-5
  (* 1 (+ 2 3 4) 4 (/ 6 7))
Henrik Hjelte's avatar
Henrik Hjelte committed
197 198
  "1 * (2 + 3 + 4) * 4 * (6 / 7)")

199 200
(test-ps-js operator-expressions-6
  (incf i)
Henrik Hjelte's avatar
Henrik Hjelte committed
201 202
  "++i")

203
(test-ps-js operator-expressions-7
204
  (decf i)
Henrik Hjelte's avatar
Henrik Hjelte committed
205 206
  "--i")

207
(test-ps-js operator-expressions-8
208
  (1- i)
Henrik Hjelte's avatar
Henrik Hjelte committed
209 210
  "i - 1")

211
(test-ps-js operator-expressions-9
212
  (1+ i)
Henrik Hjelte's avatar
Henrik Hjelte committed
213 214
  "i + 1")

215
(test-ps-js operator-expressions-10
216
  (not (< i 2))
Henrik Hjelte's avatar
Henrik Hjelte committed
217 218
  "i >= 2")

219
(test-ps-js operator-expressions-11
220
  (not (eql i 2))
Henrik Hjelte's avatar
Henrik Hjelte committed
221 222
  "i != 2")

223 224
(test-ps-js body-forms-1
  (progn (blorg i) (blafoo i))
Henrik Hjelte's avatar
Henrik Hjelte committed
225 226 227
  "blorg(i);
blafoo(i);")

228 229
(test-ps-js body-forms-2
  (+ i (progn (blorg i) (blafoo i)))
Henrik Hjelte's avatar
Henrik Hjelte committed
230 231
  "i + (blorg(i), blafoo(i))")

232
(test-ps-js function-definition-1
Henrik Hjelte's avatar
Henrik Hjelte committed
233
  (defun a-function (a b)
234
  (return (+ a b)))
Henrik Hjelte's avatar
Henrik Hjelte committed
235 236 237 238
  "function aFunction(a, b) {
  return a + b;
}")

239 240
(test-ps-js function-definition-2
  (lambda (a b) (return (+ a b)))
Henrik Hjelte's avatar
Henrik Hjelte committed
241 242 243 244
  "function (a, b) {
  return a + b;
}")

245 246
(test-ps-js assignment-1
  (setf a 1)
Vladimir Sedach's avatar
Vladimir Sedach committed
247
  "a = 1;")
Henrik Hjelte's avatar
Henrik Hjelte committed
248

249 250
(test-ps-js assignment-2
  (setf a 2 b 3 c 4 x (+ a b c))
Henrik Hjelte's avatar
Henrik Hjelte committed
251 252 253 254 255
  "a = 2;
b = 3;
c = 4;
x = a + b + c;")

256 257
(test-ps-js assignment-3
  (setf a (1+ a))
Vladimir Sedach's avatar
Vladimir Sedach committed
258
  "a++;")
Henrik Hjelte's avatar
Henrik Hjelte committed
259

260
(test-ps-js assignment-4
261
  (setf a (+ a 2 3 4 a))
Vladimir Sedach's avatar
Vladimir Sedach committed
262
  "a += 2 + 3 + 4 + a;")
Henrik Hjelte's avatar
Henrik Hjelte committed
263

264 265
(test-ps-js assignment-5
  (setf a (- 1 a))
Vladimir Sedach's avatar
Vladimir Sedach committed
266
  "a = 1 - a;")
Henrik Hjelte's avatar
Henrik Hjelte committed
267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
(test-ps-js assignment-6
  (defun (setf color) (new-color el)
  (setf (slot-value (slot-value el 'style) 'color) new-color))
  "function __setf_color(newColor, el) {
  el.style.color = newColor;
};")

(test-ps-js assignment-7
  (setf (color some-div) (+ 23 "em"))
  "var _js2 = someDiv;
var _js1 = 23 + 'em';
__setf_color(_js1, _js2);")

(test-ps-js assignment-8
  (defsetf left (el) (offset)
  `(setf (slot-value (slot-value ,el 'style) 'left) ,offset))
  "null")

(test-ps-js assignment-9
  (setf (left some-div) (+ 123 "px"))
  "var _js2 = someDiv;
var _js1 = 123 + 'px';
_js2.style.left = _js1;")

(test-ps-js assignment-10
  (progn (defmacro left (el)
         `(slot-value ,el 'offset-left))
       (left some-div))
  "someDiv.offsetLeft;")

298 299
(test-ps-js single-argument-statements-1
  (return 1)
Henrik Hjelte's avatar
Henrik Hjelte committed
300 301
  "return 1")

302 303
(test-ps-js single-argument-statements-2
  (throw "foobar")
Henrik Hjelte's avatar
Henrik Hjelte committed
304 305
  "throw 'foobar'")

306 307
(test-ps-js single-argument-expression-1
  (delete (new (*foobar 2 3 4)))
Henrik Hjelte's avatar
Henrik Hjelte committed
308 309
  "delete new Foobar(2, 3, 4)")

310
(test-ps-js single-argument-expression-2
Henrik Hjelte's avatar
Henrik Hjelte committed
311 312
  (if (= (typeof blorg) *string)
    (alert (+ "blorg is a string: " blorg))
313
    (alert "blorg is not a string"))
Henrik Hjelte's avatar
Henrik Hjelte committed
314 315 316 317 318 319
  "if (typeof blorg == String) {
  alert('blorg is a string: ' + blorg);
} else {
  alert('blorg is not a string');
}")

320
(test-ps-js conditional-statements-1
Henrik Hjelte's avatar
Henrik Hjelte committed
321 322
  (if (blorg.is-correct)
    (progn (carry-on) (return i))
323
    (alert "blorg is not correct!"))
Henrik Hjelte's avatar
Henrik Hjelte committed
324 325 326 327 328 329 330
  "if (blorg.isCorrect()) {
  carryOn();
  return i;
} else {
  alert('blorg is not correct!');
}")

331 332
(test-ps-js conditional-statements-2
  (+ i (if (blorg.add-one) 1 2))
Henrik Hjelte's avatar
Henrik Hjelte committed
333 334
  "i + (blorg.addOne() ? 1 : 2)")

335
(test-ps-js conditional-statements-3
Henrik Hjelte's avatar
Henrik Hjelte committed
336 337
  (when (blorg.is-correct)
  (carry-on)
338
  (return i))
Henrik Hjelte's avatar
Henrik Hjelte committed
339 340 341 342 343
  "if (blorg.isCorrect()) {
  carryOn();
  return i;
}")

344
(test-ps-js conditional-statements-4
Henrik Hjelte's avatar
Henrik Hjelte committed
345
  (unless (blorg.is-correct)
346
  (alert "blorg is not correct!"))
Henrik Hjelte's avatar
Henrik Hjelte committed
347 348 349 350
  "if (!blorg.isCorrect()) {
  alert('blorg is not correct!');
}")

351 352
(test-ps-js variable-declaration-1
  (defvar *a* (array 1 2 3))
353
  "var A = [ 1, 2, 3 ]")
Henrik Hjelte's avatar
Henrik Hjelte committed
354

355
(test-ps-js variable-declaration-2
Henrik Hjelte's avatar
Henrik Hjelte committed
356 357 358 359
  (if (= i 1)
    (progn (defvar blorg "hallo")
           (alert blorg))
    (progn (defvar blorg "blitzel")
360
           (alert blorg)))
Henrik Hjelte's avatar
Henrik Hjelte committed
361 362 363 364 365 366 367 368
  "if (i == 1) {
  var blorg = 'hallo';
  alert(blorg);
} else {
  var blorg = 'blitzel';
  alert(blorg);
}")

369
(test-ps-js variable-declaration-3
Henrik Hjelte's avatar
Henrik Hjelte committed
370 371 372 373
  (if (= i 1)
    (let ((blorg "hallo"))
      (alert blorg))
    (let ((blorg "blitzel"))
374
      (alert blorg)))
Henrik Hjelte's avatar
Henrik Hjelte committed
375 376 377 378 379 380 381 382
  "if (i == 1) {
  var blorg = 'hallo';
  alert(blorg);
} else {
  var blorg = 'blitzel';
  alert(blorg);
}")

383
(test-ps-js iteration-constructs-1
Henrik Hjelte's avatar
Henrik Hjelte committed
384 385 386 387
  (do ((i 0 (1+ i))
     (l (aref blorg i) (aref blorg i)))
    ((or (= i blorg.length)
         (eql l "Fumitastic")))
388 389
  (document.write (+ "L is " l)))
  "for (var i = 0, l = blorg[i];
Henrik Hjelte's avatar
Henrik Hjelte committed
390
     !(i == blorg.length || l == 'Fumitastic');
Henrik Hjelte's avatar
Henrik Hjelte committed
391 392 393 394
     i = i + 1, l = blorg[i]) {
  document.write('L is ' + l);
}")

395
(test-ps-js iteration-constructs-2
Henrik Hjelte's avatar
Henrik Hjelte committed
396
  (dotimes (i blorg.length)
397
  (document.write (+ "L is " (aref blorg i))))
Henrik Hjelte's avatar
Henrik Hjelte committed
398
  "for (var i = 0; i < blorg.length; i = i + 1) {
Henrik Hjelte's avatar
Henrik Hjelte committed
399 400 401
  document.write('L is ' + blorg[i]);
}")

402
(test-ps-js iteration-constructs-3
Henrik Hjelte's avatar
Henrik Hjelte committed
403
  (dolist (l blorg)
404
  (document.write (+ "L is " l)))
405
  "  var tmpArr1 = blorg;
Henrik Hjelte's avatar
Henrik Hjelte committed
406 407 408 409
  for (var tmpI2 = 0; tmpI2 < tmpArr1.length;
    tmpI2 = tmpI2 + 1) {
    var l = tmpArr1[tmpI2];
    document.write('L is ' + l);
410
  };")
Henrik Hjelte's avatar
Henrik Hjelte committed
411

412
(test-ps-js iteration-constructs-4
Henrik Hjelte's avatar
Henrik Hjelte committed
413
  (doeach (i object)
414
   (document.write (+ i " is " (aref object i))))
Henrik Hjelte's avatar
Henrik Hjelte committed
415 416 417 418
  "for (var i in object) {
  document.write(i + ' is ' + object[i]);
}")

419
(test-ps-js iteration-constructs-5
Henrik Hjelte's avatar
Henrik Hjelte committed
420
  (while (film.is-not-finished)
421
  (this.eat (new *popcorn)))
Henrik Hjelte's avatar
Henrik Hjelte committed
422 423 424 425
  "while (film.isNotFinished()) {
  this.eat(new Popcorn);
}")

426
(test-ps-js the-case-statement-1
Henrik Hjelte's avatar
Henrik Hjelte committed
427
  (case (aref blorg i)
428
  ((1 "one") (alert "one"))
Henrik Hjelte's avatar
Henrik Hjelte committed
429
  (2 (alert "two"))
430
  (t (alert "default clause")))
Henrik Hjelte's avatar
Henrik Hjelte committed
431
  "switch (blorg[i]) {
432
  case 1:   
433 434 435 436 437 438
  case 'one':
            alert('one');
            break;
  case 2:
            alert('two');
            break;
Henrik Hjelte's avatar
Henrik Hjelte committed
439 440 441
  default:   alert('default clause');
}")

442 443 444 445 446 447 448 449 450 451 452
(test-ps-js the-case-statement-2
  (switch (aref blorg i)
  (1 (alert "If I get here"))
  (2 (alert "I also get here"))
  (default (alert "I always get here")))
  "switch (blorg[i]) {
  case 1:   alert('If I get here');
  case 2:   alert('I also get here');
  default:   alert('I always get here');
}")

453
(test-ps-js the-with-statement-1
454
  (with (create :foo "foo" :i "i")
455 456
  (alert (+ "i is now intermediary scoped: " i)))
  "with ({ foo : 'foo',
Henrik Hjelte's avatar
Henrik Hjelte committed
457 458 459 460
        i : 'i' }) {
  alert('i is now intermediary scoped: ' + i);
}")

461 462
(test-ps-js the-try-statement-1
  (try (throw "i")
Henrik Hjelte's avatar
Henrik Hjelte committed
463 464 465
 (:catch (error)
   (alert (+ "an error happened: " error)))
 (:finally
466
   (alert "Leaving the try form")))
Henrik Hjelte's avatar
Henrik Hjelte committed
467 468 469 470 471 472 473 474
  "try {
  throw 'i';
} catch (error) {
  alert('an error happened: ' + error);
} finally {
  alert('Leaving the try form');
}")

475
(test-ps-js the-html-generator-1
476
  (ps-html ((:a :href "foobar") "blorg"))
Henrik Hjelte's avatar
Henrik Hjelte committed
477
  "'<a href=\"foobar\">blorg</a>'")
Henrik Hjelte's avatar
Henrik Hjelte committed
478

479
(test-ps-js the-html-generator-2
480
  (ps-html ((:a :href (generate-a-link)) "blorg"))
Henrik Hjelte's avatar
Henrik Hjelte committed
481
  "'<a href=\"' + generateALink() + '\">blorg</a>'")
Henrik Hjelte's avatar
Henrik Hjelte committed
482

483
(test-ps-js the-html-generator-3
Henrik Hjelte's avatar
Henrik Hjelte committed
484
  (document.write
485 486
  (ps-html ((:a :href "#"
                :onclick (lisp (ps-inline (transport)))) "link")))
487
  "document.write('<a href=\"#\" onclick=\"' + 'javascript:transport();' + '\">link</a>')")
Henrik Hjelte's avatar
Henrik Hjelte committed
488

489
(test-ps-js the-html-generator-4
490 491 492
  (let ((disabled nil)
      (authorized t))
   (setf element.inner-h-t-m-l
493
         (ps-html ((:textarea (or disabled (not authorized)) :disabled "disabled")
494
                "Edit me"))))
495
  "   var disabled = null;
496 497 498 499
   var authorized = true;
   element.innerHTML =
   '<textarea'
   + (disabled || !authorized ? ' disabled=\"' + 'disabled' + '\"' : '')
500
   + '>Edit me</textarea>';")
501 502

(test-ps-js the-html-generator-5
Henrik Hjelte's avatar
Henrik Hjelte committed
503
  (css-inline :color "red"
504
            :font-size "x-small")
Henrik Hjelte's avatar
Henrik Hjelte committed
505 506
  "'color:red;font-size:x-small'")

507
(test-ps-js the-html-generator-6
Henrik Hjelte's avatar
Henrik Hjelte committed
508
  (defun make-color-div(color-name)
509
    (return (ps-html ((:div :style (css-inline :color color-name))
510
                   color-name " looks like this."))))
Henrik Hjelte's avatar
Henrik Hjelte committed
511 512 513 514 515
  "function makeColorDiv(colorName) {
  return '<div style=\"' + ('color:' + colorName) + '\">' + colorName
    + ' looks like this.</div>';
}")