Move mocks to their own module

This commit is contained in:
Michael Miller 2019-11-03 09:44:32 -07:00
parent 541dc661ca
commit c95e228bde
12 changed files with 56 additions and 52 deletions

View file

@ -703,33 +703,33 @@ module Spectator
# # Is equivalent to:
# expect("foobar".has_back_references?).to_not be_true
# ```
macro method_missing(call)
{% if call.name.starts_with?("be_") %}
# Remove `be_` prefix.
{% method_name = call.name[3..-1] %}
{% matcher = "PredicateMatcher" %}
{% elsif call.name.starts_with?("have_") %}
# Remove `have_` prefix.
{% method_name = call.name[5..-1] %}
{% matcher = "HavePredicateMatcher" %}
{% else %}
{% raise "Undefined local variable or method '#{call}'" %}
{% end %}
descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) }
label = String::Builder.new({{method_name.stringify}})
{% unless call.args.empty? %}
label << '('
{% for arg, index in call.args %}
label << {{arg}}
{% if index < call.args.size - 1 %}
label << ", "
{% end %}
{% end %}
label << ')'
{% end %}
test_value = ::Spectator::TestValue.new(descriptor, label.to_s)
::Spectator::Matchers::{{matcher.id}}.new(test_value)
end
# macro method_missing(call)
# {% if call.name.starts_with?("be_") %}
# # Remove `be_` prefix.
# {% method_name = call.name[3..-1] %}
# {% matcher = "PredicateMatcher" %}
# {% elsif call.name.starts_with?("have_") %}
# # Remove `have_` prefix.
# {% method_name = call.name[5..-1] %}
# {% matcher = "HavePredicateMatcher" %}
# {% else %}
# {% raise "Undefined local variable or method '#{call}'" %}
# {% end %}
#
# descriptor = { {{method_name}}: Tuple.new({{call.args.splat}}) }
# label = String::Builder.new({{method_name.stringify}})
# {% unless call.args.empty? %}
# label << '('
# {% for arg, index in call.args %}
# label << {{arg}}
# {% if index < call.args.size - 1 %}
# label << ", "
# {% end %}
# {% end %}
# label << ')'
# {% end %}
# test_value = ::Spectator::TestValue.new(descriptor, label.to_s)
# ::Spectator::Matchers::{{matcher.id}}.new(test_value)
# end
end
end

View file

@ -1,13 +1,11 @@
require "../double"
require "../generic_method_stub"
require "../open_mock"
require "../mocks"
module Spectator::DSL
macro double(name, &block)
{% if block.is_a?(Nop) %}
Double{{name.id}}.new(self)
{% else %}
class Double{{name.id}} < ::Spectator::Double
class Double{{name.id}} < ::Spectator::Mocks::Double
def initialize(@spectator_test : {{@type.id}})
super({{name.id.symbolize}})
end
@ -19,12 +17,12 @@ module Spectator::DSL
{% end %}
end
def allow(double : ::Spectator::Double)
OpenMock.new(double)
def allow(double : ::Spectator::Mocks::Double)
Mocks::OpenMock.new(double)
end
macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__)
%source = ::Spectator::Source.new({{_source_file}}, {{_source_line}})
::Spectator::GenericMethodStub(Nil).new({{method_name.symbolize}}, %source, ->{ nil })
::Spectator::Mocks::GenericMethodStub(Nil).new({{method_name.symbolize}}, %source, ->{ nil })
end
end

View file

@ -29,8 +29,7 @@ require "./example_group"
require "./nested_example_group"
require "./root_example_group"
require "./mock"
require "./double"
require "./mocks"
require "./config"
require "./config_builder"

View file

@ -1,4 +1,4 @@
require "../double"
require "../mocks/double"
require "./standard_matcher"
module Spectator::Matchers
@ -11,7 +11,7 @@ module Spectator::Matchers
end
def match?(actual : TestExpression(T)) : Bool forall T
double = actual.value.as(Double)
double = actual.value.as(Mocks::Double)
calls = double.spectator_stub_calls(@expected.value)
!calls.empty?
end

7
src/spectator/mocks.cr Normal file
View file

@ -0,0 +1,7 @@
require "./mocks/*"
module Spectator
# Functionality for mocking existing types.
module Mocks
end
end

View file

@ -1,7 +1,7 @@
require "./generic_method_call"
require "./generic_method_stub"
module Spectator
module Spectator::Mocks
abstract class Double
@spectator_stubs = Deque(MethodStub).new
@spectator_stub_calls = Deque(MethodCall).new
@ -37,20 +37,20 @@ module Spectator
%}
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%call = ::Spectator::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
%call = ::Spectator::Mocks::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
@spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%call)))
%stub.as(::Spectator::GenericMethodStub(typeof(%method({{args.splat}})))).call(%call)
%stub.as(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}})))).call(%call)
else
%method({{args.splat}})
end
end
def {{name}}({{params.splat}}){% if definition.is_a?(TypeDeclaration) %} : {{definition.type}}{% end %}
%call = ::Spectator::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
%call = ::Spectator::Mocks::GenericMethodCall.create({{name.symbolize}}{% unless args.empty? %}, {{args.splat}}{% end %})
@spectator_stub_calls << %call
if (%stub = @spectator_stubs.find(&.callable?(%call)))
%stub.as(::Spectator::GenericMethodStub(typeof(%method({{args.splat}}) { |*%yield_args| yield *%yield_args }))).call(%call)
%stub.as(::Spectator::Mocks::GenericMethodStub(typeof(%method({{args.splat}}) { |*%yield_args| yield *%yield_args }))).call(%call)
else
%method({{args.splat}}) do |*%yield_args|
yield *%yield_args

View file

@ -1,6 +1,6 @@
require "./method_call"
module Spectator
module Spectator::Mocks
class GenericMethodCall(T, NT) < MethodCall
getter args : T

View file

@ -1,8 +1,8 @@
require "../source"
require "./method_call"
require "./method_stub"
require "./source"
module Spectator
module Spectator::Mocks
class GenericMethodStub(ReturnType) < MethodStub
def initialize(name : Symbol, source : Source, @proc : -> ReturnType)
super(name, source)

View file

@ -1,4 +1,4 @@
module Spectator
module Spectator::Mocks
abstract class MethodCall
getter name : Symbol

View file

@ -1,6 +1,6 @@
require "./source"
require "../source"
module Spectator
module Spectator::Mocks
abstract class MethodStub
def initialize(@name : Symbol, @source : Source)
end

View file

@ -1,4 +1,4 @@
module Spectator
module Spectator::Mocks
module Mock
macro included
{% for meth in @type.methods %}

View file

@ -1,4 +1,4 @@
module Spectator
module Spectator::Mocks
struct OpenMock
def initialize(@mock : Double)
end