1
0
forked from alex-eg/sex

read the unit tests' sources from a file, the way sexc does

The reader exported parse-all only so the tests could give a source the
file name its locations carry; read-from-file gives it one, through the
reader's own interface. The pipeline helpers move out of codegen.scm for
every suite to use.
This commit is contained in:
2026-10-20 11:48:02 +03:00
parent 8488a58be1
commit 4758802810
6 changed files with 89 additions and 88 deletions

View File

@@ -27,10 +27,7 @@
read-raw-forms
current-features
platform-features
;; for the tests
parse-all)
platform-features)
;;; Sentinels for structural tokens
(define close-paren (list '%close-paren))

View File

@@ -6,66 +6,7 @@
;;; the existing suite, because both need an operand shape that no
;;; earlier test program happened to use.
(import (chicken condition)
(chicken port)
(chicken string)
srfi-13
fmt-c-writer
reader
semen
utils)
(define (read-source source)
(with-input-from-string source
(lambda ()
(parameterize ((current-source-file "codegen.sex"))
(parse-all (current-input-port))))))
(define (sex->c source)
"Compile SOURCE, a string of Sex, and return the generated C."
(let ((forms (read-source source)))
(with-output-to-string
(lambda ()
(parameterize ((sex-line-directives 'none))
(emit-c (semen-process forms)))))))
;;; Warnings are what `warns?' is for; a test that happens to trip one
;;; should not print it
(define (silently thunk)
"THUNK's value, with whatever it wrote to stderr dropped."
(let ((value #f))
(with-error-output-to-string (lambda () (set! value (thunk))))
value))
(define (emits? source fragment)
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
(define (failure-message thunk)
"The error THUNK raises as the user sees it -- message plus arguments,
the way CHICKEN prints it -- or #f if it raises none."
(handle-exceptions e
(with-output-to-string
(lambda ()
(display ((condition-property-accessor 'exn 'message) e))
(for-each (lambda (a) (display " ") (write a))
((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f)))
(define (fails-with? thunk fragment)
(let ((m (failure-message thunk)))
(and m (string-contains m fragment) #t)))
(define (reports? source fragment)
(fails-with? (lambda () (sex->c source)) fragment))
(define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
whitespace aside, THEN?"
(let* ((c (silently (lambda () (sex->c source))))
(at (string-contains c first)))
(and at
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
(and next (string-prefix? then (substring c next)))))))
(import srfi-13)
(define (in-fn body)
(string-append "(fn f ((a int) (b int)) void " body ")"))
@@ -80,13 +21,6 @@ whitespace aside, THEN?"
" (var p (struct point)) (var pp (* struct point) (& p))"
" (var l (struct line)) " body ")"))
(define (warnings source)
"What compiling SOURCE writes to stderr."
(with-error-output-to-string (lambda () (sex->c source))))
(define (warns? source fragment)
(and (string-contains (warnings source) fragment) #t))
(test-group "codegen"
;; A case body shared the switch's scope, so two cases could not

View File

@@ -5,8 +5,7 @@
;;; is checked here is what the compiler refuses, and that it refuses
;;; it with the name of whatever is wrong.
;;;
;;; `sex->c', `read-source', `fails-with?', `emits?', `reports?'
;;; and `in-fn' come from codegen.scm, which run.scm includes first.
;;; `in-fn' comes from codegen.scm, which run.scm includes first.
(import sex-modules)

View File

@@ -17,14 +17,13 @@
;;; get the true source line) and in the C output (to get the line the
;;; directives claim). The two must agree.
(import (chicken port)
(import (chicken pathname)
(chicken port)
(chicken string)
srfi-1
srfi-13
fmt-c-writer
reader
semen
utils)
semen)
;;; ------------------------------------------------------------------
;;; Fixture. Line numbers are in the trailing comments; keep them
@@ -98,15 +97,9 @@
;;; Pipeline and #line accounting
(define (compile-to-c source)
"Run reader -> semen -> writer on SOURCE, returning the generated C.
parse-all is called directly rather than through read-raw-forms so the
fixture can be given a file name: a location carries one, and the
file is what makes an imported module report itself rather than the unit
that imported it."
(let ((forms (with-input-from-string source
(lambda ()
(parameterize ((current-source-file "fixture.sex"))
(parse-all (current-input-port)))))))
"Run reader -> semen -> writer on SOURCE, read from fixture.sex,
returning the generated C."
(let ((forms (read-source source "fixture.sex")))
(with-output-to-string
(lambda () (emit-c (semen-process forms))))))
@@ -247,6 +240,8 @@ after that is one more. Lines before the first directive get #f."
;; form was read from.
(test-group "file name"
(test "every directive names the fixture"
(list "\"fixture.sex\"")
(list "fixture.sex")
(delete-duplicates
(filter values (map directive-file (split-lines c-out)))))))
(map (lambda (quoted)
(pathname-strip-directory (string-trim-both quoted #\")))
(filter values (map directive-file (split-lines c-out))))))))

75
tests/pipeline.scm Normal file
View File

@@ -0,0 +1,75 @@
;;; Sex source in, C or the error it raises out, the way sexc runs
;;; the pipeline: reader -> semen -> writer. The suites after this one
;;; check the compiler through these.
(import (chicken condition)
(chicken file)
(chicken pathname)
(chicken port)
srfi-13
fmt-c-writer
reader
semen)
(define (read-source source #!optional (name "codegen.sex"))
"The forms of SOURCE, read from a file NAME, which is what the
locations then name: (read-source \"(f)\") reads (f) from codegen.sex."
(let* ((dir (create-temporary-directory))
(file (make-pathname dir name)))
(with-output-to-file file (lambda () (display source)))
(let ((forms (read-from-file file)))
(delete-directory dir #t)
forms)))
(define (sex->c source)
"Compile SOURCE, a string of Sex, and return the generated C."
(let ((forms (read-source source)))
(with-output-to-string
(lambda ()
(parameterize ((sex-line-directives 'none))
(emit-c (semen-process forms)))))))
;;; Warnings are what `warns?' is for; a test that happens to trip one
;;; should not print it
(define (silently thunk)
"THUNK's value, with whatever it wrote to stderr dropped."
(let ((value #f))
(with-error-output-to-string (lambda () (set! value (thunk))))
value))
(define (emits? source fragment)
(and (string-contains (silently (lambda () (sex->c source))) fragment) #t))
(define (failure-message thunk)
"The error THUNK raises as the user sees it -- message plus arguments,
the way CHICKEN prints it -- or #f if it raises none."
(handle-exceptions e
(with-output-to-string
(lambda ()
(display ((condition-property-accessor 'exn 'message) e))
(for-each (lambda (a) (display " ") (write a))
((condition-property-accessor 'exn 'arguments) e))))
(begin (thunk) #f)))
(define (fails-with? thunk fragment)
(let ((m (failure-message thunk)))
(and m (string-contains m fragment) #t)))
(define (reports? source fragment)
(fails-with? (lambda () (sex->c source)) fragment))
(define (followed-by? source first then)
"Is FIRST in the C compiled from SOURCE, and the next thing past it,
whitespace aside, THEN?"
(let* ((c (silently (lambda () (sex->c source))))
(at (string-contains c first)))
(and at
(let ((next (string-skip c char-whitespace? (+ at (string-length first)))))
(and next (string-prefix? then (substring c next)))))))
(define (warnings source)
"What compiling SOURCE writes to stderr."
(with-error-output-to-string (lambda () (sex->c source))))
(define (warns? source fragment)
(and (string-contains (warnings source) fragment) #t))

View File

@@ -1,6 +1,7 @@
(import
test)
(include-relative "pipeline.scm")
(include-relative "basic.scm")
(include-relative "semen.scm")
(include-relative "reader.scm")