Browse Source

Added AutoTest for quick high-level testing

Using compile-time code generation/modification, functions with
"test--" prefix are automatically called by AutoTest's generated main
function.

I'm not really bought into "Test Driven Design", but I do like having
high-level demonstrations of functionality built in to the
modules. Currently, both SDL and Tracy modules have test
functions. Ogre will need some more work to ensure it has the
necessary files.
pitch-detection
Macoy Madson 3 years ago
parent
commit
9068422058
  1. 6
      Build_Debug.sh
  2. 2
      Dependencies/cakelisp
  3. 65
      src/AutoTest.cake
  4. 4
      src/OgreCore.cake
  5. 5
      src/SDL.cake
  6. 7
      src/Tracy.cake

6
Build_Debug.sh

@ -5,6 +5,8 @@
# ./Dependencies/cakelisp/bin/cakelisp --verbose-processes test/OgreApp.cake
# ./Dependencies/cakelisp/bin/cakelisp --verbose-processes src/SDL.cake
./Dependencies/cakelisp/bin/cakelisp --verbose-compile-time-build-objects test/SDLOgreApp.cake \
&& echo "\n\nRUNTIME\n" && cd test && ./SDLOgreApp
# ./Dependencies/cakelisp/bin/cakelisp --verbose-compile-time-build-objects test/SDLOgreApp.cake \
# && echo "\n\nRUNTIME\n" && cd test && ./SDLOgreApp
# ./Dependencies/cakelisp/bin/cakelisp --verbose-compile-time-build-objects src/Tracy.cake
# ./Dependencies/cakelisp/bin/cakelisp --verbose-compile-time-build-objects src/AutoTest.cake src/Tracy.cake
./Dependencies/cakelisp/bin/cakelisp --verbose-compile-time-build-objects src/AutoTest.cake src/SDL.cake src/Tracy.cake

2
Dependencies/cakelisp

@ -1 +1 @@
Subproject commit 599b6c8b16cb6bbb079d37f0145feb23a22a757b
Subproject commit 49b5319f2576a3d8f0aa183de1736a7689855984

65
src/AutoTest.cake

@ -0,0 +1,65 @@
;; 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
(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--"))
(continue))
(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) (+ (std::string "../") (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)))
(prettyPrintTokens (deref main-definition))
(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)

4
src/OgreCore.cake

@ -129,7 +129,8 @@
"-IDependencies/ogre-next/build/Debug/include"
"-IDependencies/ogre-next/Components/Overlay/include")
;; TODO: Automatically build Ogre if it isn't built yet, and copy necessary files
;; TODO: Automatically build Ogre if it isn't built yet
;; TODO: Copy necessary files (Hlms materials etc.) so self-test/user exes can run
;; (defun-comptime ogre-pre-build-hook ()
;; (unless (fileExists "Dependencies/ogre-next/build/Debug/lib")
;; (printf "error: Ogre is not built yet. Please run ./BuildDependencies_Debug.sh\n")
@ -164,4 +165,3 @@
(return true))
(add-compile-time-hook pre-link ogre-link-hook)

5
src/SDL.cake

@ -40,7 +40,7 @@
(SDL_Quit))
;; TODO: Automatically promote this if no main is defined. Separate target instead?
(defun sdl-main (&return int)
(defun test--sdl-main (&return int)
(printf "Hello, SDL!\n")
(var window (* SDL_Window) nullptr)
(unless (sdl-initialize (addr window)) (return 1))
@ -53,6 +53,9 @@
(while (SDL_PollEvent (addr event))
(when (= (field event type) SDL_QUIT)
(set exit-reason "Window event")))
(var currentKeyStates (* (const Uint8)) (SDL_GetKeyboardState nullptr))
(when (at SDL_SCANCODE_ESCAPE currentKeyStates)
(set exit-reason "Escape pressed"))
(SDL_UpdateWindowSurface window))
(when exit-reason

7
src/Tracy.cake

@ -7,16 +7,19 @@
;; TODO: Linux only
(c-import "unistd.h")
(defun test-tracy-main (&return int)
(defun test--tracy-main (&return int)
(ZoneScopedN "main")
(printf "Waiting for profiler to connect...\n")
(while (not (on-call (call (in tracy GetProfiler)) IsConnected))
(ZoneScopedN "wait for profiler")
(sleep 1))
(var i int 0)
(while (< i 100000)
(var num-times (const int) 10)
(while (< i num-times)
(ZoneScopedN "hot loop")
(printf "hot loop %d / %d\n" i num-times)
(sleep 1)
(incr i))
(return 0))

Loading…
Cancel
Save