summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2010-03-31 00:09:40 +0200
committerLudovic Courtès <ludo@gnu.org>2010-03-31 00:42:01 +0200
commitbd7131d3adf60b98837bd8bc3711ec7cf9069569 (patch)
tree43ea9afcfcdf27da6ec7899ead9c69a81e235ee5
parent2115b8eb408d8f965d3c0d28cb087e55f0eb5daf (diff)
downloadguile-bd7131d3adf60b98837bd8bc3711ec7cf9069569.tar.gz
Add rudimentary ECMAScript tests.
* test-suite/Makefile.am (SCM_TESTS): Add `tests/ecmascript.test'. * test-suite/tests/ecmascript.test: New file.
-rw-r--r--test-suite/Makefile.am1
-rw-r--r--test-suite/tests/ecmascript.test74
2 files changed, 75 insertions, 0 deletions
diff --git a/test-suite/Makefile.am b/test-suite/Makefile.am
index 94789d347..ad8a8f771 100644
--- a/test-suite/Makefile.am
+++ b/test-suite/Makefile.am
@@ -34,6 +34,7 @@ SCM_TESTS = tests/alist.test \
tests/common-list.test \
tests/control.test \
tests/continuations.test \
+ tests/ecmascript.test \
tests/elisp.test \
tests/elisp-compiler.test \
tests/elisp-reader.test \
diff --git a/test-suite/tests/ecmascript.test b/test-suite/tests/ecmascript.test
new file mode 100644
index 000000000..c5ef34438
--- /dev/null
+++ b/test-suite/tests/ecmascript.test
@@ -0,0 +1,74 @@
+;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*-
+;;;;
+;;;; Copyright (C) 2010 Free Software Foundation, Inc.
+;;;;
+;;;; This library is free software; you can redistribute it and/or
+;;;; modify it under the terms of the GNU Lesser General Public
+;;;; License as published by the Free Software Foundation; either
+;;;; version 3 of the License, or (at your option) any later version.
+;;;;
+;;;; This library is distributed in the hope that it will be useful,
+;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+;;;; Lesser General Public License for more details.
+;;;;
+;;;; You should have received a copy of the GNU Lesser General Public
+;;;; License along with this library; if not, write to the Free Software
+;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+(define-module (test-ecmascript)
+ #:use-module (test-suite lib)
+ #:use-module (language ecmascript parse)
+ #:use-module ((system base compile) #:select (compile)))
+
+
+(define (eread str)
+ (call-with-input-string str read-ecmascript))
+
+(define-syntax parse
+ (syntax-rules ()
+ ((_ expression expected)
+ (pass-if expression
+ (equal? expected (eread expression))))))
+
+(with-test-prefix "parser"
+
+ (parse "true;" 'true)
+ (parse "2 + 2;" '(+ (number 2) (number 2)))
+ (parse "\"hello\";" '(string "hello"))
+ (parse "function square(x) { return x * x; }"
+ '(var (square (lambda (x) (return (* (ref x) (ref x)))))))
+ (parse "document.write('Hello, world!');"
+ '(call (pref (ref document) write) ((string "Hello, world!"))))
+ (parse "var x = { foo: 12, bar: \"hello\" };"
+ '(begin (var (x (object (foo (number 12))
+ (bar (string "hello")))))
+ (begin))))
+
+
+(define-syntax ecompile
+ (syntax-rules ()
+ ((_ expression expected)
+ (pass-if expression
+ (equal? expected
+ (compile (call-with-input-string expression read-ecmascript)
+ #:from 'ecmascript
+ #:to 'value))))))
+
+(with-test-prefix "compiler"
+
+ (ecompile "true;" #t)
+ (ecompile "2 + 2;" 4)
+ (ecompile "\"hello\";" "hello")
+
+ ;; FIXME: Broken!
+ ;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });"
+ ;; '(1 4 9 16))
+
+ ;; Examples from
+ ;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>.
+
+ (ecompile "42 + \" good times!\";"
+ "42 good times!")
+ (ecompile "[0,1,2,3,4,5].length * 7;"
+ 42))