Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / tests / ecmascript.test
1 ;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*-
2 ;;;;
3 ;;;; Copyright (C) 2010, 2011, 2013 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 \f
25 (define (eread str)
26 (with-fluids ((%default-port-encoding "utf-8"))
27 (call-with-input-string str read-ecmascript)))
28 (define (eread/1 str)
29 (with-fluids ((%default-port-encoding "utf-8"))
30 (call-with-input-string str read-ecmascript/1)))
31
32 (define-syntax parse
33 (syntax-rules ()
34 ((_ expression expected)
35 (begin
36 (pass-if expression
37 (equal? expected (eread expression)))
38 (pass-if expression
39 (equal? expected (eread/1 expression)))))))
40
41 (with-test-prefix "parser"
42
43 (parse "true;" 'true)
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")))))
54 (begin)))
55 (parse "\"\\x12\";" ; Latin-1 escape in string literal
56 '(string "\x12"))
57 (parse "\"\\u1234\";" ; Unicode escape in string literal
58 '(string "\u1234"))
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)))
63
64 \f
65 (define-syntax ecompile
66 (syntax-rules ()
67 ((_ expression)
68 (pass-if expression
69 (not (not
70 (compile (call-with-input-string expression read-ecmascript)
71 #:from 'ecmascript
72 #:to 'value)))))
73 ((_ expression expected)
74 (pass-if expression
75 (equal? expected
76 (compile (call-with-input-string expression read-ecmascript)
77 #:from 'ecmascript
78 #:to 'value))))))
79
80 (with-test-prefix "compiler"
81
82 (ecompile "true;" #t)
83 (ecompile "if (3 > 2) true; else false;" #t)
84 (ecompile "2 + 2;" 4)
85 (ecompile "\"hello\";" "hello")
86 (ecompile "var test = { bar: 1 };")
87
88 ;; FIXME: Broken!
89 ;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });"
90 ;; '(1 4 9 16))
91
92 ;; Examples from
93 ;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>.
94
95 (ecompile "42 + \" good times!\";"
96 "42 good times!")
97 (ecompile "[0,1,2,3,4,5].length * 7;"
98 42))