Skip to content

Commit

Permalink
Don't let inlined functions be faked.
Browse files Browse the repository at this point in the history
  • Loading branch information
marick committed Nov 9, 2011
1 parent 71bac38 commit 088ec48
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions HISTORY.md
@@ -1,4 +1,5 @@
1.3-alpha5
* Prevents you from overriding functions that the compiler inlines.
* Now works properly with aot-compilation. More properly, at least.
* partial prerequisites
https://github.com/marick/Midje/wiki/Partial-prerequisites
Expand Down
10 changes: 10 additions & 0 deletions src/midje/error_handling/semi_sweet_errors.clj
Expand Up @@ -8,13 +8,23 @@
[midje.ideas.metaconstants :only [metaconstant-symbol?]]
[midje.ideas.arrow-symbols :only [=contains=>]]))

(defn- compiler-will-inline-fn? [fn]
(contains? (meta (resolve fn)) :inline))

(defmethod validate "fake" [[_fake_ & fake-form :as form]]
(let [funcall (first fake-form)]
(cond (not (list? funcall))
(user-error-report-form
form
"The left-hand-side of a prerequisite must look like a function call."
(cl-format nil "`~S` doesn't." funcall))

(compiler-will-inline-fn? (first funcall))
(user-error-report-form
form
(cl-format nil "You cannot override the function `~S`: it is inlined by the Clojure compiler." (first funcall)))


:else
fake-form)))

Expand Down
5 changes: 0 additions & 5 deletions test/midje/error_handling/t_semi_sweet_errors.clj
Expand Up @@ -25,11 +25,6 @@

;;; Full-bore tests.

(defmacro user-error-with-notes [& notes]
`(just (contains {:notes (just ~@notes)
:type :user-error})))


(after-silently
(expect (+ 1 2) =>)
(expect @reported => (user-error-with-notes
Expand Down
11 changes: 11 additions & 0 deletions test/midje/internal_ideas/t_fakes.clj
Expand Up @@ -158,6 +158,17 @@
text => #"clojure\.core/every\?"
text => #"interferes with.*Midje")))

;; And inlined functions can't be faked

(defn doubler [n] (+ n n))

(after-silently
(fact
(doubler 3) => 0
(provided
(+ 3 3) => 0))
(fact @reported => (user-error-with-notes #"inlined")))

;; How it works

(defn ^{:dynamic true} function-symbol-of-interest [n] n)
Expand Down
5 changes: 5 additions & 0 deletions test/midje/test_util.clj
Expand Up @@ -68,3 +68,8 @@

(defn at-line [line-no form] (with-meta form {:line line-no}))

(defmacro user-error-with-notes [& notes]
`(just (contains {:notes (just ~@notes)
:type :user-error})))


0 comments on commit 088ec48

Please sign in to comment.