From 99a41b9d04dd5fc7794e97af27dae2f281406091 Mon Sep 17 00:00:00 2001 From: alex-eg Date: Wed, 14 Oct 2026 20:09:17 +0300 Subject: [PATCH] share reading a test source and catching its error encapsulation.scm had copies of both from codegen.scm. --- tests/codegen.scm | 24 +++++++++++++++--------- tests/encapsulation.scm | 13 ++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/codegen.scm b/tests/codegen.scm index 6e8855f..552c552 100644 --- a/tests/codegen.scm +++ b/tests/codegen.scm @@ -15,12 +15,15 @@ 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 (with-input-from-string source - (lambda () - (parameterize ((current-source-file "codegen.sex")) - (parse-all (current-input-port))))))) + (let ((forms (read-source source))) (with-output-to-string (lambda () (parameterize ((sex-line-directives 'none)) @@ -37,17 +40,20 @@ (define (emits? source fragment) (and (string-contains (silently (lambda () (sex->c source))) fragment) #t)) -(define (error-message source) - "Compile SOURCE and return the error text as the user sees it -- -message plus arguments, the way CHICKEN prints it -- or #f if SOURCE -compiles." +(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 (sex->c source) #f))) + (begin (thunk) #f))) + +(define (error-message source) + "Compile SOURCE and return its error text, or #f if it compiles." + (failure-message (lambda () (sex->c source)))) (define (reports? source fragment) (let ((m (error-message source))) diff --git a/tests/encapsulation.scm b/tests/encapsulation.scm index a9dde45..a83e996 100644 --- a/tests/encapsulation.scm +++ b/tests/encapsulation.scm @@ -5,23 +5,18 @@ ;;; is checked here is what the compiler refuses, and that it refuses ;;; it with the name of whatever is wrong. ;;; -;;; `sex->c', `emits?', `reports?' and `in-fn' come from codegen.scm, -;;; which run.scm includes first. +;;; `sex->c', `read-source', `failure-message', `emits?', `reports?' +;;; and `in-fn' come from codegen.scm, which run.scm includes first. (import sex-modules) (define (interface-of source) "The declarations SOURCE, compiled as a module, hands its importers ahead of its pub forms." - (check-public-interface - (with-input-from-string source - (lambda () - (parameterize ((current-source-file "codegen.sex")) - (parse-all (current-input-port))))))) + (check-public-interface (read-source source))) (define (interface-reports? source fragment) - (let ((m (handle-exceptions e ((condition-property-accessor 'exn 'message) e) - (begin (interface-of source) #f)))) + (let ((m (failure-message (lambda () (interface-of source))))) (and m (string-contains m fragment) #t))) (define (ops-src . body)