Skip to content
Browse files

Merge branch 'master' of github.com:marick/Midje

  • Loading branch information...
2 parents 216809b + 25d5bca commit 108ff232a1b44090f1fcd2e6871d168618ba408f @AlexBaranosky AlexBaranosky committed Oct 23, 2011
View
7 HISTORY.md
@@ -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
View
39 leiningen/timings.clj
@@ -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]))))
+
View
2 project.clj
@@ -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]"]
View
1 src/midje/ideas/arrow_symbols.clj
@@ -4,6 +4,7 @@
(def =not=> "=not=>")
(def =deny=> "=deny=>")
(def =streams=> "=streams=>")
+(def =expands-to=> "=expands-to=>")
(def =future=> "=future=>")
(def =contains=> "=contains=>")
View
2 src/midje/ideas/arrows.clj
@@ -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))
View
8 src/midje/semi_sweet.clj
@@ -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)))
@@ -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)
@@ -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)]
View
38 test/behaviors/t_macros.clj
@@ -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))))))
+
View
17 test/midje/t_semi_sweet.clj
@@ -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.
@@ -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.
Something went wrong with that request. Please try again.