Improve assert library

This commit is contained in:
retropikzel 2026-09-07 20:47:02 +03:00
parent f0a97e1ba4
commit ef3d49653d
6 changed files with 29 additions and 21 deletions

View File

@ -1,15 +1,4 @@
(define-syntax assert
(syntax-rules ()
((_ check body)
(let ((result body))
(when (not (check result))
(error "assertion error: " check body result))
result))))
(define-syntax asserts
(syntax-rules ()
((_ (check ...) body)
(let ((result body))
(when (not (check result)) (error "assertion error: " check body result)) ...
result))))
((_ check val err-msg)
(unless (check val) (error err-msg val)))))

View File

@ -1,7 +1,10 @@
;; SPDX-License-Identifier: LGPL
;; VERSION: 0.1.0
;; DESCRIPTION: Assert
;; ADDITIONAL_LIBRARIES: retropikzel/assert/pass.sld
(define-library
(retropikzel assert)
(import (scheme base)
(scheme write))
(export assert
asserts)
(import (scheme base))
(export assert)
(include "assert.scm"))

View File

@ -0,0 +1 @@
LGPL

View File

@ -0,0 +1 @@
(define-syntax assert (syntax-rules () ((_ check val err-msg) #t)))

View File

@ -0,0 +1,9 @@
;; SPDX-License-Identifier: LGPL
;; VERSION: 0.1.0
;; DESCRIPTION: Assert passtrough
(define-library
(retropikzel assert pass)
(import (scheme base))
(export assert)
(include "pass.scm"))

View File

@ -10,12 +10,17 @@
(test-begin "assert")
(test-equal 1 (assert number? 1))
(define (plus a b)
(assert integer? a "a must be integer")
(assert integer? b "b must be integer")
(+ a b))
(write (plus 5 5))
(newline)
(test-equal 10 (plus 5 5))
(test-equal 'error
(guard (condition (else 'error))
(assert number? "string")))
(test-equal 1 (asserts (number? (lambda (i) (= i 1))) 1))
(plus 5 'a)))
(test-end "assert")