Skip to content

Commit

Permalink
Can fake functions in other namespaces (Wilkes Joiner)
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Jul 13, 2010
1 parent fc8a5e1 commit 1748147
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
@@ -1 +1,2 @@
* Brian Marick
* Wilkes Joiner
3 changes: 3 additions & 0 deletions HISTORY.md
@@ -0,0 +1,3 @@
= 0.2.0

* Can fake functions from other namespaces. (Wilkes Joiner)
Binary file modified downloads/semi-sweet-examples.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions examples/semi-sweet-examples/lein-expected-output
Expand Up @@ -11,7 +11,7 @@
[null] FAIL at (core_test.clj:40)
[null] Actual result did not pass expected function.
[null] expected function: (exactly odd?)
[null] actual result: #<core$even_QMARK___4680 clojure.core$even_QMARK___4680@7c33788d>
[null] actual result: #<core$even_QMARK___4680 clojure.core$even_QMARK___4680@c163956>
[null] ^^^^ The previous failure was expected ^^^^
[null] FAIL at (core_test.clj:51)
[null] Actual result did not pass expected function.
Expand All @@ -22,9 +22,9 @@
[null] This expectation was never satisfied:
[null] (first-fake 3) should be called at least once.
[null] ^^^^ The previous failure was expected ^^^^
[null] Ran 9 tests containing 10 assertions.
[null] Ran 10 tests containing 12 assertions.
[null] 5 failures, 0 errors.
[null] --------------------
[null] Total:
[null] Ran 9 tests containing 10 assertions.
[null] Ran 10 tests containing 12 assertions.
[null] 5 failures, 0 errors.
4 changes: 2 additions & 2 deletions examples/semi-sweet-examples/project.clj
@@ -1,7 +1,7 @@
(defproject semi-sweet-simple "0.2.0"
(defproject semi-sweet-simple "0.3.0"
:description "An example of using Midje semi-sweet mocking"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]]
:dev-dependencies [[midje "0.1.1"]]
:dev-dependencies [[midje "0.2.0"]]
)

2 changes: 1 addition & 1 deletion examples/semi-sweet-examples/run
@@ -1,2 +1,2 @@
cd test
java -cp ../lib/clojure-1.1.0.jar:../lib/clojure-contrib-1.1.0.jar:../lib/midje-0.1.1.jar clojure.main -i semi_sweet_simple/core_test.clj -e '(ns semi-sweet-simple.core-test)(run-tests)'
java -cp ../lib/clojure-1.1.0.jar:../lib/clojure-contrib-1.1.0.jar:../lib/midje-0.2.0.jar clojure.main -i semi_sweet_simple/core_test.clj -e '(ns semi-sweet-simple.core-test)(run-tests)'
6 changes: 3 additions & 3 deletions examples/semi-sweet-examples/run-expected-output
Expand Up @@ -15,7 +15,7 @@ expected function: odd?
FAIL at (core_test.clj:40)
Actual result did not pass expected function.
expected function: (exactly odd?)
actual result: #<core$even_QMARK___4680 clojure.core$even_QMARK___4680@19e09a4>
actual result: #<core$even_QMARK___4680 clojure.core$even_QMARK___4680@1dfd868>
^^^^ The previous failure was expected ^^^^

FAIL at (core_test.clj:51)
Expand All @@ -29,6 +29,6 @@ This expectation was never satisfied:
(first-fake 3) should be called at least once.
^^^^ The previous failure was expected ^^^^

Ran 9 tests containing 10 assertions.
Ran 10 tests containing 12 assertions.
5 failures, 0 errors.
{:type :summary, :test 9, :pass 5, :fail 5, :error 0}
{:type :summary, :test 10, :pass 7, :fail 5, :error 0}
34 changes: 23 additions & 11 deletions examples/semi-sweet-examples/test/semi_sweet_simple/core_test.clj
Expand Up @@ -117,19 +117,30 @@
;; for that. If you do, let me know.


;; Right now, you can't fake a function outside the current
;; namespace. There's a saying from the OO world: "don't mock objects
;; you don't own", so maybe this limitation is OK. If you want it, let
;; me know, but I'm not sure it's possible.
(comment
(defn function-under-test-5 [arg]
(inc arg))

(deftest example-of-mocking-functions-from-another-namespace
(expect (function-under-test-5 1) => 5
(fake (clojure.core/inc 1) => 5)))
;; You can fake a function outside the current namespace. Suppose we
;; have a function that operates on two sets. We want to override
;; clojure.set/intersection so that our tests can only talk about properties
;; of tests, rather than have to laboriously construct actual sets with those
;; properties. So we pass in descriptive strings and fake out intersection.
;;
;; There is (will be) more support for this style in midje.sweet.

(use 'clojure.set)
(defn set-handler [set1 set2]
(if (empty? (intersection set1 set2))
set1
(intersection set1 set2)))

(deftest example-of-faking-function-from-another-namespace
"For disjoint sets, return the first."
(expect (set-handler "some set" "some disjoint set") => "some set"
(fake (intersection "some set" "some disjoint set") => #{}))
"For overlapping sets, return the intersection"
(expect (set-handler "set" "overlapping set") => #{"intersection"}
(fake (intersection "set" "overlapping set") => #{"intersection"}))
)


;; My development style is "programming by wishful thinking"
;; (Sussman). Suppose I'm writing function (quux). If I hit anything
;; hard, I say "I really really believe there's already a function
Expand Down Expand Up @@ -162,5 +173,6 @@
(example-of-a-simple-fake-failure)
(example-of-multiple-faked-functions)
(example-of-interesting-functional-args)
(example-of-faking-function-from-another-namespace)
)

2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject midje "0.1.1"
(defproject midje "0.2.0"
:description "A mock/stubbing library for Clojure"
:dependencies [[org.clojure/clojure "1.1.0"]
[org.clojure/clojure-contrib "1.1.0"]])
Expand Down
15 changes: 15 additions & 0 deletions test/midje/semi_sweet_test.clj
Expand Up @@ -140,3 +140,18 @@
(let [myfun (fn [x] (list x))]
(expect (myfun 1) => :list-called
(fake (list 1) => :list-called))))

(use 'clojure.set)
(defn set-handler [set1 set2]
(if (empty? (intersection set1 set2))
set1
(intersection set1 set2)))

(deftest fake-function-from-other-namespace-used-in-var
(expect (set-handler 'set 'disjoint-set) => 'set
(fake (intersection 'set 'disjoint-set) => #{}))
(expect (set-handler 'set 'overlapping-set) => #{'intersection}
(fake (intersection 'set 'overlapping-set) => #{'intersection}))
)


0 comments on commit 1748147

Please sign in to comment.