This commit is contained in:
2026-06-08 13:21:57 +02:00
parent 823130e3ac
commit 8bee76328b
23 changed files with 734 additions and 382 deletions
+71 -28
View File
@@ -1,35 +1,78 @@
#lang racket/base
(require rackunit
racket/runtime-path
"../main.rkt"
(require "../main.rkt"
"jsmaker-test-framework.rkt")
(provide regression-tests)
(check-public-api)
(define-runtime-path main-module "../main.rkt")
(define simple-function
(js (define (add1 x) (return (+ x 1)))))
(check-contains 'simple-function "function add1(x)" simple-function)
(check-contains 'simple-function-return "return x + 1;" simple-function)
(define regression-tests
(test-suite
"core js macro output"
(test-case "the public API exports js only"
(check-exn exn:fail? (lambda () (dynamic-require main-module 'js1)))
(check-exn exn:fail? (lambda () (dynamic-require main-module 'js/expression))))
(test-case "arithmetic and boolean expressions can be emitted as statements"
(check-js-equal? (js (+ 1 2)) "1 + 2;\n")
(check-js-equal? (js (and a b)) "a && b;\n")
(check-js-equal? (js (not ready)) "!(ready);\n"))
(test-case "value and function definitions"
(check-js-equal? (js (define answer 42)) "let answer = 42;\n")
(check-js-contains?
(js (define (square x) (return (* x x))))
"function square(x)"))
(test-case "conditionals and begin blocks"
(define out (js (if (> x 0) (return x) (return 0))))
(check-js-contains? out "if (x > 0)")
(check-js-contains? out "return x;")
(check-js-contains? (js (begin (set! x 1) (return x))) "x = 1;"))))
(define escaped-string
(js (define (message) (return "regel 1\nregel 2 \"ok\""))))
(check-contains 'string-newline "regel 1\\nregel 2" escaped-string)
(check-contains 'string-quote "\\\"ok\\\"" escaped-string)
(module+ test
(require rackunit/text-ui)
(run-tests regression-tests))
(define list-program
(js (define (values) (return (list 1 "a" #t #f)))))
(check-contains 'list-literal "return [1, \"a\", true, false];" list-program)
(define cons-program
(js (define (prepend xs) (return (cons 1 xs)))))
(check-contains 'cons-generation "[1].concat(xs)" cons-program)
(define send-program
(js (define (unique xs) (return (send Array from (new Set xs))))))
(check-contains 'send-generation "Array.from(new Set(xs))" send-program)
(define dot-set-program
(js (define (setHtml el html) (set! (js-dot el innerHTML) html) (return (js-dot el innerHTML)))))
(check-contains 'dot-set "el.innerHTML = html;" dot-set-program)
(check-contains 'dot-return "return el.innerHTML;" dot-set-program)
(define ref-program
(js (define (at xs i) (return (js-ref xs i)))))
(check-contains 'ref-variable-index "return xs[i];" ref-program)
(define ref-string-key-program
(js (define (nameOf obj) (return (js-ref obj "name")))))
(check-contains 'ref-string-key "return obj[\"name\"];" ref-string-key-program)
(define ref-set-program
(js (define (put xs i value) (set! (js-ref xs i) value) (return xs))))
(check-contains 'ref-set "xs[i] = value;" ref-set-program)
(define ref-nested-program
(js (define (nested matrix r c) (return (js-ref matrix r c)))))
(check-contains 'ref-nested "return matrix[r][c];" ref-nested-program)
(define ordinary-let
(js (define (ordinaryLet x)
(let ([x 1] [y x])
(return y)))))
(check-matches 'ordinary-let-temp #rx"const .* = 1;" ordinary-let)
(check-contains 'ordinary-let-inner "{\nlet x =" ordinary-let)
(check-contains 'ordinary-let-inner-y "\nlet y =" ordinary-let)
(check-contains 'ordinary-let-return "return y;" ordinary-let)
(define let-star
(js (define (sequentialLet x)
(let* ([x 1] [y x])
(return y)))))
(check-contains 'let-star-x "let x = 1;" let-star)
(check-contains 'let-star-y "let y = x;" let-star)
(define named-let
(js (define (sumTo n)
(let loop ([i 0] [acc 0])
(if (> i n)
(return acc)
(loop (+ i 1) (+ acc i)))))))
(check-contains 'named-let-while "while (true)" named-let)
(check-contains 'named-let-continue "continue;" named-let)
(check-matches 'named-let-parallel-update #rx"const .* = i \\+ 1;" named-let)
(module+ main
(test-summary 'jsmaker-regression))