From 4abf97139b007454532ed90297a896365c7d39c1 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 26 Aug 2019 21:06:06 -0600 Subject: [PATCH] Experimenting with auto-mocking methods --- src/spectator/dsl/mock_dsl.cr | 5 +++++ src/spectator/dsl/structure_dsl.cr | 18 ++++++++++++++++++ src/spectator/includes.cr | 2 ++ src/spectator/mock.cr | 30 ++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100644 src/spectator/dsl/mock_dsl.cr create mode 100644 src/spectator/mock.cr diff --git a/src/spectator/dsl/mock_dsl.cr b/src/spectator/dsl/mock_dsl.cr new file mode 100644 index 0000000..92b02da --- /dev/null +++ b/src/spectator/dsl/mock_dsl.cr @@ -0,0 +1,5 @@ +module Spectator::DSL + module MockDSL + + end +end diff --git a/src/spectator/dsl/structure_dsl.cr b/src/spectator/dsl/structure_dsl.cr index 1957d03..d88f4d6 100644 --- a/src/spectator/dsl/structure_dsl.cr +++ b/src/spectator/dsl/structure_dsl.cr @@ -1419,6 +1419,24 @@ module Spectator::DSL end end + macro mock(type) + {% real_type = type.resolve %} + {% if real_type < ::Reference %} + class ::{{real_type.name.id}} + include ::Spectator::Mock + end + {% elsif real_type < ::Value %} + struct ::{{real_type.name.id}} + include ::Spectator::Mock + end + {% else %} + module ::{{real_type.name.id}} + include ::Spectator::Mock + end + {% end %} + {% debug %} + end + # Creates an example, or a test case. # The *what* argument describes "what" is being tested or asserted. # The block contains the code to run the test. diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index 9861c89..ef5d06f 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -30,6 +30,8 @@ require "./example_group" require "./nested_example_group" require "./root_example_group" +require "./mock" + require "./config" require "./config_builder" require "./config_source" diff --git a/src/spectator/mock.cr b/src/spectator/mock.cr new file mode 100644 index 0000000..76df55b --- /dev/null +++ b/src/spectator/mock.cr @@ -0,0 +1,30 @@ +module Spectator + module Mock + macro included + {% for meth in @type.methods %} + {% if meth.visibility != :public %}{{meth.visibility.id}} {% end %}def {{meth.name.id}}( + {% for arg, i in meth.args %} + {% if meth.splat_index && i == meth.splat_index %} + *{{arg}}{% if i + (meth.accepts_block? ? 0 : 1) < meth.args.size %},{% end %} + {% else %} + {{arg}}{% if i + (meth.accepts_block? ? 0 : 1) < meth.args.size %},{% end %} + {% end %} + {% end %} + {% if meth.accepts_block? %}&{% if meth.block_arg %}{{meth.block_arg}}{% else %}__spec_block{% end %}{% end %} + ){% if meth.return_type %} : {{meth.return_type}}{% end %} + previous_def( + {% for arg, i in meth.args %} + {% if !meth.splat_index || i < meth.splat_index %} + {{arg.name.id}}{% if i + (meth.accepts_block? ? 0 : 1) < meth.args.size %},{% end %} + {% elsif meth.splat_index && i > meth.splat_index %} + {{arg.name.id}}: {{arg.name}}{% if i + (meth.accepts_block? ? 0 : 1) < meth.args.size %},{% end %} + {% end %} + {% end %} + {% if meth.accepts_block? %}&{% if meth.block_arg %}{{meth.block_arg}}{% else %}__spec_block{% end %}{% end %} + ) + end + {% end %} + {% debug %} + end + end +end