Skip to content

Instantly share code, notes, and snippets.

@shawwn
Created September 9, 2018 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shawwn/53c06cd30f29b064c1f6c66f7896f208 to your computer and use it in GitHub Desktop.
Save shawwn/53c06cd30f29b064c1f6c66f7896f208 to your computer and use it in GitHub Desktop.
#!/usr/bin/env lumen
(define passed 0)
(define failed 0)
(define tests ())
(define system (require 'system))
(define reader (load "reader.l"))
(define-macro test (x msg)
`(if (not ,x)
(do (set failed (+ failed 1))
(return ,msg))
(inc passed)))
(define equal? (a b)
(if (atom? a) (= a b)
(= (str a) (str b))))
(define-macro test= (a b)
(let-unique (x y)
`(let (,x ,a ,y ,b)
(print (str '(test= ,a ,b) ))
(test (equal? ,x ,y)
(cat "failed: expected " (str ,x) ", was " (str ,y) " for " (str '(test= ,a ,b) ))))))
(define-macro define-test (name rest: body)
`(add tests (list ',name (fn () ,@body))))
(define-global run ()
(each ((name f)) tests
(let result (f)
(when (string? result)
(print (cat " " name " " result)))))
(print (cat " " passed " passed, " failed " failed")))
(define-test reader
(let (read-file (get system 'read-file)
read (get reader 'read-string))
(test= nil (read ""))
(test= "nil" (read "nil"))
(test= "17" (read "17"))
(test= "1.5e-2" (read "1.5e-2"))
(test= "true" (read "true"))
(test= "false" (read "false"))
(test= 'hi (read "hi"))
(test= '"hi" (read "\"hi\""))
(test= "|hi|" (read "|hi|"))
(test= "(1 2)" (read "(1 2)"))
(test= "(1 (a))" (read "(1 (a))"))
(test= "'a'" (read "'a'"))
(test= "`a" (read "`a"))
(test= "`" (read "`,a"))
(test= "`" (read "`,@a"))
(test= "(1 2 a: 7)" (read "(1 2 a: 7)"))
(test= 1 (- -1))
(test= "0?" (read "0?"))
(test= "0!" (read "0!"))
(test= "0." (read "0."))))
(define-test read-more
(let read (get reader 'read-string)
(test= "17" (read "17" true))
(let more ()
(test= "(open" (read "(open" more))
(test= more (read "\"unterminated " more))
(test= "|identifier" (read "|identifier" more))
(test= "'(a b c" (read "'(a b c" more))
(test= "`(a b c" (read "`(a b c" more))
(test= "`(a b " (read "`(a b ,(z" more))
(test= "`" (read "`\"biz" more))
(test= "'" (read "'\"boz" more)))
(let ((ok e) (guard (read "\"open")))
(test= false ok)
(test= "Expected \" at 5" (get e 'message)))))
(run)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment