Login
7 branches 0 tags
Ben (X13/Arch) WIP: stackless 2218ce7 3 years ago 763 Commits
nujel / stdlib_modules / test.nuj
;;;; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
;;;; This project uses the MIT license, a copy should be included under /LICENSE
[def test-list   #nil]
[def test-count     0]
[def nujel-start    0]
[def success-count  0]
[def error-count    0]
[def print-errors  #t]
[def print-passes  #f]

[export add* [defn test/add* [result expr]
                   [set! test-list [cons [cons result expr] test-list]]
                   [set! test-count [+ test-count 1]]]]

[export add [defmacro test/add [result . expr]
                      "Add a test where EXPR must eval to RESULT"
                      `[~test/add* ~result ~[list 'quote [cons 'do expr]]]]]

[export reset! [defn reset! []
                     [set! test-list [cons 4 '[+ 3 1]]]
                     [set! test-count 1]]]

[defn display-results [description]
      "Prints the result Message"
      [random/seed-initialize!]
      [efmtln "{} {} - [{} / {}] in {} ms"
              [if [and [zero? error-count] [> test-count 0]]
                  [if [zero? success-count]
                      [ansi-yellow "?"]
                      [ansi-green "✓"]]
                  [ansi-red "✗"]]
              description
              [if [zero? success-count]
                  [ansi-yellow success-count]
                  [ansi-green success-count]]
              [if [zero? error-count]
                  [ansi-gray error-count]
                  [ansi-red error-count]]
              [- [time/milliseconds] nujel-start]]]

[defn test-success [res-should res-is expr]
      "Should be called after a test has finished successfully"
      [when print-passes
            [efmtln "{} == {}\r\n{}\r\n\r\n"
                    [ansi-green [string/write res-is]]
                    [ansi-green [string/write res-should]]
                    [string/write expr]]]
      [set! success-count [+ 1 success-count]]]

[defn test-failure [res-should res-is expr]
      "Should be called if EXPR does not equal RES"
      [when print-errors
            [pfmtln "{} != {}\r\n{}\r\n\r\n"
                    [ansi-red [string/write res-is]]
                    [ansi-green [string/write res-should]]
                    [string/write expr]]]
      [set! error-count [+ 1 error-count]]]

[export run-test! [defn run-test! [result rawexpr i]
                        "Tests that RAWEXPR evaluates to RESULT"
                        [try [fn [err]
                                 [display/error err]
                               [test-failure result [list :exception-caught err] rawexpr]]
                             [def expr [eval rawexpr]]
                             [if [equal? result expr]
                                 [test-success result expr rawexpr]
                                 [test-failure result expr rawexpr]]]]]

[export init! [defn init! [output-passes hide-errors]
                   "Initialize the testing framework"
                   [set! print-errors   [not [boolean hide-errors]]]
                   [set! print-passes   [boolean output-passes]]
                   [set! nujel-start    [time/milliseconds]]
                   [set! success-count  0]
                   [set! error-count    0]]]

[export finish! [defn finish! [description]
                      [display-results description]
                      [return error-count]]]

[export run [defn run [output-passes hide-errors]
                  "Run through all automated Tests"
                  [init!]
                  [doseq [cur-test test-list]
                         [run-test! [car cur-test]
                                    [cdr cur-test]]]
                  [finish! [fmt "{} {} [{System/OS} {System/Architecture}]" [ansi-blue ":core"] [ansi-rainbow "Nujel"]]]]]

[reset!]

[deftest #t [lambda? test/run]]
;; [error"Evaluating comments is a terrible Idea!"] [newline] [exit 2]