├── .gitignore ├── LICENSE ├── README.md ├── project.janet ├── src └── tester.janet └── test └── tester_test.janet /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 Sean Walker 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tester 2 | 3 | __A testing library for janet__ 4 | 5 | ## Installation 6 | 7 | Add the dependency [https://github.com/joy-framework/tester](https://github.com/joy-framework/tester) to your `project.janet` file: 8 | 9 | ```clojure 10 | (declare-project 11 | :dependencies ["https://github.com/joy-framework/tester"]) 12 | ``` 13 | 14 | ## Usage 15 | 16 | Create a `.janet` file for testing and use this library like so: 17 | 18 | 19 | ```clojure 20 | (import tester :prefix "" :exit true) 21 | 22 | (deftest 23 | (test "1 + 1 = 2" 24 | (is (= 2 (+ 1 1)))) 25 | 26 | (test "expected = actual" 27 | (let [expected "expected" 28 | actual "expected"] 29 | (is (= expected actual))))) 30 | ``` 31 | 32 | Run your tests from the terminal with `jpm test` in your project directory 33 | 34 | ```sh 35 | jpm test 36 | ``` 37 | 38 | For a better workflow, use [entr](https://github.com/eradman/entr) to restart `jpm test` automatically on a file change like this: 39 | 40 | ```clojure 41 | ; # ... rest of project.janet above 42 | 43 | (phony "watch" [] 44 | (os/shell "find . -name '*.janet' | entr -r -d jpm test")) 45 | ``` 46 | 47 | Then run this from the terminal and you're all set to get a fast running test suite on any file change in `src` or in `test` 48 | 49 | ```sh 50 | jpm run watch 51 | ``` 52 | 53 | ## Contribute 54 | 55 | Any issues or pull requests are welcome! 56 | 57 | ## License 58 | 59 | MIT 60 | -------------------------------------------------------------------------------- /project.janet: -------------------------------------------------------------------------------- 1 | (declare-project 2 | :name "tester" 3 | :description "A testing library for janet" 4 | :dependencies [] 5 | :author "Sean Walker" 6 | :license "MIT" 7 | :url "https://github.com/joy-framework/tester" 8 | :repo "git+https://github.com/joy-framework/tester") 9 | 10 | (declare-source 11 | :source @["src/tester.janet"]) 12 | 13 | (phony "watch" [] 14 | (os/shell "find . -name '*.janet' | entr -c jpm test")) 15 | -------------------------------------------------------------------------------- /src/tester.janet: -------------------------------------------------------------------------------- 1 | # Stolen from https://github.com/janet-lang/janet/blob/master/test/helper.janet 2 | # Helper code for running tests 3 | 4 | (var num-tests-passed 0) 5 | (var num-tests-run 0) 6 | (var start-time 0) 7 | (var suite-name "") 8 | 9 | 10 | (defn assert 11 | "Override's the default assert with some nice error handling." 12 | [x e] 13 | (++ num-tests-run) 14 | (if x 15 | (++ num-tests-passed) 16 | (if (empty? suite-name) 17 | (print "Test: " e) 18 | (print "Suite: " suite-name "\nTest: " e))) 19 | x) 20 | 21 | 22 | (defn test [e x] 23 | (try 24 | (assert x e) 25 | ([err fib] 26 | (file/write stdout "\n\e[31mx\e[0m ") 27 | (print e) 28 | (debug/stacktrace fib err) 29 | (os/exit 1)))) 30 | 31 | 32 | (defmacro is [form] 33 | (let [[op expected actual] form] 34 | ~(let [_op ,op 35 | len ,(length form) 36 | _expected ,expected 37 | _actual (if (= len 2) 38 | false 39 | ,actual) 40 | result (if (= len 2) 41 | ~,(_op _expected) 42 | ~,(_op _expected _actual))] 43 | (if result 44 | true 45 | (do 46 | (printf "\nFailed: %q" ',form) 47 | (printf "Expected: %q" (or (= len 2) _expected)) 48 | (printf "Actual: %q" _actual)))))) 49 | 50 | 51 | (defmacro catch [& forms] 52 | ~(try 53 | (do ,;forms) 54 | ([err] 55 | err))) 56 | 57 | 58 | (defmacro assert-error 59 | [msg & forms] 60 | (def errsym (keyword (gensym))) 61 | ~(assert (= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg)) 62 | 63 | 64 | (defmacro assert-no-error 65 | [msg & forms] 66 | (def errsym (keyword (gensym))) 67 | ~(assert (not= ,errsym (try (do ,;forms) ([_] ,errsym))) ,msg)) 68 | 69 | 70 | (defn start-suite [&opt name] 71 | (when (string? name) 72 | (set suite-name name)) 73 | 74 | (set num-tests-passed 0) 75 | (set num-tests-run 0) 76 | (set start-time (os/clock))) 77 | 78 | 79 | (defn end-suite [] 80 | (let [delta (- (os/clock) start-time) 81 | failures? (not= num-tests-passed num-tests-run)] 82 | 83 | (printf "%s" (if failures? "\n" "")) 84 | 85 | (unless (empty? suite-name) 86 | (print "Suite: " suite-name)) 87 | 88 | (printf "Tests: %d/%d passed" num-tests-passed num-tests-run) 89 | (printf "Duration: %.3f seconds" delta) 90 | 91 | (if failures? 92 | (os/exit 1) 93 | (print)))) 94 | 95 | 96 | (defmacro deftest [& forms] 97 | ~(do 98 | (start-suite) 99 | ,(splice forms) 100 | (end-suite))) 101 | 102 | 103 | (defmacro deftests [& forms] 104 | ~(deftest ,;forms)) 105 | 106 | 107 | (defmacro defsuite [name & forms] 108 | ~(do 109 | (start-suite ,name) 110 | ,(splice forms) 111 | (end-suite))) 112 | -------------------------------------------------------------------------------- /test/tester_test.janet: -------------------------------------------------------------------------------- 1 | (import "src/tester" :prefix "" :exit true) 2 | 3 | (var doubled 1) 4 | 5 | # deftests is optional 6 | (deftests 7 | (test "dont eval actual twice*" 8 | (is (= 2 (do (set doubled (inc doubled)) 9 | doubled)))) 10 | 11 | (test "without is" 12 | (= 2 (+ 1 1))) 13 | 14 | (test "1 + 1 = 2" 15 | (is (= 2 (+ 1 1)))) 16 | 17 | (test "1 + 2 = 3" 18 | (is (= 3 (+ 1 2)))) 19 | 20 | (test "errors can be tested" 21 | (is (= "hello" (catch (error "hello"))))) 22 | 23 | (test "is works with let" 24 | (let [expected "expected" 25 | actual "expected"] 26 | (is (= expected actual)))) 27 | 28 | (test "dont eval actual twice" 29 | (is (= 3 (do (set doubled (inc doubled)) 30 | doubled)))) 31 | 32 | (test "tuples shouldn't error" 33 | (is (= ["hello"] (freeze @["hello"])))) 34 | 35 | (test "empty?" 36 | (is (empty? [])))) 37 | 38 | # use defsuite to give a suite a name (name is required) 39 | (defsuite "predicates" 40 | (test "any?" 41 | (is (any? [true])))) 42 | --------------------------------------------------------------------------------