2010-03-31 00:09:40 +02:00
|
|
|
|
;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*-
|
|
|
|
|
|
;;;;
|
2011-01-03 02:22:35 -05:00
|
|
|
|
;;;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
|
2010-03-31 00:09:40 +02:00
|
|
|
|
;;;;
|
|
|
|
|
|
;;;; 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))
|
2010-11-18 12:26:20 +01:00
|
|
|
|
(define (eread/1 str)
|
|
|
|
|
|
(call-with-input-string str read-ecmascript/1))
|
2010-03-31 00:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
(define-syntax parse
|
|
|
|
|
|
(syntax-rules ()
|
|
|
|
|
|
((_ expression expected)
|
2010-11-18 12:26:20 +01:00
|
|
|
|
(begin
|
|
|
|
|
|
(pass-if expression
|
|
|
|
|
|
(equal? expected (eread expression)))
|
|
|
|
|
|
(pass-if expression
|
|
|
|
|
|
(equal? expected (eread/1 expression)))))))
|
2010-03-31 00:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
(with-test-prefix "parser"
|
|
|
|
|
|
|
|
|
|
|
|
(parse "true;" 'true)
|
|
|
|
|
|
(parse "2 + 2;" '(+ (number 2) (number 2)))
|
2011-01-26 23:45:13 +01:00
|
|
|
|
(parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace
|
2010-03-31 00:09:40 +02:00
|
|
|
|
(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")))))
|
2011-01-11 16:32:17 -05:00
|
|
|
|
(begin)))
|
|
|
|
|
|
(parse "\"\\x12\";" ; Latin-1 escape in string literal
|
|
|
|
|
|
'(string "\x12"))
|
|
|
|
|
|
(parse "\"\\u1234\";" ; Unicode escape in string literal
|
2011-01-26 23:45:13 +01:00
|
|
|
|
'(string "\u1234"))
|
|
|
|
|
|
(parse "function foo(x) { }" ; empty function body
|
|
|
|
|
|
'(var (foo (lambda (x) (begin)))))
|
|
|
|
|
|
(parse ".123;" '(number 0.123))
|
|
|
|
|
|
(parse "0xff;" '(number 255)))
|
2010-03-31 00:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax ecompile
|
|
|
|
|
|
(syntax-rules ()
|
2011-01-03 02:22:35 -05:00
|
|
|
|
((_ expression)
|
|
|
|
|
|
(pass-if expression
|
|
|
|
|
|
(not (not
|
|
|
|
|
|
(compile (call-with-input-string expression read-ecmascript)
|
|
|
|
|
|
#:from 'ecmascript
|
|
|
|
|
|
#:to 'value)))))
|
2010-03-31 00:09:40 +02:00
|
|
|
|
((_ 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")
|
2011-01-03 02:22:35 -05:00
|
|
|
|
(ecompile "var test = { bar: 1 };")
|
2010-03-31 00:09:40 +02:00
|
|
|
|
|
|
|
|
|
|
;; 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))
|