class Mocks::LazyDouble(Methods)
- Mocks::LazyDouble(Methods)
- Reference
- Object
Overview
Double that can be quickly created and used within a test (function) without needing to define it in advance.
The Methods type argument must be a NamedTuple
.
The entries in Methods the method names and their return types.
That is, if Methods is {some_method: Int32}
,
then an instance of this double would return an Int32
from #some_method
.
This type of double will respond to all methods,
but only the methods with an entry in Methods can return a value.
Methods not defined will raise an UnexpectedMessage
error.
double = LazyDouble.new({some_method: 42, another_method: "example"})
double.some_method # => 42
double.another_method # => "example"
double.unknown_method # raises `UnexpectedMessage`
Included Modules
Extended Modules
Defined in:
mocks/lazy_double.crConstructors
-
.new(name, values : Methods, null_object : Bool = false)
Creates a new lazy double with initial stubs.
-
.new(name = nil, **values)
Creates a new lazy double with initial stubs.
- .unsafe_construct(address : Pointer, *args, **opts) : self
Macro Summary
Instance Method Summary
-
#!=(other)
Returns
true
if this object is not equal to other. -
#!~(other)
Shortcut to
!(self =~ other)
. -
#==(other : self)
Redefine virtually all methods to support stubs.
-
#==(other)
Returns
false
(other can only be aValue
here). -
#===(other)
Case equality.
-
#=~(other)
Pattern match.
-
#as_null_object
Creates a null object wrapper for the current double.
-
#dup
Returns a shallow copy of this object.
-
#hash(hasher)
See
Object#hash(hasher)
-
#hash
Generates an
UInt64
hash value for this object. -
#in?(collection : Object) : Bool
Returns
true
ifself
is included in the collection argument. -
#in?(*values : Object) : Bool
Returns
true
ifself
is included in the collection argument. -
#inspect(io : IO) : Nil
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
-
#inspect : String
Returns an unambiguous and information-rich string representation of this object, typically intended for developers.
-
#itself
Returns
self
. -
#not_nil!(message)
Returns
self
. -
#not_nil!
Returns
self
. -
#pretty_inspect(width = 79, newline = "\n", indent = 0) : String
Returns a pretty printed version of
self
. -
#pretty_print(pp : PrettyPrint) : Nil
Pretty prints
self
into the given printer. - #pretty_print(pp) : Nil
-
#same?(other : Reference) : Bool
Returns
true
if this reference is the same as other. -
#same?(other : Nil)
Returns
false
: a reference is nevernil
. -
#tap(&)
Yields
self
to the block, and then returnsself
. -
#to_s(io : IO) : Nil
Constructs a string representation of the double.
-
#to_s : String
Returns a nicely readable and concise string representation of this object, typically intended for users.
-
#try(&)
Yields
self
. -
#unsafe_as(type : T.class) forall T
Unsafely reinterprets the bytes of an object as being of another
type
.
Class methods inherited from module Mocks::Stubbable
unexpected_method_call(method_name : Symbol, abstract_call : Bool, type : NoReturn.class) : NoReturnunexpected_method_call(method_name : Symbol, abstract_call : Bool, type : T.class) : T forall T unexpected_method_call
Macros inherited from module Mocks::Stubbable
stub(method)
stub
Constructor Detail
Creates a new lazy double with initial stubs.
The name can be anything or nil for an anonymous double.
The values must be a NamedTuple
.
The null_object flag controls the behavior for when an undefined method is called.
True is to return self (null object behavior) and false is to raise an UnexpectedMessage
error.
Creates a new lazy double with initial stubs.
The name can be anything or nil for an anonymous double. Specify default stubs by passing keyword arguments. Each keyword is the name of a method to stub and its value is the value returned by that method.
LazyDouble.new(:fake, some_method: 42, another_method: "foo")
Macro Detail
Instance Method Detail
Returns true
if this object is not equal to other.
By default this method is implemented as !(self == other)
so there's no need to override this unless there's a more efficient
way to do it.
Redefine virtually all methods to support stubs.
FIXME Reuse method signature generation code.
Returns false
(other can only be a Value
here).
Case equality.
The #===
method is used in a case ... when ... end
expression.
For example, this code:
case value
when x
# something when x
when y
# something when y
end
Is equivalent to this code:
if x === value
# something when x
elsif y === value
# something when y
end
Object simply implements #===
by invoking #==
, but subclasses
(notably Regex
) can override it to provide meaningful case-equality semantics.
Pattern match.
Overridden by descendants (notably Regex
and String
) to provide meaningful
pattern-match semantics.
Returns a shallow copy of this object.
This allocates a new object and copies the contents of
self
into it.
Generates an UInt64
hash value for this object.
This method must have the property that a == b
implies a.hash == b.hash
.
The hash value is used along with #==
by the Hash
class to determine if two objects
reference the same hash key.
Subclasses must not override this method. Instead, they must define #hash(hasher)
,
though usually the macro def_hash
can be used to generate this method.
Returns true
if self
is included in the collection argument.
10.in?(0..100) # => true
10.in?({0, 1, 10}) # => true
10.in?(0, 1, 10) # => true
10.in?(:foo, :bar) # => false
Returns true
if self
is included in the collection argument.
10.in?(0..100) # => true
10.in?({0, 1, 10}) # => true
10.in?(0, 1, 10) # => true
10.in?(:foo, :bar) # => false
Appends a String representation of this object which includes its class name, its object address and the values of all instance variables.
class Person
def initialize(@name : String, @age : Int32)
end
end
Person.new("John", 32).inspect # => #<Person:0x10fd31f20 @name="John", @age=32>
Returns an unambiguous and information-rich string representation of this object, typically intended for developers.
This method should usually not be overridden. It delegates to
#inspect(IO)
which can be overridden for custom implementations.
Also see #to_s
.
Returns self
.
str = "hello"
str.itself.object_id == str.object_id # => true
Returns self
.
Nil
overrides this method and raises NilAssertionError
, see Nil#not_nil!
.
This method can be used to remove Nil
from a union type.
However, it should be avoided if possible and is often considered a code smell.
Usually, you can write code in a way that the compiler can safely exclude Nil
types,
for example using if var
.
#not_nil!
is only meant as a last resort when there's no other way to explain this to the compiler.
Either way, consider instead raising a concrete exception with a descriptive message.
message has no effect. It is only used by Nil#not_nil!(message = nil)
.
Returns self
.
Nil
overrides this method and raises NilAssertionError
, see Nil#not_nil!
.
This method can be used to remove Nil
from a union type.
However, it should be avoided if possible and is often considered a code smell.
Usually, you can write code in a way that the compiler can safely exclude Nil
types,
for example using if var
.
#not_nil!
is only meant as a last resort when there's no other way to explain this to the compiler.
Either way, consider instead raising a concrete exception with a descriptive message.
Returns a pretty printed version of self
.
Pretty prints self
into the given printer.
By default appends a text that is the result of invoking
#inspect
on self
. Subclasses should override
for custom pretty printing.
Returns true
if this reference is the same as other. This is only
true
if this reference's object_id
is the same as other's.
Returns false
: a reference is never nil
.
Yields self
to the block, and then returns self
.
The primary purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain.
(1..10).tap { |x| puts "original: #{x.inspect}" }
.to_a.tap { |x| puts "array: #{x.inspect}" }
.select { |x| x % 2 == 0 }.tap { |x| puts "evens: #{x.inspect}" }
.map { |x| x*x }.tap { |x| puts "squares: #{x.inspect}" }
Returns a nicely readable and concise string representation of this object, typically intended for users.
This method should usually not be overridden. It delegates to
#to_s(IO)
which can be overridden for custom implementations.
Also see #inspect
.
Yields self
. Nil
overrides this method and doesn't yield.
This method is useful for dealing with nilable types, to safely
perform operations only when the value is not nil
.
# First program argument in downcase, or nil
ARGV[0]?.try &.downcase
Unsafely reinterprets the bytes of an object as being of another type
.
This method is useful to treat a type that is represented as a chunk of
bytes as another type where those bytes convey useful information. As an
example, you can check the individual bytes of an Int32
:
0x01020304.unsafe_as(StaticArray(UInt8, 4)) # => StaticArray[4, 3, 2, 1]
Or treat the bytes of a Float64
as an Int64
:
1.234_f64.unsafe_as(Int64) # => 4608236261112822104
This method is unsafe because it behaves unpredictably when the given
type
doesn't have the same bytesize as the receiver, or when the given
type
representation doesn't semantically match the underlying bytes.
Also note that because #unsafe_as
is a regular method, unlike the pseudo-method
as
, you can't specify some types in the type grammar using a short notation, so
specifying a static array must always be done as StaticArray(T, N)
, a tuple
as Tuple(...)
and so on, never as UInt8[4]
or {Int32, Int32}
.