Skip to content

Commit

Permalink
Merge branch 'master' of github.com:marick/Midje
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBaranosky committed Oct 24, 2011
2 parents 216809b + 25d5bca commit 108ff23
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 4 deletions.
7 changes: 7 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
1.3-alpha5
* =expands-to=> for testing macros (in a style derived from
/Let Over Lambda/. (via Phil Calçado)
https://github.com/marick/Midje/wiki/Macros
* You can write (f) => anything :never instead :times 0.
Alex Baranovsky

1.3-alpha4
--------
* Believed to run under Clojure 1.3
Expand Down
39 changes: 39 additions & 0 deletions leiningen/timings.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
;; -*- indent-tabs-mode: nil -*-

(ns leiningen.timings
(:refer-clojure :exclude [test])
(:use [leiningen.util.ns :only [namespaces-in-dir]]
[leiningen.test :only [*exit-after-tests*]]
[leiningen.compile :only [eval-in-project]]
[clojure.set :only [difference]]))

(defn require-namespaces-form [namespaces]
`(let [array# (doall (map (fn [_#] (let [start# (.getTime (java.util.Date.))]
(when (= clojure.lang.MultiFn (type clojure.test/report))
(defmethod clojure.test/report :begin-test-ns [m#]))

(alter-var-root (var clojure.test/*report-counters*)
(fn [_#] (ref clojure.test/*initial-report-counters*)))

(dosync (alter @#'clojure.core/*loaded-libs* difference (set '~namespaces)))
(doseq [n# '~namespaces] (require n# :reload))

(- (.getTime (java.util.Date.)) start#)))
(range 7)))]
(println (sort array#))))

(defn timings
"Run both Midje and clojure.test tests.
Namespaces are looked up in both the src/ and test/ subdirectories.
If no namespaces are given, runs tests in all namespaces."
[project & namespaces]
(let [desired-namespaces (if (empty? namespaces)
(concat (namespaces-in-dir (:test-path project))
(namespaces-in-dir (:source-path project)))
(map symbol namespaces))]
(eval-in-project project
(require-namespaces-form desired-namespaces)
nil
nil
'(require '[clojure walk template stacktrace test string set]))))

2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject midje "1.3-alpha4"
(defproject midje "1.3-alpha5"
:description "A TDD library for Clojure, with an emphasis on mocks"
:dependencies [[org.clojure/clojure "[1.2.0],[1.2.1],[1.3.0]"]
; :dependencies [[org.clojure/clojure "[1.2.0],[1.2.1]"]
Expand Down
1 change: 1 addition & 0 deletions src/midje/ideas/arrow_symbols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
(def =not=> "=not=>")
(def =deny=> "=deny=>")
(def =streams=> "=streams=>")
(def =expands-to=> "=expands-to=>")
(def =future=> "=future=>")
(def =contains=> "=contains=>")

Expand Down
2 changes: 1 addition & 1 deletion src/midje/ideas/arrows.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
(:require [clojure.zip :as zip]))

;; Arrow groupings
(def expect-arrows [=> =not=> =deny=> =future=>])
(def expect-arrows [=> =not=> =deny=> =future=> =expands-to=>])
(def fake-arrows [=> =contains=> =streams=>])
(def all-arrows (concat expect-arrows fake-arrows))

Expand Down
8 changes: 8 additions & 0 deletions src/midje/semi_sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
;; but those fail for reasons I don't understand. Bah.
(defn- check-for-arrow [arrow]
(get {=> :check-match
=expands-to=> :check-match
=not=> :check-negated-match
=deny=> :check-negated-match} (name arrow)))

Expand All @@ -57,6 +58,7 @@
(get {=> :expect*
=not=> :expect*
=deny=> :expect*
=expands-to=> :expect-macro*
=future=> :report-future-fact} (name arrow)))

(defmulti ^{:private true} expect-expansion handling-of-check-part)
Expand All @@ -66,6 +68,12 @@
`(let [check# (unprocessed-check ~call-form ~arrow ~expected-result ~overrides)]
(expect* check# (vector ~@fakes))))

(defmethod expect-expansion :expect-macro*
[call-form arrow expected-result fakes overrides]
(let [expanded-macro `(macroexpand-1 '~call-form)
escaped-expected-result `(quote ~expected-result)]
(expect-expansion expanded-macro => escaped-expected-result fakes overrides)))

(defmethod expect-expansion :report-future-fact
[call-form arrow expected-result fakes overrides]
`(let [check# (unprocessed-check ~call-form ~arrow ~expected-result ~overrides)]
Expand Down
38 changes: 38 additions & 0 deletions test/behaviors/t_macros.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns behaviors.t-macros
(:use [midje sweet test-util]))

;; Example from wiki
(defmacro my-if [test true-branch false-branch]
`(cond ~test ~true-branch :else ~false-branch))

(fact
(my-if (odd? 1) 1 2) =expands-to=> (clojure.core/cond (odd? 1) 1 :else 2))



(defmacro macro-with-expander [arg1 & other-args]
(let [modified (str arg1 "-suffix")]
`(str ~modified "(was " ~arg1 ")"
(apply + '~other-args))))

(defn times-ten [arg] (* 10 arg))

(defmacro macro-with-only-expansion [& args]
`(map times-ten ~@args))

(defmacro macro-calling-other-macro [arg others]
`(str ~arg (first (macro-with-only-expansion (list ~@others)))))

(facts "about expecting on macro expansion"
(fact "macros with expansion code only work"
(macro-with-only-expansion (list 1 2 3)) => (list 10 20 30)
(macro-with-only-expansion (list 1 2 3)) =expands-to=> (clojure.core/map behaviors.t-macros/times-ten (list 1 2 3)))

(fact "macros with expander code work"
(macro-with-expander 666 10 20 30) => "666-suffix(was 666)60"
(macro-with-expander 666 10 20 30) =expands-to=> (clojure.core/str "666-suffix" "(was " 666 ")" (clojure.core/apply clojure.core/+ '(10 20 30))))

(fact "macros calling other macros are macroexpand-1"
(macro-calling-other-macro 999 [ 10 20 30]) => "999100"
(macro-calling-other-macro 999 [ 10 20 30]) =expands-to=> (clojure.core/str 999 (clojure.core/first (behaviors.t-macros/macro-with-only-expansion (clojure.core/list 10 20 30))))))

17 changes: 15 additions & 2 deletions test/midje/t_semi_sweet.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
(facts "about arrows"
(let [result (map check-for-arrow
'(=> midje.semi-sweet/=> midje.sweet/=>
=not=> midje.semi-sweet/=not=> midje.sweet/=not=>))]
=not=> midje.semi-sweet/=not=> midje.sweet/=not=>
=expands-to=> midje.semi-sweet/=expands-to=> midje.sweet/=expands-to=>))]
(fact result => [:check-match :check-match :check-match
:check-negated-match :check-negated-match :check-negated-match])))
:check-negated-match :check-negated-match :check-negated-match
:check-match :check-match :check-match])))

(fact "separating overrides of an #expect from fakes"
;; The lets are because fact isn't smart enough not to add overrides to fake call otherwise.
Expand Down Expand Up @@ -296,3 +298,14 @@
skippable => is-semi-sweet-keyword?)))


(defmacro some-macro [arg] `(+ 100 200 ~arg))

(facts "about =expands-to=>"
(fact "calls blah with quoted code"
(some-macro 8) =expands-to=> (clojure.core/+ 100 200 8))
(fact "fails if expansion does not match expected list"
(after-silently
(expect (some-macro 1) =expands-to=> (clojure.core/- 100 200 1))
@reported => (one-of (contains {:type :mock-expected-result-failure
:actual `(clojure.core/+ 100 200 1)
:expected `(clojure.core/- 100 200 1)})))))

0 comments on commit 108ff23

Please sign in to comment.