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

added cl-mock #392

Merged
merged 2 commits into from
Oct 6, 2021
Merged

added cl-mock #392

merged 2 commits into from
Oct 6, 2021

Conversation

mdbergmann
Copy link
Contributor

No description provided.

Copy link
Contributor Author

@mdbergmann mdbergmann left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was too lazy to go to the terminal and update my fork. So there is still this old commit that got merged already (gtwiwtg).
I had thought that GitHub is smart enough to realize this.
Do you think it's ok, or should I update my fork?

@mdbergmann
Copy link
Contributor Author

No worries, I've updated my fork.

@vindarel
Copy link
Collaborator

vindarel commented Mar 19, 2021

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
Copy link
Contributor Author

mdbergmann commented Mar 20, 2021

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
Copy link
Collaborator

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
Copy link

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

@mdbergmann
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
Contributor Author

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

@sheepduke
Copy link

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

  • Mockingbird distinguashes with-stubs (with return value) and with-mocks (implemented as stubs returning NIL). cl-mock only has with-mocks that cover both cases.
  • Mockingbird distinguashes with-stubs (lexical) and with-dynamic-stubs (dynamic). For example you have function foo that is called by bar. A lexical stub of foo will not affect bar, while a dynamic stub does. cl-mock always uses dynamic stub.
  • The answer macro provided by cl-mock can be used to implement stubs that return different values when given different arguments (keyword arguments etc are also supported). Code example: (answer (foo 10) 42) returns 42 when (foo 10) is called, but has no effect when called with 100. Mockingbird does not have this function.
  • Mockingbird has explicit support for stubing/mocking generic functions, while cl-mock should support it out of the box.

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
Copy link
Contributor Author

@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
Copy link
Collaborator

vindarel commented Oct 6, 2021

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).

@vindarel vindarel merged commit 9e23c47 into CodyReichert:master Oct 6, 2021
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

Successfully merging this pull request may close these issues.

3 participants