| 1 |
;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (C) 2010, 2011, 2013, 2016 Free Software Foundation, Inc. |
| 4 |
;;;; |
| 5 |
;;;; This library is free software; you can redistribute it and/or |
| 6 |
;;;; modify it under the terms of the GNU Lesser General Public |
| 7 |
;;;; License as published by the Free Software Foundation; either |
| 8 |
;;;; version 3 of the License, or (at your option) any later version. |
| 9 |
;;;; |
| 10 |
;;;; This library is distributed in the hope that it will be useful, |
| 11 |
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
;;;; Lesser General Public License for more details. |
| 14 |
;;;; |
| 15 |
;;;; You should have received a copy of the GNU Lesser General Public |
| 16 |
;;;; License along with this library; if not, write to the Free Software |
| 17 |
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 |
|
| 19 |
(define-module (test-ecmascript) |
| 20 |
#:use-module (test-suite lib) |
| 21 |
#:use-module (language ecmascript parse) |
| 22 |
#:use-module ((system base compile) #:select (compile))) |
| 23 |
|
| 24 |
|
| 25 |
(define (eread str) |
| 26 |
(call-with-input-string str read-ecmascript)) |
| 27 |
(define (eread/1 str) |
| 28 |
(call-with-input-string str read-ecmascript/1)) |
| 29 |
|
| 30 |
(define-syntax parse |
| 31 |
(syntax-rules () |
| 32 |
((_ expression expected) |
| 33 |
(begin |
| 34 |
(pass-if expression |
| 35 |
(equal? expected (eread expression))) |
| 36 |
(pass-if expression |
| 37 |
(equal? expected (eread/1 expression))))))) |
| 38 |
|
| 39 |
(with-test-prefix "parser" |
| 40 |
|
| 41 |
(parse "true;" 'true) |
| 42 |
(parse "2 + 2;" '(+ (number 2) (number 2))) |
| 43 |
(parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace |
| 44 |
(parse "\"hello\";" '(string "hello")) |
| 45 |
(parse "function square(x) { return x * x; }" |
| 46 |
'(var (square (lambda (x) (return (* (ref x) (ref x))))))) |
| 47 |
(parse "document.write('Hello, world!');" |
| 48 |
'(call (pref (ref document) write) ((string "Hello, world!")))) |
| 49 |
(parse "var x = { foo: 12, bar: \"hello\" };" |
| 50 |
'(begin (var (x (object (foo (number 12)) |
| 51 |
(bar (string "hello"))))) |
| 52 |
(begin))) |
| 53 |
(parse "\"\\x12\";" ; Latin-1 escape in string literal |
| 54 |
'(string "\x12")) |
| 55 |
(parse "\"\\u1234\";" ; Unicode escape in string literal |
| 56 |
'(string "\u1234")) |
| 57 |
(parse "function foo(x) { }" ; empty function body |
| 58 |
'(var (foo (lambda (x) (begin))))) |
| 59 |
(parse ".123;" '(number 0.123)) |
| 60 |
(parse "0xff;" '(number 255))) |
| 61 |
|
| 62 |
|
| 63 |
(define-syntax ecompile |
| 64 |
(syntax-rules () |
| 65 |
((_ expression) |
| 66 |
(pass-if expression |
| 67 |
(not (not |
| 68 |
(compile (call-with-input-string expression read-ecmascript) |
| 69 |
#:from 'ecmascript |
| 70 |
#:to 'value))))) |
| 71 |
((_ expression expected) |
| 72 |
(pass-if expression |
| 73 |
(equal? expected |
| 74 |
(compile (call-with-input-string expression read-ecmascript) |
| 75 |
#:from 'ecmascript |
| 76 |
#:to 'value)))))) |
| 77 |
|
| 78 |
(with-test-prefix "compiler" |
| 79 |
|
| 80 |
(ecompile "true;" #t) |
| 81 |
(ecompile "if (3 > 2) true; else false;" #t) |
| 82 |
(ecompile "2 + 2;" 4) |
| 83 |
(ecompile "\"hello\";" "hello") |
| 84 |
(ecompile "var test = { bar: 1 };") |
| 85 |
|
| 86 |
(pass-if "new Object;" |
| 87 |
(not (not |
| 88 |
(compile (call-with-input-string "new Object;" read-ecmascript) |
| 89 |
#:from 'ecmascript |
| 90 |
#:to 'tree-il)))) ; Can't reference `Object' as value here |
| 91 |
|
| 92 |
;; FIXME: Broken! |
| 93 |
;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });" |
| 94 |
;; '(1 4 9 16)) |
| 95 |
|
| 96 |
;; Examples from |
| 97 |
;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>. |
| 98 |
|
| 99 |
(ecompile "42 + \" good times!\";" |
| 100 |
"42 good times!") |
| 101 |
(ecompile "[0,1,2,3,4,5].length * 7;" |
| 102 |
42)) |