From 4758802810f7e4faa1d1eb32f0c3bcd26b7b6f48 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Tue, 20 Oct 2026 11:48:02 +0300 Subject: [PATCH] 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. --- reader.scm | 5 +-- tests/codegen.scm | 68 +---------------------------------- tests/encapsulation.scm | 3 +- tests/line-directives.scm | 25 ++++++------- tests/pipeline.scm | 75 +++++++++++++++++++++++++++++++++++++++ tests/run.scm | 1 + 6 files changed, 89 insertions(+), 88 deletions(-) create mode 100644 tests/pipeline.scm diff --git a/reader.scm b/reader.scm index a3c058e3..98310125 100644 --- a/reader.scm +++ b/reader.scm @@ -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)) diff --git a/tests/codegen.scm b/tests/codegen.scm index d3cd6320..2e1f0795 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -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 diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index d4d9a672..6dcc47a2 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -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) diff --git a/tests/line-directives.scm b/tests/line-directives.scm index 7a82100a..b27286e3 100644 --- a/tests/line-directives.scm +++ b/tests/line-directives.scm @@ -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)))))))) diff --git a/tests/pipeline.scm b/tests/pipeline.scm new file mode 100644 index 00000000..253f5115 --- /dev/null +++ b/tests/pipeline.scm @@ -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)) diff --git a/tests/run.scm b/tests/run.scm index 0065c04a..b31b48a7 100644 --- a/tests/run.scm +++ b/tests/run.scm @@ -1,6 +1,7 @@ (import test) +(include-relative "pipeline.scm") (include-relative "basic.scm") (include-relative "semen.scm") (include-relative "reader.scm")