CodyReichert / awesome-cl

A curated list of awesome Common Lisp frameworks, libraries and other shiny stuff.
https://awesome-cl.com
Other
2.48k stars 184 forks source link

added cl-mock #392

Closed mdbergmann closed 2 years ago

mdbergmann commented 3 years ago

No worries, I've updated my fork.

vindarel commented 3 years ago

Could we choose one maybe?

I thought mockingbird had more features, but actually they have the same set? Mockingbird allows to check that a function was called (call-times-for) and with which arguments (verify-nth-call-args-for). cl-mock has this with invocations.

It seems they both do the difference between lexical and dynamic mocks.

I find mockingbird's README way cleaner and the projects looks a tiny bit more lively (3 more stars, 13 more commits and its last commit is more recent by one year \o/ ).

Does cl-mock do something more? Where is its pattern-matching thingy explained?

And BTW, it still relies on and links to Optima which is deprecated.

mdbergmann commented 3 years ago

cl-mock can capture the call arguments and it allows to verify and decide for a return in the answer call, like this:

(answer (atom-feed:generate-feed feed-model)
      (if (not (hash-table-p feed-model))
          (cons :nok "Wrong input type")
          (cons :ok "<feed")))

feed-model is the captured call argument. Here you could use pattern matching.

In mockingbird you have to setup separate with- contexts.

But don't worry. We don't need to add it. I thought there could be two (it seems like they are the only ones). So just close the PR if you think Mockingbird should stay. In any case, I'll check if I can make a PR to cl-mock to change the use of Optima to Trivia.

vindarel commented 3 years ago

In any case, I'll check if I can make a PR to cl-mock to change the use of Optima to Trivia.

Cool. I leave this open.

sheepduke commented 2 years ago

It seems that Mockingbird supports mocking methods (apart from trivial functions). Does cl-mock supports it too?

mdbergmann commented 2 years ago

AFAIK methods in Common Lisp are just implementations of generic functions. I think mocking ordinary functions and generic functions is not very different. From the cl-mock readme:

This small library provides a way to replace the actual implementation of either regular or generic functions with mocks.

mdbergmann commented 2 years ago

While that is written in the readme, there is an issue that mentions this as a TODO feature. So I'm not sure. Give it a try.

mdbergmann commented 2 years ago

But it seems to work:

CL-USER> (defgeneric foo (a))
#<STANDARD-GENERIC-FUNCTION FOO #x302001AD07CF>
CL-USER> (cl-mock:with-mocks ()
           (cl-mock:answer (foo a-var) "a-var-mock")
           (print (foo "some-string")))
;Compiler warnings :
;   In an anonymous lambda form inside an anonymous lambda form: Unused lexical variable A-VAR

"a-var-mock" 
"a-var-mock"

A method can be additionally defined and it still works:

CL-USER> (defmethod foo (a) a)
#<STANDARD-METHOD FOO (T)>
mdbergmann commented 2 years ago

btw: cl-mock was changed to use Trivia some time ago.

sheepduke commented 2 years ago

@vindarel I tried both Mockdingbird and cl-mock and found something below:

Having said that, I would suggest selecting cl-mock over Mockingbird in the awesome-cl list and introduce cl-mock in the cookbook.

What do you think?

@mdbergmann Please correct me if I made any mistake. :-) (And I appreciate your time to experiment with the generic function thing.)

mdbergmann commented 2 years ago

@sheepduke I haven't looked at Mockingbird, but I trust your comparison is OK. I think both should be added to awesome-cl as probably both have their strengths and weaknesses and maybe it's a matter of choosing.

vindarel commented 2 years ago

Ok then, thank you for the detailed comparison. The added line could redirect to this discussion for more details I guess (or a future blog post/gist of yours).