Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot use prerequisites for functions with type hinted vars #466

Open
viebel opened this issue Apr 16, 2020 · 2 comments
Open

Cannot use prerequisites for functions with type hinted vars #466

viebel opened this issue Apr 16, 2020 · 2 comments

Comments

@viebel
Copy link

viebel commented Apr 16, 2020

I couldn't find a way to use prerequisites for functions with type hinted vars. The root cause is related to how type hint works and it comes down to some limitation of with-redefs as explained in #295.

However, as a midje user I am expecting some workaround (when using with-redefs the workaround is to type hint the mocking function).

Can someone think of a way to define prerequisites for the following scenario?

There is a workaround exposed here but it requires to modify the code of the function that is tested which is not desirable in my case.

Can someone think of a workaround (or maybe a fix in midje) that will allow me to use prerequisites for type hinted functions without modifying the source code.

Here is a simple illustration of my use case:

This code causes an exception:

(defn bar [^String x ^long x])
(defn foo []
  (bar "a" 1))

(fact
 (foo ) => nil
 (provided (bar "a" 1) => nil))
java.lang.ClassCastException: clojure.lang.AFunction$1 cannot be cast to clojure.lang.IFn$OLO

While this code works as expected

(defn bar [^String x ^long x])
(defn foo []
  (#'bar "a" 1))

(fact
 (foo ) => nil
 (provided (bar "a" 1) => nil))
@viebel
Copy link
Author

viebel commented Oct 1, 2020

Could anyone help on this one?

@philomates
Copy link
Collaborator

I can confirm that this issue is also present in mockfn, which is a test-framework agnostic reimplementation of the best parts (in my opinion) of Midje's mocking capabilities

If you want to explore coming up with a fix for this, I would recommend trying to get it going in that project because it has a much smaller codebase than Midje

to reproduce there:

(ns mockfn.scratch
  (:require [mockfn.macros :refer [providing]]))

(defn bar [^String x ^long x])
(defn foo []
  (bar "a" 1))

(providing [(bar "a" 1) nil]
 (foo))

This is a cornercase that seems to be a nuance of how Clojure is implemented (as far as I understand), so I personally don't plan to dive into this at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants