Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

* Fix arity validation staying disabled after a `stubs` or `verify` block with
`ignore_arity: true` raises
* Fix `ArgumentError: unknown keyword` when a replaced class's `new` or class
method takes an options hash and is called with brace-less keywords
[#20](https://github.com/testdouble/mocktail/pull/20)
* Replaced `new` and class methods with no keyword parameters now accept
keyword-style calls as a trailing hash, as real Ruby does, instead of raising
`ArgumentError`

# 3.0.0

Expand Down
5 changes: 5 additions & 0 deletions lib/mocktail/handles_dry_new_call.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative "share/folds_keywords_into_options_hash"

module Mocktail
class HandlesDryNewCall
extend T::Sig
Expand All @@ -7,9 +9,12 @@ def initialize
@logs_call = LogsCall.new
@fulfills_stubbing = FulfillsStubbing.new
@imitates_type = ImitatesType.new
@folds_keywords_into_options_hash = FoldsKeywordsIntoOptionsHash.new
end

def handle(type, args, kwargs, block)
args, kwargs = @folds_keywords_into_options_hash.fold(type.instance_method(:initialize), args, kwargs)

@validates_arguments.validate(Call.new(
original_method: type.instance_method(:initialize),
args: args,
Expand Down
5 changes: 5 additions & 0 deletions lib/mocktail/replaces_type/redefines_singleton_methods.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require_relative "../share/folds_keywords_into_options_hash"

module Mocktail
class RedefinesSingletonMethods
extend T::Sig

def initialize
@handles_dry_call = HandlesDryCall.new
@folds_keywords_into_options_hash = FoldsKeywordsIntoOptionsHash.new
end

def redefine(type)
Expand All @@ -16,10 +19,12 @@ def redefine(type)

declare_singleton_method_missing_errors!(type)
handles_dry_call = @handles_dry_call
folds_keywords_into_options_hash = @folds_keywords_into_options_hash
type_replacement.replacement_methods = type_replacement.original_methods&.map { |original_method|
type.singleton_class.send(:undef_method, original_method.name)
type.define_singleton_method original_method.name, ->(*args, **kwargs, &block) {
if TopShelf.instance.singleton_methods_replaced?(type)
args, kwargs = folds_keywords_into_options_hash.fold(original_method, args, kwargs)
handles_dry_call.handle(Call.new(
singleton: true,
double: type,
Expand Down
22 changes: 22 additions & 0 deletions lib/mocktail/share/folds_keywords_into_options_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Mocktail
class FoldsKeywordsIntoOptionsHash
extend T::Sig

KEYWORD_PARAM_TYPES = [:key, :keyreq, :keyrest, :nokey].freeze

def initialize
@grabs_original_method_parameters = GrabsOriginalMethodParameters.new
end

# Ruby 3 passes keywords to a method with no keyword params as a trailing
# positional hash, but our generic (*args, **kwargs) lambdas always split
# them out, so fold them back in to match what the real method would see

def fold(original_method, args, kwargs)
return [args, kwargs] if kwargs.empty? || original_method.nil?
return [args, kwargs] if @grabs_original_method_parameters.grab(original_method).any? { |type, _| KEYWORD_PARAM_TYPES.include?(type) }

[[*args, kwargs], {}]
end
end
end
5 changes: 5 additions & 0 deletions lib/mocktail/sorbet/mocktail/handles_dry_new_call.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# typed: strict

require_relative "share/folds_keywords_into_options_hash"

module Mocktail
class HandlesDryNewCall
extend T::Sig
Expand All @@ -10,10 +12,13 @@ def initialize
@logs_call = T.let(LogsCall.new, LogsCall)
@fulfills_stubbing = T.let(FulfillsStubbing.new, FulfillsStubbing)
@imitates_type = T.let(ImitatesType.new, ImitatesType)
@folds_keywords_into_options_hash = T.let(FoldsKeywordsIntoOptionsHash.new, FoldsKeywordsIntoOptionsHash)
end

sig { params(type: T::Class[T.all(T, Object)], args: T::Array[T.anything], kwargs: T::Hash[Symbol, T.anything], block: T.nilable(Proc)).returns(T.anything) }
def handle(type, args, kwargs, block)
args, kwargs = @folds_keywords_into_options_hash.fold(type.instance_method(:initialize), args, kwargs)

@validates_arguments.validate(Call.new(
original_method: type.instance_method(:initialize),
args: args,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# typed: strict

require_relative "../share/folds_keywords_into_options_hash"

module Mocktail
class RedefinesSingletonMethods
extend T::Sig

sig { void }
def initialize
@handles_dry_call = T.let(HandlesDryCall.new, HandlesDryCall)
@folds_keywords_into_options_hash = T.let(FoldsKeywordsIntoOptionsHash.new, FoldsKeywordsIntoOptionsHash)
end

sig { params(type: T.any(T::Class[T.anything], Module)).void }
Expand All @@ -20,10 +23,12 @@ def redefine(type)

declare_singleton_method_missing_errors!(type)
handles_dry_call = @handles_dry_call
folds_keywords_into_options_hash = @folds_keywords_into_options_hash
type_replacement.replacement_methods = type_replacement.original_methods&.map { |original_method|
type.singleton_class.send(:undef_method, original_method.name)
type.define_singleton_method original_method.name, ->(*args, **kwargs, &block) {
if TopShelf.instance.singleton_methods_replaced?(type)
args, kwargs = folds_keywords_into_options_hash.fold(original_method, args, kwargs)
handles_dry_call.handle(Call.new(
singleton: true,
double: type,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict

module Mocktail
class FoldsKeywordsIntoOptionsHash
extend T::Sig

KEYWORD_PARAM_TYPES = T.let([:key, :keyreq, :keyrest, :nokey].freeze, T::Array[Symbol])

sig { void }
def initialize
@grabs_original_method_parameters = T.let(GrabsOriginalMethodParameters.new, GrabsOriginalMethodParameters)
end

# Ruby 3 passes keywords to a method with no keyword params as a trailing
# positional hash, but our generic (*args, **kwargs) lambdas always split
# them out, so fold them back in to match what the real method would see
sig { params(original_method: T.nilable(T.any(UnboundMethod, Method)), args: T::Array[T.anything], kwargs: T::Hash[Symbol, T.anything]).returns([T::Array[T.anything], T::Hash[Symbol, T.anything]]) }
def fold(original_method, args, kwargs)
return [args, kwargs] if kwargs.empty? || original_method.nil?
return [args, kwargs] if @grabs_original_method_parameters.grab(original_method).any? { |type, _| KEYWORD_PARAM_TYPES.include?(type) }

[[*args, kwargs], {}]
end
end
end
5 changes: 5 additions & 0 deletions src/mocktail/handles_dry_new_call.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# typed: strict

require_relative "share/folds_keywords_into_options_hash"

module Mocktail
class HandlesDryNewCall
extend T::Sig
Expand All @@ -10,10 +12,13 @@ def initialize
@logs_call = T.let(LogsCall.new, LogsCall)
@fulfills_stubbing = T.let(FulfillsStubbing.new, FulfillsStubbing)
@imitates_type = T.let(ImitatesType.new, ImitatesType)
@folds_keywords_into_options_hash = T.let(FoldsKeywordsIntoOptionsHash.new, FoldsKeywordsIntoOptionsHash)
end

sig { params(type: T::Class[T.all(T, Object)], args: T::Array[T.anything], kwargs: T::Hash[Symbol, T.anything], block: T.nilable(Proc)).returns(T.anything) }
def handle(type, args, kwargs, block)
args, kwargs = @folds_keywords_into_options_hash.fold(type.instance_method(:initialize), args, kwargs)

@validates_arguments.validate(Call.new(
original_method: type.instance_method(:initialize),
args: args,
Expand Down
5 changes: 5 additions & 0 deletions src/mocktail/replaces_type/redefines_singleton_methods.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# typed: strict

require_relative "../share/folds_keywords_into_options_hash"

module Mocktail
class RedefinesSingletonMethods
extend T::Sig

sig { void }
def initialize
@handles_dry_call = T.let(HandlesDryCall.new, HandlesDryCall)
@folds_keywords_into_options_hash = T.let(FoldsKeywordsIntoOptionsHash.new, FoldsKeywordsIntoOptionsHash)
end

sig { params(type: T.any(T::Class[T.anything], Module)).void }
Expand All @@ -20,10 +23,12 @@ def redefine(type)

declare_singleton_method_missing_errors!(type)
handles_dry_call = @handles_dry_call
folds_keywords_into_options_hash = @folds_keywords_into_options_hash
type_replacement.replacement_methods = type_replacement.original_methods&.map { |original_method|
type.singleton_class.send(:undef_method, original_method.name)
type.define_singleton_method original_method.name, ->(*args, **kwargs, &block) {
if TopShelf.instance.singleton_methods_replaced?(type)
args, kwargs = folds_keywords_into_options_hash.fold(original_method, args, kwargs)
handles_dry_call.handle(Call.new(
singleton: true,
double: type,
Expand Down
25 changes: 25 additions & 0 deletions src/mocktail/share/folds_keywords_into_options_hash.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# typed: strict

module Mocktail
class FoldsKeywordsIntoOptionsHash
extend T::Sig

KEYWORD_PARAM_TYPES = T.let([:key, :keyreq, :keyrest, :nokey].freeze, T::Array[Symbol])

sig { void }
def initialize
@grabs_original_method_parameters = T.let(GrabsOriginalMethodParameters.new, GrabsOriginalMethodParameters)
end

# Ruby 3 passes keywords to a method with no keyword params as a trailing
# positional hash, but our generic (*args, **kwargs) lambdas always split
# them out, so fold them back in to match what the real method would see
sig { params(original_method: T.nilable(T.any(UnboundMethod, Method)), args: T::Array[T.anything], kwargs: T::Hash[Symbol, T.anything]).returns([T::Array[T.anything], T::Hash[Symbol, T.anything]]) }
def fold(original_method, args, kwargs)
return [args, kwargs] if kwargs.empty? || original_method.nil?
return [args, kwargs] if @grabs_original_method_parameters.grab(original_method).any? { |type, _| KEYWORD_PARAM_TYPES.include?(type) }

[[*args, kwargs], {}]
end
end
end
23 changes: 23 additions & 0 deletions test/safe/of_next_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ def initialize(a, b:)
end
end

class OptionsHash
extend T::Sig

sig { params(opts: T.untyped).void }
def initialize(opts = {})
raise "options hash required!"
end
end

sig { void }
def test_of_next_with_options_hash_initializer
options_hash = Mocktail.of_next(OptionsHash)

assert_same options_hash, OptionsHash.new(item: "hello")
end

sig { void }
def test_of_next_with_braced_options_hash_initializer
options_hash = Mocktail.of_next(OptionsHash)

assert_same options_hash, OptionsHash.new({item: "hello"})
end

sig { void }
def test_of_next
neato_mocktail = Mocktail.of_next(Neato)
Expand Down
53 changes: 52 additions & 1 deletion test/safe/replace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ def test_replace_class
House.summarize("Madiera", features: [:walls, :floor]) { "🆒" }

# Argument errors still propogate
assert_raises(ArgumentError) { House.room(wrong: :arg) }
assert_raises(ArgumentError) { T.unsafe(House).room }
assert_raises(ArgumentError) { T.unsafe(House).size }
assert_raises(ArgumentError) { T.unsafe(House).summarize("title", "wups") }

# Keywords passed to a method without keyword params become a positional hash, as in Ruby
House.room(wrong: :arg)

# verification works
verify { House.room("living") }
verify { |m| House.size(uom: m.is_a(String)) }
Expand Down Expand Up @@ -103,6 +106,54 @@ def test_replace_class
assert_match(/def self.color\n end/, error.message)
end

class Settings
extend T::Sig

sig { params(opts: T.untyped).void }
def initialize(opts = {})
raise "Unimplemented"
end

sig { params(opts: T.untyped).returns(T.untyped) }
def self.configure(opts = {})
raise "Unimplemented"
end
end

sig { void }
def test_replace_class_whose_new_takes_an_options_hash
Mocktail.replace(Settings)

stubs { T.unsafe(Settings).new(item: "hello") }.with { :stubbed }

assert_equal :stubbed, Settings.new(item: "hello")
assert_equal :stubbed, Settings.new({item: "hello"})
verify(times: 2) { Settings.new(item: "hello") }
end

sig { void }
def test_replace_class_whose_class_method_takes_an_options_hash
Mocktail.replace(Settings)

stubs { Settings.configure(item: "hello") }.with { :configured }

assert_equal :configured, Settings.configure(item: "hello")
assert_equal :configured, Settings.configure({item: "hello"})
verify(times: 2) { Settings.configure(item: "hello") }
end

class AcceptsNoKeywords
# sorbet-runtime can't build a sig for `**nil`, so skip the sig wrapper
define_method :initialize, ->(a, b = {}, **nil) { raise "Unimplemented" }
end

sig { void }
def test_replace_class_whose_new_accepts_no_keywords
Mocktail.replace(AcceptsNoKeywords)

assert_raises(ArgumentError) { T.unsafe(AcceptsNoKeywords).new(1, x: 1) }
end

module Home
extend T::Sig

Expand Down
Loading
Loading