Skip to content

Using private functions in prerequisites

marick edited this page Feb 28, 2013 · 2 revisions

Executable examples

Earlier, you saw three ways of using private functions in checkables: referring to them via their var, testable-privates, and expose-testables. Only the first way works when you want use a private function as a prerequisite:

    (fact
      (counter 4) => 8
      (provided
        (#'scratch.core/super-secret-function 4) => 4))

Why don't the other ways work?

testable-privates and expose-testables create new vars in the current namespace. They have the same names as the private vars, and they point at the same function, but they're different objects.

Prerequisites work by temporarily pointing vars at new functions. Therefore, in this code:

    (ns scratch.core-tests)
    (testable-privates scratch.core super-secret-function)
    (fact
      (counter 4) => 8
      (provided
        (super-secret-function 4) => 4))

... the prerequisite overrides the test namespace's var (#'scratch.core-tests/super-secret-function) while leaving the original var pointing to the original function. Since the code for counter is in the source namespace, its call to super-secret-function uses the un-overridden var (#'scratch.core/super-secret-function).

Clone this wiki locally