Permalink
Browse files

Move production-mode to sweet; make it work right.

  • Loading branch information...
1 parent 2c50684 commit e1cc4e461d27e00256065ce23be01a68593f7bb5 @marick committed May 7, 2013
@@ -1,16 +1,19 @@
(ns midje.production-mode)
+;; This function is needed because midje.sweet is not fully
+;; loaded when this file is loaded.
(letfn [(value-within [namespace-symbol variable-symbol]
(if-let [namespace (find-ns namespace-symbol)]
(var-get ((ns-map namespace) variable-symbol))
- true))]
+ (throw (Error. (str "It should be impossible for production mode to be checked before "
+ namespace-symbol
+ " is loaded.")))))]
(defn user-desires-checking?
- "If any of clojure.test/*load-tests* or midje.sweet/*include-midje-checks*
- or midje.semi-sweet/*include-midje-checks* are false, facts won't run."
+ "If clojure.test/*load-tests* or midje.sweet/include-midje-checks
+ is false, facts won't run."
[]
(and (value-within 'clojure.test '*load-tests*)
- (value-within 'midje.sweet '*include-midje-checks*)
- (value-within 'midje.semi-sweet '*include-midje-checks*))))
+ (value-within 'midje.sweet 'include-midje-checks))))
View
@@ -27,21 +27,6 @@
;;; Interface: unfinished
-;;; Interface: production mode
-
-(defonce
- ^{:doc "True by default. If set to false, Midje checks are not
- included into production code, whether compiled or loaded."
- :dynamic true}
- *include-midje-checks* :original-truthy-value)
-
-(set-validator! #'*include-midje-checks*
- (fn [val]
- (when-not (= val :original-truthy-value)
- (deprecate "Set #'midje.sweet/*include-midje-checks* instead of the #'midje.semi-sweet version."))
- true))
-
-
;;; Interface: Main macros
(defmacro fake
@@ -64,8 +49,8 @@
results are as expected. If the expected results are a function, it
will be called with the actual result as its single argument.
- To strip tests from production code, set either clojure.test/*load-tests*,
- midje.semi-sweet/*include-midje-checks*, or midje.sweet/*include-midje-checks* to false."
+ To strip tests from production code, set either clojure.test/*load-tests*
+ or midje.sweet/include-midje-checks to false."
{:arglists '([call-form arrow expected-result & fakes+overrides])}
[& _]
(deprecate "`expect` (and the entire midje.semi-sweet namespace) will be removed in 1.6.")
View
@@ -38,6 +38,26 @@
(immigrate 'midje.checkers)
(immigrate 'midje.semi-sweet)
+(def include-midje-checks
+ "True by default. If set to false, Midje checks are not
+ included into production code, whether compiled or loaded.
+
+ Note that the variable must be set, as with `alter-var-root`
+ or `def`, not bound, as with `binding`."
+ true)
+
+(defonce
+ ^{:doc "This variable is defunct. Use `include-midje-checks` instead."
+ :dynamic true}
+ *include-midje-checks* :original-truthy-value)
+
+(set-validator! #'*include-midje-checks*
+ (fn [val]
+ (when-not (= val :original-truthy-value)
+ (emit/fail {:type :parse-error
+ :notes ["*include-midje-checks* is defunct. Use `include-midje-checks` instead."]
+ :position (position/compile-time-fallback-position)}))
+ true))
(defn- unfinished* [names]
(pile/macro-for [name names]
@@ -57,12 +77,6 @@
throw Errors if ever called."
[& names] (unfinished* names))
-;; Following two are required because `intern` doesn't transfer "dynamicity".
-(def ^{:doc "True by default. If set to false, Midje checks are not
- included into production code, whether compiled or loaded."
- :dynamic true}
- *include-midje-checks* *include-midje-checks*)
-
(defalias before background/before)
(defalias after background/after)
(defalias around background/around)
@@ -117,9 +131,8 @@
Note that in this case the square brackets can be omitted."
[background-forms & foreground-forms]
- (if (user-desires-checking?)
- (error/parse-and-catch-failure &form #(parse-facts/midjcoexpand &form))
- `(do ~@foreground-forms)))
+ (when (user-desires-checking?)
+ (error/parse-and-catch-failure &form #(parse-facts/midjcoexpand &form))))
(defmacro with-state-changes
"Describe how state should change before or after enclosed facts. Example:
@@ -209,8 +222,9 @@
(defmacro fact-group
"Supply default metadata to all facts in the body."
[& forms]
- (let [[metadata body] (parse-metadata/separate-multi-fact-metadata forms)]
- (parse-metadata/with-wrapped-metadata metadata
- (parse-facts/midjcoexpand `(do ~@body)))))
+ (when (user-desires-checking?)
+ (let [[metadata body] (parse-metadata/separate-multi-fact-metadata forms)]
+ (parse-metadata/with-wrapped-metadata metadata
+ (parse-facts/midjcoexpand `(do ~@body))))))
@@ -1,6 +0,0 @@
-
-(require 'clojure.test)
-;; This file is used to check whether checks can be compiled out.
-(expect (throw (Exception. "Should never be called")) => true)
-(clojure.test/deftest tests-can-be-compiled-out
- (expect false => true))
@@ -56,13 +56,13 @@
;; When Midje is in "production mode", `defrecord-openly` does the same thing as `defrecord`, so
;; the speed gains of inlining remain:
-(alter-var-root #'*include-midje-checks* (constantly false))
+(alter-var-root #'include-midje-checks (constantly false))
(defrecord-openly MyRecord [a b]
Addable
(add-fields [this] (+ a b)))
-(alter-var-root #'*include-midje-checks* (constantly true))
+(alter-var-root #'include-midje-checks (constantly true))
(silent-fact
(let [rec (MyRecord. 1 2)]
@@ -0,0 +1,42 @@
+(ns as-documentation.production-mode
+ (:use midje.sweet))
+
+;;; People sometimes intermix facts with production code. When you actually deploy the production code,
+;;; you may not want the Midje facts checked at load time. "Production mode" prevents that.
+
+;;; (Note: checking facts at load time may do little harm: why not run
+;;; the tests when you deploy? Loaded facts don't take up too much space.)
+
+;;; Either midje.sweet/*include-midje-checks* or clojure.test/*load-tests* can be used to
+;;; turn tests off.
+
+(alter-var-root #'midje.sweet/include-midje-checks (constantly false))
+
+(fact
+ (throw (new Error "Should not be called.")))
+
+(facts
+ (throw (new Error "Should not be called.")))
+
+(fact-group
+ (throw (new Error "Should not be called.")))
+
+(with-state-changes []
+ (throw (new Error "Should not be called.")))
+
+(alter-var-root #'midje.sweet/include-midje-checks (constantly true))
+(alter-var-root #'clojure.test/*load-tests* (constantly false))
+
+(fact
+ (throw (new Error "Should not be called.")))
+
+(facts
+ (throw (new Error "Should not be called.")))
+
+(fact-group
+ (throw (new Error "Should not be called.")))
+
+(with-state-changes []
+ (throw (new Error "Should not be called.")))
+
+(alter-var-root #'clojure.test/*load-tests* (constantly true))
@@ -1,22 +0,0 @@
-(ns behaviors.t-background-production-mode
- (:use [midje.sweet]))
-
-(alter-var-root #'*include-midje-checks* (constantly false))
-
-(fact (/ 1 0) => 33)
-(facts (/ 1 0) => 33)
-(background (broken-form ...meta...) =>)
-(fact (+ 1 (broken-form ...meta...) => 33))
-
-(against-background [(broken-form2 ...meta2...) =>]
- (fact (+ 1 (broken-form-2 ...meta2...)) => 33)
- (def #^:dynamic *localvar* "this must nevertheless be defined"))
-
-(str *localvar*) ;; Non-fact forms are still processed.
-
-
-(tabular (fact (inc ?n) => ?n)
- ?n
- 1)
-
-(alter-var-root #'*include-midje-checks* (constantly true))
@@ -24,12 +24,15 @@
unexpanded-out-rec (list* 'clojure.core/defrecord 'R type-or-record-tail)]
(fact "normally, types and records are rewritten"
(macroexpand-1 in-typ) =not=> unexpanded-out-typ
- (macroexpand-1 in-rec) =not=> unexpanded-out-rec
+ (macroexpand-1 in-rec) =not=> unexpanded-out-rec)
- "but they remain the same when the user doesn't want checking"
- (with-bindings {#'midje.sweet/*include-midje-checks* false}
+ (fact "but they remain the same when the user doesn't want checking"
+ (try
+ (alter-var-root #'midje.sweet/include-midje-checks (constantly false))
(macroexpand-1 in-typ) => unexpanded-out-typ
- (macroexpand-1 in-rec) => unexpanded-out-rec)))
+ (macroexpand-1 in-rec) => unexpanded-out-rec
+ (finally
+ (alter-var-root #'midje.sweet/include-midje-checks (constantly true))))))
View
@@ -90,21 +90,6 @@
(str "It should be the identity for value " v) ;; doc string needn't be constant
(a-fun v) => v))
-(binding [midje.semi-sweet/*include-midje-checks* false]
- (load "sweet_compile_out"))
-
-
-;; For the time being, we still support semi-sweet macros.
-;; The files to be loaded will blow up unless we're in production-mode.
-(binding [clojure.test/*load-tests* false]
- (load "semi_sweet_compile_out"))
-
-(binding [midje.semi-sweet/*include-midje-checks* false]
- (load "semi_sweet_compile_out"))
-
-
-
-
;; It doesn't matter which namespace the => is in
(silent-fact (+ 1 1) midje.semi-sweet/=> 3)
(note-that fact-fails, (fact-actual 2), (fact-expected 3))

0 comments on commit e1cc4e4

Please sign in to comment.