Repository navigation
Fold keywords into an options hash for replaced new and class methods - #34
Merged
Merged
Conversation
Replacing a class swaps `new` and its class methods for generic
`(*args, **kwargs)` lambdas, so Ruby always splits brace-less keywords
like `Klass.new(item: "hello")` into kwargs. Mocktail then validated
those kwargs against the real method, and when that method takes an
options hash (`def initialize(opts = {})`) rather than keywords, it
raised `ArgumentError: unknown keyword: :item`, even though Ruby 3
passes keywords to a method with no keyword params as a trailing
positional hash. Instance-method doubles never had this problem
because their lambdas are generated from the real signature.
Add FoldsKeywordsIntoOptionsHash, which applies Ruby's rule: fold
non-empty kwargs into a trailing hash when the original method has no
:key, :keyreq, :keyrest, or :nokey params. It reads params through
GrabsOriginalMethodParameters so Sorbet-wrapped methods report their
real signature. Call it before building the Call in HandlesDryNewCall
and in the mocked branch of replaced class methods, so validation,
call logging, and stub matching all see the same shape instance
doubles already record.
test_replace_class asserted that `House.room(wrong: :arg)` raises for
`def self.room(room_name)`, which real Ruby accepts, so it now asserts
on a call Ruby also rejects.
Picks up the failing test from #20 and makes it pass.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replacing a class swaps
newand its class methods for generic(*args, **kwargs)lambdas, so Ruby always splits brace-less keywords likeKlass.new(item: "hello")into kwargs. Mocktail then validated those kwargs against the real method, and when that method takes an options hash (def initialize(opts = {})) rather than keywords, it raisedArgumentError: unknown keyword: :item, even though Ruby 3 passes keywords to a method with no keyword params as a trailing positional hash. Instance-method doubles never had this problem because their lambdas are generated from the real signature.Add FoldsKeywordsIntoOptionsHash, which applies Ruby's rule: fold non-empty kwargs into a trailing hash when the original method has no :key, :keyreq, :keyrest, or :nokey params. It reads params through GrabsOriginalMethodParameters so Sorbet-wrapped methods report their real signature. Call it before building the Call in HandlesDryNewCall and in the mocked branch of replaced class methods, so validation, call logging, and stub matching all see the same shape instance doubles already record.
test_replace_class asserted that
House.room(wrong: :arg)raises fordef self.room(room_name), which real Ruby accepts, so it now asserts on a call Ruby also rejects.Picks up the failing test from #20 and makes it pass.