GameLib is my library for making games
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.

67 lines
3.6 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 &comptime-only "Macros.cake")
;; 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 (& EvaluatorEnvironment)
was-code-modified (& bool) &return bool)
(var functions-to-test (<> std::vector (<> std::pair std::string (* (const Token)))))
(var required-imports (<> std::vector (<> std::pair (* (const char)) (* (const Token)))))
(for-in definition-pair (& ObjectDefinitionPair) (field environment definitions)
(unless (!= (in std string npos) (on-call (field definition-pair first) find "test--"))
(on-call functions-to-test push_back (call (in std make_pair)
(field definition-pair first)
(field definition-pair second definitionInvocation)))
(on-call required-imports push_back (call (in std make_pair)
(path definition-pair . second . definitionInvocation > source)
(field definition-pair second definitionInvocation))))
(get-or-create-comptime-var 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) (on-call functions-to-test size))
(return true))
(unless (on-call functions-to-test empty)
;; We're copying this to the main def, so it's fine if it gets destroyed
(var test-body (<> std::vector Token))
(for-in function-pair (& (<> std::pair std::string (* (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))
(tokenize-push test-body
(set num-errors (+ num-errors
((token-splice-addr function-name-token))))))
(var main-definition (* (<> std::vector Token)) (new (<> std::vector Token)))
(on-call (field environment comptimeTokens) push_back main-definition)
(for-in import-pair (& (<> std::pair (* (const char)) (* (const Token)))) required-imports
(var import-str Token (deref (field import-pair second)))
(set (field import-str type) TokenType_String)
(set (field import-str contents) (field import-pair first))
(tokenize-push (deref main-definition)
(import (token-splice-addr import-str))))
(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) (on-call functions-to-test size))
(unless (ReplaceAndEvaluateDefinition environment "main" (deref main-definition))
(return false))
(set was-code-modified true))
(return true))
(add-compile-time-hook post-references-resolved find-add-tests)
(set-cakelisp-option executable-output "test/autoTest")