From c95e228bdef9eb4f511ddb15eb231d701995daca Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 3 Nov 2019 09:44:32 -0700 Subject: [PATCH] Move mocks to their own module --- src/spectator/dsl/matchers.cr | 56 +++++++++---------- src/spectator/dsl/mocks.cr | 12 ++-- src/spectator/includes.cr | 3 +- src/spectator/matchers/receive_matcher.cr | 4 +- src/spectator/mocks.cr | 7 +++ src/spectator/{ => mocks}/double.cr | 10 ++-- .../{ => mocks}/generic_method_call.cr | 2 +- .../{ => mocks}/generic_method_stub.cr | 4 +- src/spectator/{ => mocks}/method_call.cr | 2 +- src/spectator/{ => mocks}/method_stub.cr | 4 +- src/spectator/{ => mocks}/mock.cr | 2 +- src/spectator/{ => mocks}/open_mock.cr | 2 +- 12 files changed, 56 insertions(+), 52 deletions(-) create mode 100644 src/spectator/mocks.cr rename src/spectator/{ => mocks}/double.cr (83%) rename src/spectator/{ => mocks}/generic_method_call.cr (95%) rename src/spectator/{ => mocks}/generic_method_stub.cr (91%) rename src/spectator/{ => mocks}/method_call.cr (81%) rename src/spectator/{ => mocks}/method_stub.cr (80%) rename src/spectator/{ => mocks}/mock.cr (98%) rename src/spectator/{ => mocks}/open_mock.cr (86%) diff --git a/src/spectator/dsl/matchers.cr b/src/spectator/dsl/matchers.cr index 0e779c3..572aea6 100644 --- a/src/spectator/dsl/matchers.cr +++ b/src/spectator/dsl/matchers.cr @@ -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 diff --git a/src/spectator/dsl/mocks.cr b/src/spectator/dsl/mocks.cr index afcb681..be05616 100644 --- a/src/spectator/dsl/mocks.cr +++ b/src/spectator/dsl/mocks.cr @@ -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 diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index d103cfa..4425f99 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -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" diff --git a/src/spectator/matchers/receive_matcher.cr b/src/spectator/matchers/receive_matcher.cr index 89b627c..7bb8756 100644 --- a/src/spectator/matchers/receive_matcher.cr +++ b/src/spectator/matchers/receive_matcher.cr @@ -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 diff --git a/src/spectator/mocks.cr b/src/spectator/mocks.cr new file mode 100644 index 0000000..f50bf9e --- /dev/null +++ b/src/spectator/mocks.cr @@ -0,0 +1,7 @@ +require "./mocks/*" + +module Spectator + # Functionality for mocking existing types. + module Mocks + end +end diff --git a/src/spectator/double.cr b/src/spectator/mocks/double.cr similarity index 83% rename from src/spectator/double.cr rename to src/spectator/mocks/double.cr index 424f850..83c3679 100644 --- a/src/spectator/double.cr +++ b/src/spectator/mocks/double.cr @@ -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 diff --git a/src/spectator/generic_method_call.cr b/src/spectator/mocks/generic_method_call.cr similarity index 95% rename from src/spectator/generic_method_call.cr rename to src/spectator/mocks/generic_method_call.cr index 75050ce..f413537 100644 --- a/src/spectator/generic_method_call.cr +++ b/src/spectator/mocks/generic_method_call.cr @@ -1,6 +1,6 @@ require "./method_call" -module Spectator +module Spectator::Mocks class GenericMethodCall(T, NT) < MethodCall getter args : T diff --git a/src/spectator/generic_method_stub.cr b/src/spectator/mocks/generic_method_stub.cr similarity index 91% rename from src/spectator/generic_method_stub.cr rename to src/spectator/mocks/generic_method_stub.cr index 0c752ab..c3349ae 100644 --- a/src/spectator/generic_method_stub.cr +++ b/src/spectator/mocks/generic_method_stub.cr @@ -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) diff --git a/src/spectator/method_call.cr b/src/spectator/mocks/method_call.cr similarity index 81% rename from src/spectator/method_call.cr rename to src/spectator/mocks/method_call.cr index 719b608..7c85862 100644 --- a/src/spectator/method_call.cr +++ b/src/spectator/mocks/method_call.cr @@ -1,4 +1,4 @@ -module Spectator +module Spectator::Mocks abstract class MethodCall getter name : Symbol diff --git a/src/spectator/method_stub.cr b/src/spectator/mocks/method_stub.cr similarity index 80% rename from src/spectator/method_stub.cr rename to src/spectator/mocks/method_stub.cr index 856ed1e..3bc3790 100644 --- a/src/spectator/method_stub.cr +++ b/src/spectator/mocks/method_stub.cr @@ -1,6 +1,6 @@ -require "./source" +require "../source" -module Spectator +module Spectator::Mocks abstract class MethodStub def initialize(@name : Symbol, @source : Source) end diff --git a/src/spectator/mock.cr b/src/spectator/mocks/mock.cr similarity index 98% rename from src/spectator/mock.cr rename to src/spectator/mocks/mock.cr index 76df55b..e46b6a9 100644 --- a/src/spectator/mock.cr +++ b/src/spectator/mocks/mock.cr @@ -1,4 +1,4 @@ -module Spectator +module Spectator::Mocks module Mock macro included {% for meth in @type.methods %} diff --git a/src/spectator/open_mock.cr b/src/spectator/mocks/open_mock.cr similarity index 86% rename from src/spectator/open_mock.cr rename to src/spectator/mocks/open_mock.cr index 64068ce..b4549f9 100644 --- a/src/spectator/open_mock.cr +++ b/src/spectator/mocks/open_mock.cr @@ -1,4 +1,4 @@ -module Spectator +module Spectator::Mocks struct OpenMock def initialize(@mock : Double) end