forked from pixie-lang/pixie
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.pxi
More file actions
101 lines (80 loc) · 2.93 KB
/
test.pxi
File metadata and controls
101 lines (80 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
(ns pixie.test
(:require [pixie.string :as s]
[pixie.fs :as fs]))
(def tests (atom {}))
(def ^:dynamic *stats*)
(def ^:dynamic *current-test*)
(defmacro deftest [nm & body]
`(do (defn ~nm []
(println "Running:" (str (namespace (var ~nm)) "/" (name (var ~nm))))
(try
~@body
(swap! *stats* update-in [:pass] (fnil inc 0))
(catch ex
(println "while running" ~(name nm) " " (quote (do ~@body)))
(swap! *stats* update-in [:fail] (fnil inc 0))
(println (str ex))
(swap! *stats* update-in [:errors] (fnil conj []) ex))))
(swap! tests assoc (symbol (str (namespace (var ~nm)) "/" (name (var ~nm)))) ~nm)))
(defn run-tests [& args]
(push-binding-frame!)
(set! (var *stats*) (atom {:fail 0 :pass 0}))
(let [match (or (first args) "")
tests (transduce (comp (filter #(>= (s/index-of (str (key %1)) match) 0))
(map val))
conj
@tests)]
(println "Running:" (count tests) "tests")
(foreach [test tests]
(test)))
(let [stats @*stats*]
(println stats)
(pop-binding-frame!)
stats))
(defn load-all-tests []
(println "Looking for tests...")
(let [dirs (distinct (map fs/dir @load-paths))
pxi-files (->> dirs
(mapcat fs/walk-files)
(filter #(fs/extension? % "pxi"))
(filter #(s/starts-with? (fs/basename %) "test-"))
(distinct))]
(foreach [file pxi-files]
(println "Loading " file)
(load-file (fs/abs file)))))
(defmacro assert= [x y]
`(let* [xr# ~x
yr# ~y]
(assert (= xr# yr#) (str (show '~x xr#) " != " (show '~y yr#)))))
(defmacro assert-throws?
([body]
`(let [exn# (try (do ~body nil) (catch e# e#))]
(assert (not (nil? exn#))
(str "Expected " (pr-str (quote ~body)) " to throw an exception"))
exn#))
([klass body]
`(let [exn# (assert-throws? ~body)]
(assert (= (type exn#) ~klass)
(str "Expected " (pr-str (quote ~body))
" to throw exception of class " (pr-str ~klass)
" but got " (pr-str (type exn#))))
exn#))
([klass msg body]
`(let [exn# (assert-throws? ~klass ~body)]
(assert (= (ex-msg exn#) ~msg)
(str "Expected " (pr-str (quote ~body))
" to throw exception with message " (pr-str ~msg)
" but got " (pr-str (ex-msg exn#)))))))
(defmacro assert [x]
`(let [x# ~x]
(assert x# (str '~x " is " x#))))
(defn show
([orig res]
(if (= orig res)
(pr-str orig)
(str (pr-str orig) " = " (pr-str res)))))
(defmacro assert-table [pattern expr & vals]
(let [parted (partition (count pattern) vals)]
`(do ~@(for [fact parted]
`(let [~@(interleave pattern fact)]
~expr)))))