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
+41 -30
View File
@@ -1,51 +1,62 @@
#lang racket/base
(require rackunit
"../main.rkt"
(require "../main.rkt"
"jsmaker-test-framework.rkt")
(provide program-tests)
(define ordinary-let-program
(string-append
(js (define (ordinary-let x)
(js (define (ordinaryLet x)
(let ([x 1] [y x])
(return y))))
"console.log(ordinary_let(99));\n"))
"\nconsole.log(JSON.stringify(ordinaryLet(99)));\n"))
(run-js-if-available 'ordinary-let-runtime ordinary-let-program "99")
(define sequential-let-program
(define let-star-program
(string-append
(js (define (sequential-let x)
(js (define (sequentialLet x)
(let* ([x 1] [y x])
(return y))))
"console.log(sequential_let(99));\n"))
"\nconsole.log(JSON.stringify(sequentialLet(99)));\n"))
(run-js-if-available 'let-star-runtime let-star-program "1")
(define named-let-program
(string-append
(js (define (sum-to n)
(js (define (sumTo n)
(let loop ([i 0] [acc 0])
(if (> i n)
(return acc)
(loop (+ i 1) (+ acc i))))))
"console.log(sum_to(10));\n"))
"\nconsole.log(JSON.stringify(sumTo(10)));\n"))
(run-js-if-available 'named-let-runtime named-let-program "55")
(define program-tests
(test-suite
"generated JavaScript program behavior"
(test-case "ordinary let uses parallel Racket binding semantics"
(check-js-contains? ordinary-let-program "const")
(check-js-contains? ordinary-let-program "let x")
(when (node-available?)
(check-equal? (run-js/trimmed ordinary-let-program) "99")))
(test-case "let* uses sequential binding semantics"
(when (node-available?)
(check-equal? (run-js/trimmed sequential-let-program) "1")))
(test-case "named let compiles to a while loop with parallel updates"
(check-js-contains? named-let-program "while (true)")
(check-js-contains? named-let-program "continue;")
(when (node-available?)
(check-equal? (run-js/trimmed named-let-program) "55")))))
(define ref-program
(string-append
(js (define (at xs i)
(return (js-ref xs i))))
"\nconsole.log(JSON.stringify(at([10,20,30],1)));\n"))
(run-js-if-available 'ref-runtime ref-program "20")
(module+ test
(require rackunit/text-ui)
(run-tests program-tests))
(define ref-set-program
(string-append
(js (define (put xs i value)
(set! (js-ref xs i) value)
(return xs)))
"\nconsole.log(JSON.stringify(put([1,2,3],1,9)));\n"))
(run-js-if-available 'ref-set-runtime ref-set-program "[1,9,3]")
(define ref-string-key-program
(string-append
(js (define (nameOf obj)
(return (js-ref obj "name"))))
"\nconsole.log(JSON.stringify(nameOf({name:\"Ada\"})));\n"))
(run-js-if-available 'ref-string-key-runtime ref-string-key-program "\"Ada\"")
(define lambda-program
(string-append
(js (define (makeAdder x)
(return (lambda (y) (return (+ x y))))))
"\nconsole.log(JSON.stringify(makeAdder(2)(3)));\n"))
(run-js-if-available 'lambda-runtime lambda-program "5")
(module+ main
(test-summary 'jsmaker-program-regression))