Experimenting with auto-mocking methods

This commit is contained in:
Michael Miller 2019-08-26 21:06:06 -06:00
parent a49f8eaa71
commit 4abf97139b
4 changed files with 55 additions and 0 deletions

View file

@ -0,0 +1,5 @@
module Spectator::DSL
module MockDSL
end
end

View file

@ -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.

View file

@ -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"

30
src/spectator/mock.cr Normal file
View file

@ -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