This repository has been archived by the owner on Mar 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.clj
106 lines (92 loc) · 2.41 KB
/
test.clj
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
102
103
104
105
106
(load "core.clj")
(defn fail
[prefix success & expr]
(print (str prefix ": "))
(apply prn expr)
(swap! success (fn [_] false)))
(defmacro tests
[& exprs]
(let [success-sym (gensym)]
`(let [~success-sym (atom true)]
~(cons 'do
(map (fn [expr]
`(try
(when-not ~expr
(fail "F" ~success-sym '~expr))
(catch ~'e
(fail "E" ~success-sym '~expr '-> ~'e))))
exprs))
(when-not (deref ~success-sym)
(println "Some tests failed!")
(throw "fail")))))
(tests
;; seqs
(every? (comp not seq)
[nil () [] (lazy-seq ()) "" {} (hash-map)])
(every? (comp (partial = ()) rest)
[nil () [] (lazy-seq ()) "" {} (hash-map)])
(every? (comp not first)
[nil () [] (lazy-seq ()) "" {} (hash-map)])
(= () [])
(not (= () nil))
(not (= [] nil))
;; numbers
(every? #(apply = %)
[[1 (*)]
[0 (+)]
[3 (+ 1 2)]
[6 (+ 1 2 3)]
[30 (* 2 3 5)]
[-1 (- 2 3)]
[-1 (- 1)]])
;; maps
(= 1 (get {:a 1 :b 2} :a))
(not (get {:a 1 :b 2} :c))
(= 3 (get {:a 1 :b 2} :c 3))
(= {} (dissoc {:a 1} :a))
;; for
(= [[1 0] [2 0] [2 1] [3 0] [3 1] [3 2]]
(for [x (range 4) y (range x)] [x y]))
;; qualified symbols
(do
(def ns-check :orig-ns)
(in-ns 'tmp-ns)
(def ns-check :new-ns)
(in-ns 'user)
(and (= :orig-ns ns-check)
(= :new-ns tmp-ns/ns-check)))
;; recur in a fn with rest args
(let [xss (atom ())
f (fn [& xs]
(swap! xss conj xs)
(when (seq xs)
(recur (next xs))))]
(f 1 2 3)
(= [nil [3] [2 3] [1 2 3]]
(deref xss)))
(= :ok ((fn [[x & xs]]
(if (= 3 x)
:ok
(recur xs))) [1 2 3]))
(try
(eval
'(loop [x 1, y 2]
(recur 3)))
(catch e
(= e "Invalid number of recur arguments: 1 given, 2 expected")))
(let [at (atom [])]
(try
(throw "thrown")
(catch e
(swap! at conj e))
(finally
(swap! at conj :ok)))
(= (deref at) [:ok "thrown"]))
;; macro expansion
(= '(lazy-seq* (fn [] [1 2 3]))
(macroexpand-1 '(lazy-seq [1 2 3])))
(= '(if 1 (do 2 3) nil)
(macroexpand-1 '(when 1 2 3)))
;; quasi quoting
(eval `(= 'a# 'a#))
(not (= `a# `a#)))