1 ;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*-
3 ;;;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc.
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.
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.
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
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)))
26 (with-fluids ((%default-port-encoding "utf-8"))
27 (call-with-input-string str read-ecmascript)))
29 (with-fluids ((%default-port-encoding "utf-8"))
30 (call-with-input-string str read-ecmascript/1)))
34 ((_ expression expected)
37 (equal? expected (eread expression)))
39 (equal? expected (eread/1 expression)))))))
41 (with-test-prefix "parser"
44 (parse "2 + 2;" '(+ (number 2) (number 2)))
45 (parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace
46 (parse "\"hello\";" '(string "hello"))
47 (parse "function square(x) { return x * x; }"
48 '(var (square (lambda (x) (return (* (ref x) (ref x)))))))
49 (parse "document.write('Hello, world!');"
50 '(call (pref (ref document) write) ((string "Hello, world!"))))
51 (parse "var x = { foo: 12, bar: \"hello\" };"
52 '(begin (var (x (object (foo (number 12))
53 (bar (string "hello")))))
55 (parse "\"\\x12\";" ; Latin-1 escape in string literal
57 (parse "\"\\u1234\";" ; Unicode escape in string literal
59 (parse "function foo(x) { }" ; empty function body
60 '(var (foo (lambda (x) (begin)))))
61 (parse ".123;" '(number 0.123))
62 (parse "0xff;" '(number 255)))
65 (define-syntax ecompile
70 (compile (call-with-input-string expression read-ecmascript)
73 ((_ expression expected)
76 (compile (call-with-input-string expression read-ecmascript)
80 (with-test-prefix "compiler"
83 (ecompile "if (3 > 2) true; else false;" #t)
85 (ecompile "\"hello\";" "hello")
86 (ecompile "var test = { bar: 1 };")
89 ;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });"
93 ;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>.
95 (ecompile "42 + \" good times!\";"
97 (ecompile "[0,1,2,3,4,5].length * 7;"