ecmascript.test 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. (define-module (test-ecmascript)
  19. #:use-module (test-suite lib)
  20. #:use-module (language ecmascript parse)
  21. #:use-module ((system base compile) #:select (compile)))
  22. (define (eread str)
  23. (call-with-input-string str read-ecmascript))
  24. (define (eread/1 str)
  25. (call-with-input-string str read-ecmascript/1))
  26. (define-syntax parse
  27. (syntax-rules ()
  28. ((_ expression expected)
  29. (begin
  30. (pass-if expression
  31. (equal? expected (eread expression)))
  32. (pass-if expression
  33. (equal? expected (eread/1 expression)))))))
  34. (with-test-prefix "parser"
  35. (parse "true;" 'true)
  36. (parse "2 + 2;" '(+ (number 2) (number 2)))
  37. (parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace
  38. (parse "\"hello\";" '(string "hello"))
  39. (parse "function square(x) { return x * x; }"
  40. '(var (square (lambda (x) (return (* (ref x) (ref x)))))))
  41. (parse "document.write('Hello, world!');"
  42. '(call (pref (ref document) write) ((string "Hello, world!"))))
  43. (parse "var x = { foo: 12, bar: \"hello\" };"
  44. '(begin (var (x (object (foo (number 12))
  45. (bar (string "hello")))))
  46. (begin)))
  47. (parse "\"\\x12\";" ; Latin-1 escape in string literal
  48. '(string "\x12"))
  49. (parse "\"\\u1234\";" ; Unicode escape in string literal
  50. '(string "\u1234"))
  51. (parse "function foo(x) { }" ; empty function body
  52. '(var (foo (lambda (x) (begin)))))
  53. (parse ".123;" '(number 0.123))
  54. (parse "0xff;" '(number 255)))
  55. (define-syntax ecompile
  56. (syntax-rules ()
  57. ((_ expression)
  58. (pass-if expression
  59. (not (not
  60. (compile (call-with-input-string expression read-ecmascript)
  61. #:from 'ecmascript
  62. #:to 'value)))))
  63. ((_ expression expected)
  64. (pass-if expression
  65. (equal? expected
  66. (compile (call-with-input-string expression read-ecmascript)
  67. #:from 'ecmascript
  68. #:to 'value))))))
  69. (with-test-prefix "compiler"
  70. (ecompile "true;" #t)
  71. (ecompile "if (3 > 2) true; else false;" #t)
  72. (ecompile "2 + 2;" 4)
  73. (ecompile "\"hello\";" "hello")
  74. (ecompile "var test = { bar: 1 };")
  75. ;; FIXME: Broken!
  76. ;; (ecompile "[1,2,3,4].map(function(x) { return x * x; });"
  77. ;; '(1 4 9 16))
  78. ;; Examples from
  79. ;; <http://wingolog.org/archives/2009/02/22/ecmascript-for-guile>.
  80. (ecompile "42 + \" good times!\";"
  81. "42 good times!")
  82. (ecompile "[0,1,2,3,4,5].length * 7;"
  83. 42))