GameLib is a collection of libraries for creating applications in Cakelisp.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
3.4 KiB

;; To use: Simply include AutoTest.cake in your cakelisp command.
;; It will call any function starting with "test--" and report the results
;; e.g. (defun test--SDL (&return int) (return -1)) will show up as a failure
(add-cakelisp-search-directory "Dependencies/cakelisp/runtime")
(import "ComptimeHelpers.cake")
(c-import "<stdio.h>")
;; This is completely replaced by find-add-tests
(defun main (&return int) (return 0))
;; Post references resolved hook find-add-tests will create a main function and call test functions
(defun-comptime find-add-tests (environment (ref EvaluatorEnvironment) &return bool)
(var functions-to-test (template (in std vector) (template (in std pair) (in std string) (addr (const Token)))))
(for-in definition-pair (ref ObjectDefinitionPair) (field environment definitions)
(unless (= 0 (call-on find (field definition-pair first) "test--"))
(call-on push_back functions-to-test (call (in std make_pair)
(field definition-pair first)
(field definition-pair second definitionInvocation))))
(get-or-create-comptime-var environment total-tests-found int)
;; No more tests found this round. Exit, otherwise we'll get in an infinite modification loop
(when (= (deref total-tests-found) (call-on size functions-to-test))
(return true))
(unless (call-on empty functions-to-test)
;; We're copying this to the main def, so it's fine if it gets destroyed
(var test-body (template (in std vector) Token))
(var main-definition (addr (template (in std vector) Token)) (new (template (in std vector) Token)))
(call-on push_back (field environment comptimeTokens) main-definition)
(for-in function-pair (ref (template (in std pair) (in std string) (addr (const Token)))) functions-to-test
(var function-name-token Token (deref (field function-pair second)))
(set (field function-name-token type) TokenType_Symbol)
(set (field function-name-token contents) (field function-pair first))
(var function-name-string-token Token function-name-token)
(set (field function-name-string-token type) TokenType_String)
;; Forward-declare test for the compiler. This helps us avoid having to import the test's
;; module into AutoTest
(tokenize-push (deref main-definition)
(declare-extern-function (token-splice-addr function-name-token) (&return int)))
(tokenize-push test-body
(fprintf stderr "\n------------- %s\n" (token-splice-addr
(set num-errors (+ num-errors
((token-splice-addr function-name-token))))
(when num-errors
(fprintf stderr "error: %s failed\n" (token-splice-addr
(return 1))))
(tokenize-push (deref main-definition)
(defun main (&return int)
(var num-errors int 0)
(token-splice-array test-body)
(return num-errors)))
(set (deref total-tests-found) (call-on size functions-to-test))
(unless (ReplaceAndEvaluateDefinition environment "main" (deref main-definition))
(return false)))
(return true))
(add-compile-time-hook post-references-resolved find-add-tests)
(add-build-config-label "AutoTest")
(set-cakelisp-option executable-output "autoTest")
(comptime-define-symbol 'auto-test)