Commit | Line | Data |
---|---|---|
bd7131d3 LC |
1 | ;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; | |
99d716b6 | 3 | ;;;; Copyright (C) 2010, 2011, 2013 Free Software Foundation, Inc. |
bd7131d3 LC |
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) | |
6dce942c | 26 | (call-with-input-string str read-ecmascript)) |
e92f113a | 27 | (define (eread/1 str) |
6dce942c | 28 | (call-with-input-string str read-ecmascript/1)) |
bd7131d3 LC |
29 | |
30 | (define-syntax parse | |
31 | (syntax-rules () | |
32 | ((_ expression expected) | |
e92f113a AW |
33 | (begin |
34 | (pass-if expression | |
35 | (equal? expected (eread expression))) | |
36 | (pass-if expression | |
37 | (equal? expected (eread/1 expression))))))) | |
bd7131d3 LC |
38 | |
39 | (with-test-prefix "parser" | |
40 | ||
41 | (parse "true;" 'true) | |
42 | (parse "2 + 2;" '(+ (number 2) (number 2))) | |
90cfcf8f | 43 | (parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace |
bd7131d3 LC |
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"))))) | |
b1846b7f NL |
52 | (begin))) |
53 | (parse "\"\\x12\";" ; Latin-1 escape in string literal | |
54 | '(string "\x12")) | |
55 | (parse "\"\\u1234\";" ; Unicode escape in string literal | |
90cfcf8f LC |
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))) | |
bd7131d3 LC |
61 | |
62 | \f | |
63 | (define-syntax ecompile | |
64 | (syntax-rules () | |
8891bd1b NL |
65 | ((_ expression) |
66 | (pass-if expression | |
67 | (not (not | |
68 | (compile (call-with-input-string expression read-ecmascript) | |
69 | #:from 'ecmascript | |
70 | #:to 'value))))) | |
bd7131d3 LC |
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) | |
ed7c4a5d | 81 | (ecompile "if (3 > 2) true; else false;" #t) |
bd7131d3 LC |
82 | (ecompile "2 + 2;" 4) |
83 | (ecompile "\"hello\";" "hello") | |
8891bd1b | 84 | (ecompile "var test = { bar: 1 };") |
bd7131d3 LC |
85 | |
86 | ;; FIXME: Broken! | |
87 | ;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });" | |
88 | ;; '(1 4 9 16)) | |
89 | ||
90 | ;; Examples from | |
91 | ;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>. | |
92 | ||
93 | (ecompile "42 + \" good times!\";" | |
94 | "42 good times!") | |
95 | (ecompile "[0,1,2,3,4,5].length * 7;" | |
96 | 42)) |