1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-02 13:00:26 +02:00
guile/test-suite/tests/ecmascript.test
Mark H Weaver 6dce942c46 String ports use UTF-8; ignore %default-port-encoding.
* libguile/strports.c (scm_mkstrport): Use UTF-8; ignore
  %default-port-encoding.  Rename 'str_len' and 'c_pos' to
  'num_bytes' and 'c_byte_pos'.  Interpret 'pos' argument
  as a character index instead of a byte index.

* module/ice-9/boot-9.scm (%cond-expand-features): Add srfi-6 to the
  list of core features.

* module/srfi/srfi-6.scm (open-input-string, open-output-string): Simply
  re-export these, since the core versions are now compliant.

* doc/ref/api-io.texi (String Ports): Remove text that describes
  non-compliant behavior of string ports with regard to encoding.

* doc/ref/srfi-modules.texi (SRFI-0): Add srfi-6 to the list of
  core features.
  (SRFI-6): Remove text that mentions non-compliant behavior of
  core string ports.

* module/ice-9/format.scm (format):
* module/ice-9/pretty-print.scm (truncated-print):
* module/rnrs/io/ports.scm (open-string-input-port,
  open-string-output-port):
* test-suite/test-suite/lib.scm (format-test-name):
* test-suite/tests/chars.test ("combining accent is pretty-printed",
  "combining X is pretty-printed"):
* test-suite/tests/ecmascript.test (eread, eread/1):
* test-suite/tests/rdelim.test:
* test-suite/tests/reader.test (read-string):
* test-suite/tests/regexp.test:
* test-suite/tests/srfi-105.test (read-string): Don't set
  %default-port-encoding before creating string ports.

* benchmark-suite/benchmarks/ports.bm (%latin1-port): Use
  'set-port-encoding!' to set the string port encoding.
  (%utf8/ascii-port, %utf8/wide-port, "rdelim"): Don't set
  %default-port-encoding before creating string ports.

* test-suite/tests/r6rs-ports.test ("lookahead-u8 non-ASCII"): Don't set
  %default-port-encoding before creating string ports.
  ("put-bytevector with UTF-16 string port", "put-bytevector with
  wrong-encoding string port"): Use 'set-port-encoding!' to set the
  string port encoding.

* test-suite/tests/print.test (tprint): Use 'set-port-encoding!' to set
  the string port encoding.
  ("truncated-print"): Use 'pass-if-equal'.

* test-suite/tests/ports.test ("encoding failure leads to exception",
  "%default-port-encoding is honored", "peek-char [latin-1]", "peek-char
  [utf-8]", "peek-char [utf-16]"): Remove tests.
  ("%default-port-encoding is ignored", "peek-char"): Add tests.
  ("suitable encoding [latin-1]", "suitable encoding [latin-3]",
  "wrong encoding, error", "wrong encoding, substitute",
  "wrong encoding, escape"): Use 'set-port-encoding!' to set the
  string port encoding.
  ("%default-port-encoding, wrong encoding"): Rewrite to use
  a file port instead of a string port.
2013-08-07 01:22:22 -04:00

96 lines
3.4 KiB
Scheme
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

;;;; ecmascript.test --- ECMAScript. -*- mode: scheme; coding: utf-8; -*-
;;;;
;;;; Copyright (C) 2010, 2011, 2013 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 (eread/1 str)
(call-with-input-string str read-ecmascript/1))
(define-syntax parse
(syntax-rules ()
((_ expression expected)
(begin
(pass-if expression
(equal? expected (eread expression)))
(pass-if expression
(equal? expected (eread/1 expression)))))))
(with-test-prefix "parser"
(parse "true;" 'true)
(parse "2 + 2;" '(+ (number 2) (number 2)))
(parse "2\xa0+2;" '(+ (number 2) (number 2))) ; U+00A0 is whitespace
(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)))
(parse "\"\\x12\";" ; Latin-1 escape in string literal
'(string "\x12"))
(parse "\"\\u1234\";" ; Unicode escape in string literal
'(string "\u1234"))
(parse "function foo(x) { }" ; empty function body
'(var (foo (lambda (x) (begin)))))
(parse ".123;" '(number 0.123))
(parse "0xff;" '(number 255)))
(define-syntax ecompile
(syntax-rules ()
((_ expression)
(pass-if expression
(not (not
(compile (call-with-input-string expression read-ecmascript)
#:from 'ecmascript
#:to 'value)))))
((_ 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 "if (3 > 2) true; else false;" #t)
(ecompile "2 + 2;" 4)
(ecompile "\"hello\";" "hello")
(ecompile "var test = { bar: 1 };")
;; 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))