-
Notifications
You must be signed in to change notification settings - Fork 25
/
expectations.clj
867 lines (741 loc) · 33.6 KB
/
expectations.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
(ns expectations
(:use clojure.set)
(:require clojure.template clojure.string clojure.pprint clojure.data))
(def nothing "no arg given")
(defn no-op [& x])
(defn anything [& _] true)
(defn anything& [& _] true)
(defn a-fn [& _] true)
(defn a-fn1 [& _] true)
(defn a-fn2 [& _] true)
(defn a-fn3 [& _] true)
(defn in [n] {::in n ::in-flag true})
(defn contains-kvs [& {:as kvs}] {::contains-kvs kvs ::contains-kvs-flag true})
;;; GLOBALS
(def run-tests-on-shutdown (atom true))
(def warn-on-iref-updates-boolean (atom false))
(def lib-namespaces (set (all-ns)))
(def ^{:dynamic true} *test-name* nil)
(def ^{:dynamic true} *test-meta* {})
(def ^{:dynamic true} *test-var* nil)
(def ^{:dynamic true} *prune-stacktrace* true)
(def ^{:dynamic true} *report-counters* nil) ; bound to a ref of a map in test-ns
(def ^{:dynamic true} *initial-report-counters* ; used to initialize *report-counters*
{:test 0, :pass 0, :fail 0, :error 0 :run-time 0})
(def ^{:dynamic true} reminder nil)
;;; UTILITIES FOR REPORTING FUNCTIONS
(defn getenv [var]
(System/getenv var))
(defn on-windows? []
(re-find #"[Ww]in" (System/getProperty "os.name")))
(defn show-raw-choice []
(if-let [choice (getenv "EXPECTATIONS_SHOW_RAW")]
(= "TRUE" (clojure.string/upper-case choice))
true))
(defn colorize-choice []
(clojure.string/upper-case (or (getenv "EXPECTATIONS_COLORIZE")
(str (not (on-windows?))))))
(def ansi-colors {:reset "[0m"
:red "[31m"
:blue "[34m"
:yellow "[33m"
:cyan "[36m"
:green "[32m"
:magenta "[35m"})
(defn ansi [code]
(str \u001b (get ansi-colors code (:reset ansi-colors))))
(defn color [code & s]
(str (ansi code) (apply str s) (ansi :reset)))
(defn colorize-filename [s]
(condp = (colorize-choice)
"TRUE" (color :magenta s)
s))
(defn colorize-raw [s]
(condp = (colorize-choice)
"TRUE" (color :cyan s)
s))
(defn colorize-results [pred s]
(condp = (colorize-choice)
"TRUE" (if (pred)
(color :green s)
(color :red s))
s))
(defn colorize-warn [s]
(condp = (colorize-choice)
"TRUE" (color :yellow s)
s))
(defn string-join [s coll]
(clojure.string/join s (remove nil? coll)))
(defn stack->file&line [ex index]
(let [s (nth (.getStackTrace ex) index)]
(str (.getFileName s) ":" (.getLineNumber s))))
(defn inc-report-counter [name]
(when *report-counters*
(dosync (commute *report-counters* assoc name
(inc (or (*report-counters* name) 0))))))
;;; TEST RESULT REPORTING
(defn test-name [{:keys [line ns]}]
(str ns ":" line))
(defn test-file [{:keys [file line]}]
(colorize-filename (str (last (re-seq #"[A-Za-z_\.]+" file)) ":" line)))
(defn raw-str [[e a]]
(with-out-str (clojure.pprint/pprint `(~'expect ~e ~a))))
(defn ^{:dynamic true} fail [test-name test-meta msg]
(println (str "\nfailure in (" (test-file test-meta) ") : " (:ns test-meta))) (println msg))
(defn ^{:dynamic true} summary [msg] (println msg))
(defn ^{:dynamic true} started [test-name test-meta])
(defn ^{:dynamic true} finished [test-name test-meta])
(defn ^{:dynamic true} ns-finished [a-ns])
(defn ^{:dynamic true} expectation-finished [a-var])
(defn ^{:dynamic true} ignored-fns [{:keys [className fileName]}]
(when *prune-stacktrace*
(or (= fileName "expectations.clj")
(= fileName "expectations_options.clj")
(= fileName "NO_SOURCE_FILE")
(= fileName "interruptible_eval.clj")
(re-seq #"clojure\.lang" className)
(re-seq #"clojure\.core" className)
(re-seq #"clojure\.main" className)
(re-seq #"java\.lang" className)
(re-seq #"java\.util\.concurrent\.ThreadPoolExecutor\$Worker" className))))
(defn pruned-stack-trace [t]
(string-join "\n"
(distinct (map (fn [{:keys [className methodName fileName lineNumber] :as m}]
(if (= methodName "invoke")
(str " on (" fileName ":" lineNumber ")")
(str " " className "$" methodName " (" fileName ":" lineNumber ")")))
(remove ignored-fns (map bean (.getStackTrace t)))))))
(defmulti report :type)
(defmethod report :pass [m]
(alter-meta! *test-var* assoc :status [:success "" (:line *test-meta*)])
(inc-report-counter :pass))
(defmethod report :fail [m]
(inc-report-counter :fail)
(let [current-test *test-var*
message (string-join "\n"
[(when reminder
(colorize-warn (str " ***** " (clojure.string/upper-case reminder) " *****")))
(when-let [msg (:raw m)]
(when (show-raw-choice) (colorize-raw (raw-str msg))))
(when-let [msg (:result m)] (str " " (string-join " " msg)))
(when (or (:expected-message m) (:actual-message m) (:message m)) " ")
(when-let [msg (:expected-message m)] (str " " msg))
(when-let [msg (:actual-message m)] (str " " msg))
(when-let [msg (:message m)] (str " " msg))])]
(alter-meta! current-test
assoc :status [:fail message (:line *test-meta*)])
(fail *test-name* *test-meta* message)))
(defmethod report :error [{:keys [result raw] :as m}]
(inc-report-counter :error)
(let [current-test *test-var*
message (string-join "\n"
[(when reminder (colorize-warn (str " ***** " (clojure.string/upper-case reminder) " *****")))
(when raw
(when (show-raw-choice) (colorize-raw (raw-str raw))))
(when-let [msg (:expected-message m)] (str " exp-msg: " msg))
(when-let [msg (:actual-message m)] (str " act-msg: " msg))
(str " threw: " (class result) " - " (.getMessage result))
(pruned-stack-trace result)])]
(alter-meta! current-test
assoc :status [:error message (:line *test-meta*)])
(fail *test-name* *test-meta* message)))
(defmethod report :summary [{:keys [test pass fail error run-time ignored-expectations]}]
(summary (str "\nRan " test " tests containing "
(+ pass fail error) " assertions in "
run-time " msecs\n"
(when (> ignored-expectations 0) (colorize-warn (str "IGNORED " ignored-expectations " EXPECTATIONS\n")))
(colorize-results (partial = 0 fail error) (str fail " failures, " error " errors")) ".")))
;; TEST RUNNING
(defn disable-run-on-shutdown [] (reset! run-tests-on-shutdown false))
(defn warn-on-iref-updates [] (reset! warn-on-iref-updates-boolean true))
(defn find-every-iref []
(->> (all-ns)
(remove #(re-seq #"(clojure\.|expectations)" (str (.name %))))
(mapcat (comp vals ns-interns))
(filter bound?)
(keep #(when-let [val (var-get %)] [% val]))
(filter (comp #{clojure.lang.Agent clojure.lang.Atom clojure.lang.Ref} class second))))
(defn add-watch-every-iref-for-updates []
(doseq [[var iref] (find-every-iref)]
(add-watch iref ::expectations-watching-state-modifications
(fn [_ reference old-state new-state]
(println (colorize-warn
(clojure.string/join " "
["WARNING:"
(or *test-name* "test name unset")
"modified" var
"from" (pr-str old-state)
"to" (pr-str new-state)])))
(when-not *test-name*
(.printStackTrace (RuntimeException. "stacktrace for var modification") System/out))))))
(defn remove-watch-every-iref-for-updates []
(doseq [[var iref] (find-every-iref)]
(remove-watch iref ::expectations-watching-state-modifications)))
(defn test-var [v]
(when-let [t (var-get v)]
(let [tn (test-name (meta v))
tm (meta v)]
(started tn tm)
(inc-report-counter :test)
(binding [*test-name* tn
*test-meta* tm
*test-var* v]
(try
(t)
(catch Throwable e
(println "\nunexpected error in" tn)
(.printStackTrace e))))
(finished tn tm))))
(defn find-expectations-vars [option-type]
(->>
(all-ns)
(mapcat (comp vals ns-interns))
(filter (comp #{option-type} :expectations-options meta))))
(defn execute-vars [vars]
(doseq [var vars]
(when (bound? var)
(when-let [vv (var-get var)]
(vv)))))
(defn create-context [work]
(let [vars (find-expectations-vars :in-context)]
(cond
(= 0 (count vars)) (work)
(= 1 (count vars)) ((var-get (first vars)) work)
:default (do
(println "expectations only supports 0 or 1 :in-context fns. Ignoring:" vars)
(work)))))
(defn test-vars [vars ignored-expectations]
(remove-ns 'expectations-options)
(try
(require 'expectations-options :reload)
(catch java.io.FileNotFoundException e))
(-> (find-expectations-vars :before-run) (execute-vars))
(when @warn-on-iref-updates-boolean
(add-watch-every-iref-for-updates))
(create-context
#(binding [*report-counters* (ref *initial-report-counters*)]
(let [ns->vars (group-by (comp :ns meta) vars)
start (System/nanoTime)]
(doseq [[a-ns the-vars] ns->vars]
(doseq [v the-vars]
(test-var v)
(expectation-finished v))
(ns-finished (ns-name a-ns)))
;;; (dorun (pmap test-var vars))
(let [result (assoc @*report-counters*
:run-time (int (/ (- (System/nanoTime) start) 1000000))
:ignored-expectations ignored-expectations)]
(when @warn-on-iref-updates-boolean
(remove-watch-every-iref-for-updates))
result)))))
(defn run-tests-in-vars [vars]
(doto (assoc (test-vars vars 0) :type :summary)
(report)))
(defn ->expectation [ns]
(->> ns
ns-interns
vals
(sort-by str)
(filter (comp :expectation meta))))
(defn ->focused-expectations [expectations]
(->> expectations (filter (comp :focused meta)) seq))
(defn run-tests [namespaces]
(let [expectations (mapcat ->expectation namespaces)]
(if-let [focused (->focused-expectations expectations)]
(doto (assoc (test-vars focused (- (count expectations) (count focused))) :type :summary)
(report))
(doto (assoc (test-vars expectations 0) :type :summary)
(report)))))
(defn run-all-tests
([] (run-tests (all-ns)))
([re] (run-tests (filter #(re-matches re (name (ns-name %))) (all-ns)))))
(defmulti nan->keyword class :default :default)
(defmethod nan->keyword java.util.Map [m]
(if (instance? clojure.lang.IRecord m)
(nan->keyword (into {} (seq m)))
(let [f (fn [[k v]] [k (if (and (number? v) (Double/isNaN v)) :DoubleNaN v)])]
(clojure.walk/postwalk (fn [x] (if (map? x) (into {} (map f x)) x)) m))))
(defmethod nan->keyword java.util.List [m]
(map #(if (and (number? %) (Double/isNaN %)) :DoubleNaN %) m))
(defmethod nan->keyword Double [m]
(if (Double/isNaN m) :DoubleNaN m))
(defmethod nan->keyword java.util.Set [m]
(reduce #(conj %1 (if (and (number? %2) (Double/isNaN %2)) :DoubleNaN %2)) #{} m))
(defmethod nan->keyword :default [m]
(if (and (number? m) (Double/isNaN m)) :DoubleNaN m))
(defmulti extended-not= (fn [x y] [(class x) (class y)]) :default :default)
(defmethod extended-not= [Double Double] [x y]
(if (and (Double/isNaN x) (Double/isNaN y))
false
(not= x y)))
(defmethod extended-not= :default [x y] (not= x y))
(defn map-intersection [e a]
(let [in-both (intersection (set (keys e)) (set (keys a)))]
(select-keys (merge-with vector e a) in-both)))
(defn ->disagreement [prefix [k [v1 v2]]]
(if (and (map? v1) (map? v2))
(string-join
"\n "
(remove nil? (map
(partial ->disagreement
(str (when prefix (str prefix " {")) k))
(map-intersection v1 v2))))
(when (extended-not= v1 v2)
(let [prefix-desc (str (when prefix (str prefix " {")) (pr-str k))
prefix-space (apply str (take (count prefix-desc) (repeat " ")))]
(str prefix-desc " expected: "
(pr-str v1) "\n " prefix-space " was: " (pr-str v2))))))
(defn map-diff-message [e a padding]
(->>
(map (partial ->disagreement nil) (map-intersection e a))
(remove nil?)
(remove empty?)
seq))
(defn normalize-keys* [a ks m]
(if (map? m)
(reduce into [] (map (fn [[k v]] (normalize-keys* a (conj ks k) v)) (seq m)))
(conj a ks)))
(defn normalize-keys [m] (normalize-keys* [] [] m))
(defn ->missing-message [m msg item]
(str (string-join " {" (map pr-str item)) " with val " (pr-str (get-in m item)) msg))
(defn map-difference [e a]
(difference (set (normalize-keys e)) (set (normalize-keys a))))
(defn map-missing-message [e a msg]
(->>
(map-difference e a)
(map (partial ->missing-message e msg))
seq))
(defn map-compare [e a str-e str-a original-a but-string]
(if (= (nan->keyword e) (nan->keyword a))
{:type :pass}
{:type :fail
:actual-message (when-let [messages (map-missing-message e a " is in expected, but not in actual")]
(string-join "\n " messages))
:expected-message (when-let [messages (map-missing-message a e " is in actual, but not in expected")]
(string-join "\n " messages))
:raw [str-e str-a]
:result ["expected:" (pr-str e) "\n" but-string (pr-str original-a)]
:message (when-let [messages (map-diff-message e a "")] (string-join "\n " messages))}))
(defmulti compare-expr (fn [e a _ _]
(cond
(and (isa? e Throwable) (not= e a)) ::expect-exception
(instance? Throwable e) ::expected-exception
(instance? Throwable a) ::actual-exception
(and (fn? e) (not= e a)) ::fn
(and (not (sorted? a)) (::in-flag a)) ::in
(and (not (sorted? e)) (::contains-kvs-flag e)) ::contains-kvs
(and (not (sorted? e)) (::interaction-flag e)) ::interaction
:default [(class e) (class a)])))
(defmethod compare-expr :default [e a str-e str-a]
(if (= e a)
{:type :pass}
{:type :fail :raw [str-e str-a]
:result ["expected:" (pr-str e)
"\n was:" (pr-str a)]}))
(defmethod compare-expr ::fn [e a str-e str-a]
(try
(if (e a)
{:type :pass}
{:type :fail :raw [str-e str-a] :result [(pr-str a) "is not" str-e]})
(catch clojure.lang.ArityException ex
{:type :fail :raw [str-e str-a]
:expected-message (str "also attempted: (" str-e " " str-a ")")
:actual-message (str " and got: " (.getMessage ex))
:result ["expected:" str-e
"\n was:" (pr-str a)]})))
(defmethod compare-expr ::contains-kvs [{e ::contains-kvs} a str-e str-a]
(compare-expr e (in a) str-e str-a))
(defmethod compare-expr ::in [e a str-e str-a]
(cond
(instance? java.util.List (::in a))
(if (seq (filter (fn [item] (= (nan->keyword e) (nan->keyword item))) (::in a)))
{:type :pass}
{:type :fail :raw [str-e str-a]
:result ["value" (pr-str e) "not found in" (::in a)]})
(instance? java.util.Set (::in a))
(if ((nan->keyword (::in a)) (nan->keyword e))
{:type :pass}
{:type :fail :raw [str-e str-a]
:result ["key" (pr-str e) "not found in" (::in a)]})
(instance? java.util.Map (::in a))
(map-compare e (select-keys (::in a) (keys e)) str-e str-a (::in a) " in:")
:default {:type :fail :raw [str-e str-a]
:result ["You supplied:" (pr-str (::in a))]
:message "You must supply a list, set, or map when using (in)"}))
(defmethod compare-expr [Class Object] [e a str-e str-a]
(if (instance? e a)
{:type :pass}
{:type :fail :raw [str-e str-a]
:expected-message (str "expected: " a " to be an instance of " e)
:actual-message (str " was: " a " is an instance of " (class a))}))
(defmethod compare-expr [Class Class] [e a str-e str-a]
(if (isa? a e)
{:type :pass}
{:type :fail :raw [str-e str-a]
:expected-message (str "expected: " a " to be a " e)}))
(defmethod compare-expr ::actual-exception [e a str-e str-a]
{:type :error
:raw [str-e str-a]
:actual-message (str "exception in actual: " str-a)
:result a})
(defmethod compare-expr ::expected-exception [e a str-e str-a]
{:type :error
:raw [str-e str-a]
:expected-message (str "exception in expected: " str-e)
:result e})
(defmethod compare-expr [java.util.regex.Pattern java.util.regex.Pattern] [e a str-e str-a]
(compare-expr (.pattern e) (.pattern a) str-e str-a))
(defmethod compare-expr [java.util.regex.Pattern Object] [e a str-e str-a]
(if (re-seq e a)
{:type :pass}
{:type :fail,
:raw [str-e str-a]
:result ["regex" (pr-str e) "not found in" (pr-str a)]}))
(defmethod compare-expr [String String] [e a str-e str-a]
(if (= e a)
{:type :pass}
(let [matches (->> (map vector e a) (take-while (partial apply =)) (map first) (apply str))
e-diverges (clojure.string/replace e matches "")
a-diverges (clojure.string/replace a matches "")]
{:type :fail :raw [str-e str-a]
:result ["expected:" (pr-str e)
"\n was:" (pr-str a)]
:message (str
"matches: " (pr-str matches)
"\n diverges: " (pr-str e-diverges)
"\n &: " (pr-str a-diverges))})))
(defmethod compare-expr ::expect-exception [e a str-e str-a]
(if (instance? e a)
{:type :pass}
{:type :fail :raw [str-e str-a]
:result [str-a "did not throw" str-e]}))
(defmethod compare-expr [java.util.Map java.util.Map] [e a str-e str-a]
(map-compare e a str-e str-a a " was:"))
(defmethod compare-expr [java.util.Set java.util.Set] [e a str-e str-a]
(if (= (nan->keyword e) (nan->keyword a))
{:type :pass}
(let [diff-fn (fn [e a] (seq (difference e a)))]
{:type :fail
:actual-message (when-let [v (diff-fn e a)]
(str (string-join ", " v)
" are in expected, but not in actual"))
:expected-message (when-let [v (diff-fn a e)]
(str (string-join ", " v)
" are in actual, but not in expected"))
:raw [str-e str-a]
:result ["expected:" e "\n was:" (pr-str a)]})))
(defmethod compare-expr [java.util.List java.util.List] [e a str-e str-a]
(if (= (nan->keyword e) (nan->keyword a))
{:type :pass}
(let [diff-fn (fn [e a] (seq (difference (set e) (set a))))]
{:type :fail
:actual-message (when-let [v (diff-fn e a)]
(str (string-join ", " (map pr-str v))
" are in expected, but not in actual"))
:expected-message (when-let [v (diff-fn a e)]
(str (string-join ", " (map pr-str v))
" are in actual, but not in expected"))
:raw [str-e str-a]
:result ["expected:" e "\n was:" (pr-str a)]
:message (cond
(and
(= (set e) (set a))
(= (count e) (count a))
(= (count e) (count (set a))))
"lists appear to contain the same items with different ordering"
(and (= (set e) (set a)) (< (count e) (count a)))
"some duplicate items in actual are not expected"
(and (= (set e) (set a)) (> (count e) (count a)))
"some duplicate items in expected are not actual"
(< (count e) (count a))
"actual is larger than expected"
(> (count e) (count a))
"expected is larger than actual")})))
(defn fn-string [f-name f-args]
(str "(" f-name (when (seq f-args) " ") (string-join " " (map pr-str f-args)) ")"))
(defn compare-individual-args [idx arg1 arg2 raw-e]
(let [{:keys [expected-message actual-message result message type]}
(compare-expr arg1 arg2 raw-e (pr-str arg2))]
(when-not (= type :pass)
(str
"\n - arg" idx ": " (pr-str arg2)
(when expected-message (str "\n " expected-message))
(when actual-message (str "\n " actual-message))
(when message (str "\n " message))))))
(defn compare-each-arg [result
[raw-e-first & raw-e-rest]
idx
{e-first 0 :as e-args :or {e-first nothing}}
{a-first 0 :as a-args :or {a-first nothing}}
max-idx]
(if (> idx max-idx)
result
(recur (str result (compare-individual-args idx e-first a-first raw-e-first))
raw-e-rest
(inc idx) (vec (rest e-args)) (vec (rest a-args)) max-idx)))
(defn compare-args [raw-e f-name expected-args actual-args]
(compare-each-arg (str "\n\n -- got: " (fn-string f-name actual-args))
raw-e
1
(vec expected-args)
(vec actual-args)
(count expected-args)))
(defn matches? [e-arg a-arg]
(if (= e-arg :anything)
true
(-> (compare-expr e-arg a-arg nil nil) :type (= :pass))))
(defn matching [[e-first & e-rest] [a-first & a-rest]]
(let [match (matches? e-first a-first)]
(cond
(and (nil? e-rest) (nil? a-rest)) match
(false? match) false
(= e-first anything&) true
:default (recur e-rest a-rest))))
(defmethod compare-expr ::interaction [{:keys [function
interactions
expected-args]}
expected-times-keyword
str-e
str-a]
(let [actual-times (count (filter (partial matching expected-args) interactions))
expected-times (expected-times-keyword {:never 0 :once 1 :twice 2})]
(if (= expected-times actual-times)
{:type :pass}
(if (empty? interactions)
{:type :fail
:result ["expected:" (fn-string function expected-args)
(name expected-times-keyword)
"\n but:" function "was never called"]}
{:type :fail
:result (apply
list
"expected:" (fn-string function expected-args)
(name expected-times-keyword)
"\n got:" (fn-string function (first interactions))
(map
(comp (partial str "\n &: ")
(partial fn-string function))
(rest interactions)))}))))
(defn compare-interaction [expected-expr f args interactions
{:keys [times-fn times]} raw-times raw-e]
(let [actual-times (count (filter (partial matching args) interactions))]
(if (times-fn times actual-times)
{:type :pass}
(if (empty? interactions)
{:type :fail
:result ["expected:" expected-expr
raw-times
"\n but:" f "was never called"]}
{:type :fail
:result (concat ["expected:" expected-expr raw-times
"\n got:" actual-times "times"]
(map (partial compare-args raw-e f args) interactions))}))))
(defn ->number-of-times [times]
(cond
(= times :never) 0
(= times :once) 1
(= times :twice) 2
(and (list? times) (number? (first times)) (= :times (last times))) (first times)
:detault `(throw (RuntimeException.
(str '~times
" is not a supported number of interactions."
" use :never, :once, :twice or"
" (x times) where x is a number - e.g. (5 times)")))))
(defn ->times [times]
(cond
(nil? times) {:times-fn = :times 1}
(keyword? times) {:times-fn = :times (->number-of-times times)}
(and (list? times) (number? (first times)) (= :times (last times))) {:times-fn =
:times (first times)}
(and (list? times) (= 'at-least (first times))) {:times-fn <=
:times (->number-of-times (last times))}
(and (list? times) (= 'at-most (first times))) {:times-fn >=
:times (->number-of-times
(last times))}
:default `(throw (RuntimeException.
(str '~times
" is not a supported number of interaction times."
" use :never, :once, :twice, (at-least ..) (at-most ..), or"
" (x times) where x is a number - e.g. (5 times).")))))
(defmacro do-fn-interaction-expect [[_ [f & args :as expected-expr] times :as e] a]
(if-not (resolve f)
`(report (compare-expr (RuntimeException. (str '~f
" needs to be a var."
" Either specify the actual var from the"
" ns or use expectations/no-op or"
" expectations/a-fn if you want to pass"
" a fn around."
)) nil '~e '~a))
`(try
(let [expected-interactions# (atom [])]
(with-redefs [~f (comp (partial swap! expected-interactions# conj) vector)]
(let [expected-args# (vector ~@args)]
(try ~a
(report (compare-interaction '~expected-expr
~(str f)
expected-args#
@expected-interactions#
~(->times times)
'~times
'~(rest (nth e 1))))
(catch Throwable t#
(report (compare-expr nil t# '~e '~a)))))))
(catch Throwable t#
(report (compare-expr t# nil '~e '~a))))))
(defn ->number-of-mock-times [times]
(cond
(= times :never) 0
(= times :once) 1
(= times :twice) 2
(and (list? times) (number? (first times)) (= :times (last times))) (first times)
:detault `(throw (RuntimeException.
(str '~times
" is not a supported number of interactions."
" use :never, :once, :twice or"
" (x times) where x is a number - e.g. (5 times)")))))
(defn ->mock-times [times]
(cond
(nil? times) '(org.mockito.Mockito/times 1)
(keyword? times) `(org.mockito.Mockito/times ~(->number-of-mock-times times))
(and (list? times) (number? (first times)) (= :times (last times))) `(org.mockito.Mockito/times ~(first times))
(and (list? times) (= 'at-least (first times))) `(org.mockito.Mockito/atLeast ~(->number-of-mock-times (last times)))
(and (list? times) (= 'at-most (first times))) `(org.mockito.Mockito/atMost ~(->number-of-mock-times (last times)))
:default `(throw (RuntimeException.
(str '~times
" is not a supported number of interaction times."
" use :never, :once, :twice, (at-least ..) (at-most ..), or"
" (x times) where x is a number - e.g. (5 times).")))))
(defmulti format-mockito-failure (comp class first vector))
(defmethod format-mockito-failure org.mockito.exceptions.verification.WantedButNotInvoked [e expected-expr times]
{:type :fail
:result ["expected:" expected-expr
times
"\n" (.getMessage e)]})
(defmethod format-mockito-failure org.mockito.exceptions.verification.junit.ArgumentsAreDifferent [e expected-expr times]
{:type :fail
:result ["expected:" expected-expr
times
"\n" (.getMessage e)]})
(defmethod format-mockito-failure org.mockito.exceptions.verification.NeverWantedButInvoked [e expected-expr times]
{:type :fail
:result ["expected:" expected-expr
times
"\n" (.getMessage e)]})
(defmethod format-mockito-failure org.mockito.exceptions.verification.TooLittleActualInvocations [e expected-expr times]
{:type :fail
:result ["expected:" expected-expr
times
"\n" (.getMessage e)]})
(defmethod format-mockito-failure org.mockito.exceptions.base.MockitoAssertionError [e expected-expr times]
{:type :fail
:result ["expected:" expected-expr
times
"\n" (.getMessage e)]})
(defmacro do-mock-interaction-expect [[_ [method o & args :as expected-expr] times :as e] a]
`(try
~a
(try
(~'verify ~o ~(->mock-times times) (~method ~@args))
(report {:type :pass})
(catch org.mockito.exceptions.base.MockitoAssertionError e#
(report (format-mockito-failure e# '~expected-expr '~times)))
(catch org.mockito.exceptions.verification.TooLittleActualInvocations e#
(report (format-mockito-failure e# '~expected-expr '~times)))
(catch org.mockito.exceptions.verification.WantedButNotInvoked e#
(report (format-mockito-failure e# '~expected-expr '~times)))
(catch org.mockito.exceptions.verification.junit.ArgumentsAreDifferent e#
(report (format-mockito-failure e# '~expected-expr '~times)))
(catch org.mockito.exceptions.verification.NeverWantedButInvoked e#
(report (format-mockito-failure e# '~expected-expr '~times)))
(catch Throwable t#
(report (compare-expr t# nil '~e '~a))))
(catch Throwable t#
(report (compare-expr nil t# '~e '~a)))))
(defmacro do-interaction-expect [[_ [f & args :as expected-expr] times :as e] a]
(if (= \. (first (name f)))
`(do-mock-interaction-expect ~e ~a)
`(do-fn-interaction-expect ~e ~a)))
(defmacro do-value-expect [e a]
`(let [e# (try ~e (catch Throwable t# t#))
a# (try ~a (catch Throwable t# t#))]
(report
(try (compare-expr e# a# '~e '~a)
(catch Throwable e2#
(compare-expr e2# a# '~e '~a))))))
(defmacro doexpect [e a]
(if (and (list? e) (= 'interaction (first e)))
`(do-interaction-expect ~e ~a)
`(do-value-expect ~e ~a)))
(defmacro expect
([a] `(expect true (if ~a true false)))
([e a]
`(def ~(vary-meta (gensym) assoc :expectation true)
(fn [] (doexpect ~e ~a)))))
(defmacro expect-let [bindings e a]
`(def ~(vary-meta (gensym) assoc :expectation true)
(fn [] (let ~bindings (doexpect ~e ~a)))))
(defmacro expect-focused
([a] `(expect-focused true (if ~a true false)))
([e a]
`(def ~(vary-meta (gensym) assoc :expectation true :focused true)
(fn [] (doexpect ~e ~a)))))
(defmacro expect-let-focused [bindings e a]
`(def ~(vary-meta (gensym) assoc :expectation true :focused true)
(fn [] (let ~bindings (doexpect ~e ~a)))))
(defmacro given [bindings form & args]
(if args
`(clojure.template/do-template ~bindings ~form ~@args)
`(clojure.template/do-template [~'x ~'y] ~(list 'expect 'y (list 'x bindings)) ~@(rest form))))
(defmacro expanding [n] (list 'quote (macroexpand-1 n)))
(->
(Runtime/getRuntime)
(.addShutdownHook
(proxy [Thread] []
(run [] (when @run-tests-on-shutdown (run-all-tests))))))
(defn interaction
([form])
([form times])) ;;; this is never used, but it's nice for auto-completion and documentation
(defn var->symbol [v]
(symbol (str (.ns v) "/" (.sym v))))
(defmulti localize class)
(defmethod localize clojure.lang.Atom [a] (atom @a))
(defmethod localize clojure.lang.Agent [a] (agent @a))
(defmethod localize clojure.lang.Ref [a] (ref @a))
(defmethod localize :default [v] v)
(defn binding-&-localized-val [var]
(when (bound? var)
(when-let [vv (var-get var)]
(when (#{clojure.lang.Agent clojure.lang.Atom clojure.lang.Ref} (class vv))
[(var->symbol var) (list 'localize (var->symbol var))]))))
(defn default-local-vals [namespaces]
(->>
namespaces
(mapcat (comp vals ns-interns))
(mapcat binding-&-localized-val)
(remove nil?)
vec))
(defmacro redef-state [namespaces & forms]
`(with-redefs ~(default-local-vals namespaces) ~@forms))
(defmacro freeze-time [time & forms]
`(try
(org.joda.time.DateTimeUtils/setCurrentMillisFixed (.getMillis ~time))
~@forms
(finally
(org.joda.time.DateTimeUtils/setCurrentMillisSystem))))
(defmacro ^{:private true} assert-args [fnname & pairs]
`(do (when-not ~(first pairs)
(throw (IllegalArgumentException.
~(str fnname " requires " (second pairs)))))
~(let [more (nnext pairs)]
(when more
(list* `assert-args fnname more)))))
(defmacro context [[sym-kw val & contexts :as args] & forms]
(assert-args context
(vector? args) "a vector for its contexts"
(even? (count args)) "an even number of forms in the contexts vector")
(if (empty? contexts)
`(~(symbol (name sym-kw)) ~val
~@forms)
`(~(symbol (name sym-kw)) ~val
(context ~(vec contexts)
~@forms))))