Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

remove scenario code

  • Loading branch information...
commit 261e4066c59da4019a6ba898fc366ca30e40876e 1 parent d1690ca
jaycfields authored
112 src/clojure/expectations/scenarios.clj
... ... @@ -1,112 +0,0 @@
1   -(ns expectations.scenarios
2   - (:require expectations)
3   - (:import [expectations ScenarioFailure])
4   - (:use clojure.walk
5   - [expectations :only [doexpect fail test-file stack->file&line report]]))
6   -
7   -(def ^{:dynamic true} *interactions*)
8   -(def in expectations/in)
9   -(defmacro expanding [n] (list 'quote (macroexpand-1 n)))
10   -
11   -(defmacro given [bindings form & args]
12   - (let [s (gensym "local")]
13   - (if args
14   - `(clojure.template/do-template ~bindings ~form ~@args)
15   - `(let [~s ~bindings]
16   - (clojure.template/do-template [~'f ~'expected]
17   - ~(list 'expect 'expected (list 'f s)) ~@(rest form))))))
18   -
19   -(defmacro stubbing [bindings & forms]
20   - (let [new-bindings (reduce (fn [a [x y]] (conj a x `(fn [& _#] ~y))) [] (partition 2 bindings))]
21   - `(with-redefs ~new-bindings ~@forms)))
22   -
23   -(defmacro expect [& args]
24   - (condp = (count args)
25   - 1 `(binding [fail (fn [test-file# test-meta# msg#] (throw (ScenarioFailure. msg#)))]
26   - (doexpect ~(first args) :once ))
27   - `(binding [fail (fn [test-file# test-meta# msg#] (throw (ScenarioFailure. msg#)))]
28   - (doexpect ~@args))))
29   -
30   -(defmacro scenario-interaction [[f & args]]
31   - `(hash-map :expectations/interaction-flag true
32   - :function ~(str f)
33   - :interactions (@*interactions* ~(str f))
34   - :expected-args (vector ~@args)))
35   -
36   -(defn detect-interactions [v]
37   - (when (seq? v)
38   - (if (= "expect" (str (first v)))
39   - (let [expect-args (flatten (next v))]
40   - (if (= "scenario-interaction" (str (first expect-args)))
41   - (second expect-args)))
42   - v)))
43   -
44   -(defn append-interaction [f-name]
45   - (fn [& args] (dosync (commute *interactions* update-in [f-name] conj args))
46   - (str f-name " result")))
47   -
48   -(defn no-op [& _])
49   -
50   -(def placeholder-fn)
51   -
52   -(def anything :anything )
53   -
54   -(defmulti localize class)
55   -(defmethod localize clojure.lang.Atom [a] (atom @a))
56   -(defmethod localize clojure.lang.Ref [a] (ref @a))
57   -(defmethod localize :default [v] v)
58   -
59   -(defn bind-to-localized [[var-name var]]
60   - (when (bound? var)
61   - (when-let [vv (var-get var)]
62   - (when (#{clojure.lang.Atom clojure.lang.Ref} (class vv))
63   - [var-name (list 'localize var-name)]))))
64   -
65   -(defn default-local-vals [ns]
66   - (if (nil? ns)
67   - []
68   - (->> (ns-interns ns)
69   - (map bind-to-localized)
70   - (reduce into []))))
71   -
72   -(defmacro localize-state [ns & forms]
73   - `(with-redefs ~(default-local-vals ns) ~@forms))
74   -
75   -(defmacro doscenario [forms & {declarative-redefs :with-redefs
76   - declarative-stubs :stubbing
77   - declarative-localize-state :localize-state
78   - :keys [freeze-time reminder]}]
79   - (let [fns (distinct (remove nil? (flatten (prewalk detect-interactions forms))))
80   - binds (reduce (fn [a f] (conj a f `(append-interaction ~(str f)))) [] fns)]
81   - `(try
82   - ~@(when freeze-time
83   - ['(import 'org.joda.time.DateTime)
84   - '(import 'org.joda.time.DateTimeUtils)
85   - `(org.joda.time.DateTimeUtils/setCurrentMillisFixed (.getMillis (condp = (class ~freeze-time)
86   - String (org.joda.time.DateTime/parse ~freeze-time)
87   - (org.joda.time.DateTime.))))])
88   - (localize-state ~declarative-localize-state
89   - (stubbing ~(vec declarative-stubs)
90   - (binding [expectations/reminder ~reminder]
91   - (with-redefs ~(vec declarative-redefs)
92   - (binding [*interactions* (ref {})]
93   - (with-redefs ~binds
94   - ~@forms))))))
95   - ~(when freeze-time
96   - '(org.joda.time.DateTimeUtils/setCurrentMillisSystem))
97   - (catch Throwable t#
98   - ~(when freeze-time
99   - '(org.joda.time.DateTimeUtils/setCurrentMillisSystem))
100   - (report {:type :error :result t#})))))
101   -
102   -(defmacro scenario [& forms]
103   - (let [[decs fs] ((juxt take-while drop-while)
104   - #(not= (class %) clojure.lang.PersistentList)
105   - (clojure.walk/prewalk-replace '{interaction scenario-interaction} forms))]
106   - `(def ~(vary-meta (gensym) assoc :expectation true)
107   - (fn [] (doscenario ~fs ~@decs)))))
108   -
109   -(defmacro scenario-focused [& forms]
110   - (let [[decs fs] ((juxt take-while drop-while) #(not= (class %) clojure.lang.PersistentList) forms)]
111   - `(def ~(vary-meta (gensym) assoc :expectation true :focused true)
112   - (fn [] (doscenario ~fs ~@decs)))))
114 test/clojure/failure/scenario_failure_examples.clj
... ... @@ -1,114 +0,0 @@
1   -(ns failure.scenario_failure_examples
2   - (:import [org.joda.time DateTime])
3   - (:use expectations.scenarios))
4   -
5   -(defn thr [] (/ 12 0))
6   -(defn two [] (thr))
7   -(defn one [] (two))
8   -
9   -(scenario
10   - (let [a (atom 0)]
11   - (swap! a inc)
12   - (expect 1 @a)
13   - (swap! a inc)
14   - (expect 2 @a)
15   - (one)
16   - (expect 1 @a)
17   - (expect "1" "2")))
18   -
19   -(scenario
20   - (let [a (atom 0)]
21   - (swap! a inc)
22   - (expect 1 @a)
23   - (swap! a inc)
24   - (expect 2 @a)
25   - (expect 1 (deref a))
26   - (expect "1" "2")))
27   -
28   -(scenario
29   - (given [x y] (expect x y)
30   - 1 88
31   - 3 77))
32   -
33   -;;; a passing one helps too
34   -(scenario
35   - (stubbing [one 99]
36   - (expect 1 (one))))
37   -
38   -(defn foo [] (println "hi"))
39   -(defn bar [] (foo))
40   -
41   -(defn foo2 [a b] (println a b))
42   -(defn bar2 [a b] (foo2 (* a a) (* b b)))
43   -
44   -(defn foo3 [& args] (println args))
45   -(defn bar3 [a b] (foo3))
46   -
47   -(def atom1 (atom 1))
48   -(def ref1 (ref 1))
49   -
50   -
51   -;; failure interaction tests
52   -(scenario
53   - (expect (interaction (foo)) :once))
54   -
55   -(scenario
56   - (expect (interaction (foo))))
57   -
58   -(scenario
59   - (bar2 1 2)
60   - (expect (interaction (foo2 1 4)) :twice))
61   -
62   -(scenario
63   - (bar2 1 2)
64   - (expect (interaction (foo2 1 4)) :never))
65   -
66   -(scenario
67   - (bar2 3 2)
68   - (bar2 2 2)
69   - (expect (interaction (foo2 1 4)) :once))
70   -
71   -(scenario
72   - :reminder "you should see this reminder"
73   - (expect (interaction (foo2 1 (/ 4 0))) :once))
74   -
75   -(scenario
76   - (bar3 1 2)
77   - (expect (interaction (foo3 1 2)) :once))
78   -
79   -(scenario
80   - (foo3 "1" 2)
81   - (expect (interaction (foo3 1 2)) :once))
82   -
83   -(scenario
84   - (expect (interaction (foo 1 2)) :once))
85   -
86   -(scenario
87   - (localize-state failure.scenario_failure_examples
88   - (swap! atom1 inc)
89   - (expect 1 @atom1)))
90   -
91   -(scenario
92   - (localize-state failure.scenario_failure_examples
93   - (dosync (alter ref1 inc))
94   - (expect 1 @ref1)))
95   -
96   -(scenario
97   - :reminder "you should see this reminder"
98   - (localize-state failure.scenario_failure_examples
99   - (swap! atom1 inc)
100   - (expect 2 @atom1))
101   - (expect 2 @atom1))
102   -
103   -(scenario
104   - (localize-state failure.scenario_failure_examples
105   - (dosync (alter ref1 inc))
106   - (expect 2 @ref1))
107   - (expect 2 @ref1))
108   -
109   -(scenario
110   - :freeze-time true
111   - (expect true
112   - (not=
113   - (DateTime.)
114   - (do (Thread/sleep 1) (DateTime.)))))
134 test/clojure/success/scenario_success_examples.clj
... ... @@ -1,134 +0,0 @@
1   -(ns success.scenario-success-examples
2   - (:import [org.joda.time DateTime])
3   - (:use expectations.scenarios))
4   -
5   -(defn foo [] (println "hi"))
6   -(defn bar [] (foo))
7   -
8   -(defn foo2 [a b] (println a b))
9   -(defn bar2 [a b] (foo2 (* a a) (* b b)))
10   -
11   -(def atom1 (atom 1))
12   -(def ref1 (ref 1))
13   -
14   -(scenario
15   - (let [a (atom 0)]
16   - (swap! a inc)
17   - (expect 1 @a)
18   - (swap! a inc)
19   - (expect 2 @a)))
20   -
21   -(scenario
22   - (expect {:a :b} (in {:a :b :c :d})))
23   -
24   -(scenario
25   - (given [x y] (expect x y)
26   - 1 1
27   - 3 3))
28   -
29   -(scenario
30   - (let [a (atom 1)]
31   - (given (swap! a inc)
32   - (expect
33   - identity 2
34   - identity 2))))
35   -
36   -(scenario
37   - (with-redefs [bar2 no-op]
38   - (expect nil? (bar2 3 4))))
39   -
40   -(scenario
41   - :with-redefs [bar2 no-op]
42   - (expect nil? (bar2 3 4)))
43   -
44   -(scenario
45   - (stubbing [foo 1
46   - bar nil]
47   - (expect 1 (foo))
48   - (expect nil? (bar))))
49   -
50   -(scenario
51   - :stubbing [foo 1
52   - bar nil]
53   - (expect 1 (foo))
54   - (expect nil? (bar)))
55   -
56   -;; success interaction tests
57   -(scenario
58   - (bar)
59   - (expect (interaction (foo)) :once))
60   -
61   -(scenario
62   - (bar)
63   - (expect (interaction (foo))))
64   -
65   -(scenario
66   - (expect (interaction (foo)) :never))
67   -
68   -(scenario
69   - ; if you expect an interaction the result of the interaction is the fn name + " result"
70   - (expect "foo result" (foo 2 4 5))
71   - (expect (interaction (foo 2 4 5))))
72   -
73   -(scenario
74   - (foo (pr-str [2 4 5]))
75   - (expect (interaction (foo "[2 4 5]"))))
76   -
77   -(scenario
78   - (bar2 1 2)
79   - (expect (interaction (foo2 1 4)) :once))
80   -
81   -(scenario
82   - (bar2 1 2)
83   - (bar2 1 2)
84   - (expect (interaction (foo2 1 4)) :twice))
85   -
86   -(scenario
87   - (bar2 1 2)
88   - (expect (interaction (foo2 anything 4)) :once))
89   -
90   -(scenario
91   - (bar2 1 2)
92   - (bar2 2 2)
93   - (expect (interaction (foo2 1 4)) :once))
94   -
95   -(scenario
96   - (localize-state success.scenario-success-examples
97   - (swap! atom1 inc)
98   - (expect 2 @atom1))
99   - (expect 1 @atom1))
100   -
101   -(scenario
102   - :reminder "you shouldn't see this reminder"
103   - :localize-state success.scenario-success-examples
104   - (swap! atom1 inc)
105   - (expect 2 @atom1))
106   -
107   -(scenario
108   - :freeze-time true
109   - :localize-state success.scenario-success-examples
110   - (expect (DateTime.) (DateTime.)))
111   -
112   -(scenario
113   - :freeze-time "2012-4-30"
114   - :localize-state success.scenario-success-examples
115   - (expect (DateTime.) (DateTime/parse "2012-4-30")))
116   -
117   -(scenario
118   - :localize-state success.scenario-success-examples
119   - (expect true
120   - (not=
121   - (DateTime.)
122   - (do (Thread/sleep 1) (DateTime.)))))
123   -
124   -(scenario
125   - (localize-state success.scenario-success-examples
126   - (dosync (alter ref1 inc))
127   - (expect 2 @ref1))
128   - (expect 1 @ref1))
129   -
130   -(defmacro a-macro [& args] `(println ~@args))
131   -
132   -(scenario
133   - (expect '(clojure.core/println 1 2)
134   - (expanding (a-macro 1 2))))

0 comments on commit 261e406

Please sign in to comment.
Something went wrong with that request. Please try again.