Scheme工程的模块隔离与构建核验[#64]

instantiate-project为每个计划创建独立命名空间,只连接显式导出的binding;项目实例不会注册到宿主的全局模块表。不同项目实例有自己的本地定义,但外部导入的模块状态仍遵循Guile的共享规则。

(define instance (instantiate-project interpreter-project))
(define run (project-binding instance '(lp-example mce entry) 'run))
(run '((+ 1 2)))

核验案例复用11个值案例和5个异常案例的实际数据;输出的是读取时的数据快照,不导入Flora模块。这里核验多模块解释器本身,自解释仍由同源自解释案例检查。

program. Scheme工程复用的值与异常案例[mce-project-cases]

Defines: (examples errors)

Assembled in Scheme工程核验模块

Outputs: lp-example/mce/checks.scm

(define examples
  '((factorial
     ((define (factorial n) (if (= n 0) 1 (* n (factorial (- n 1)))))
      (factorial 6))
     720)
    (lexical
     ((define x 10)
      (define add-x (lambda (y) (+ x y)))
      ((lambda (x) (add-x 2)) 100))
     12)
    (closure
     ((define (make-adder x) (lambda (y) (+ x y)))
      (define add-three (make-adder 3))
      (add-three 4))
     7)
    (lazy-if ((if #t 7 missing)) 7)
    (truth ((if 0 1 2)) 1)
    (mapping ((map (lambda (x) (* x x)) '(1 2 3 4))) (1 4 9 16))
    (application ((apply + '(1 2 3))) 6)
    (named-let
     ((let loop ((n 5) (total 0))
        (if (= n 0) total (loop (- n 1) (+ total n)))))
     15)
    (let-star ((let* ((x 3) (y (+ x 4))) (* x y))) 21)
    (short-circuit ((list (and #f missing) (or 9 missing))) (#f 9))
    (conditional ((cond ((= 1 2) missing) ((= 2 2) 8) (else 0))) 8)))


(define errors
  '((missing (missing) mce-unbound)
    (arity (((lambda (x) x) 1 2)) mce-arity)
    (call ((1 2)) mce-not-procedure)
    (syntax ((if #t 1)) mce-syntax)
    (parameters ((lambda (x x) x)) mce-syntax)))

program. Scheme工程的独立核验入口[mce-project-checks]

Defines: (caught-key run-checks)

Assembled in Scheme工程核验模块

Outputs: lp-example/mce/checks.scm

Requires: run, examples, errors

(define (caught-key thunk)
       (catch #t (lambda () (thunk) 'returned)
         (lambda (key . arguments) key)))

(define (run-checks)
       (for-each
         (lambda (example)
           (unless (equal? (run (cadr example)) (caddr example))
             (error "Result mismatch" (car example)))) examples)
       (for-each
         (lambda (example)
           (unless (eq? (caught-key (lambda () (run (cadr example)))) (caddr example))
             (error "Exception mismatch" (car example)))) errors)
       (list (length examples) (length errors)))

example. Scheme工程的11项值与5项异常核验[mce-project-result]

(run-project-checks)

⇒ (11 5) · checked

program-check将已经核验的结果tree关联到被测计划,使各Module的Checks指向这项证据;它本身不增加执行,也不自动推断测试覆盖范围。