shard-spectator/src/spectator/dsl/structure_dsl.cr

198 lines
5.1 KiB
Crystal
Raw Normal View History

2018-09-15 17:21:23 +00:00
require "../example_group"
module Spectator::DSL
module StructureDSL
def initialize(sample_values : Internals::SampleValues)
end
macro describe(what, &block)
context({{what}}) {{block}}
end
2018-09-15 17:21:23 +00:00
macro context(what, &block)
module Group%group
include {{@type.id}}
2018-09-15 17:21:23 +00:00
{% if what.is_a?(Path) || what.is_a?(Generic) %}
_spectator_described_class {{what}}
{% end %}
2018-09-15 17:21:23 +00:00
::Spectator::DSL::Builder.start_group(
{{what.is_a?(StringLiteral) ? what : what.stringify}}
)
{{block.body}}
::Spectator::DSL::Builder.end_group
2018-09-15 17:21:23 +00:00
end
end
2018-09-15 17:21:23 +00:00
macro given(collection, &block)
module Group%group
include {{@type.id}}
def %collection
{{collection}}
end
@%wrapper : ::Spectator::Internals::ValueWrapper
def {{block.args.empty? ? "value".id : block.args.first}}
@%wrapper.as(::Spectator::Internals::TypedValueWrapper(typeof(%collection.first))).value
end
def initialize(sample_values : ::Spectator::Internals::SampleValues)
super
@%wrapper = sample_values.get_wrapper(:%group)
end
_spectator_given_collection Collection%collection, %to_a, %collection
%to_a = Collection%collection.new.%to_a
2018-09-15 17:21:23 +00:00
::Spectator::DSL::Builder.start_given_group(
{{collection.stringify}},
%to_a,
:%group
)
{{block.body}}
::Spectator::DSL::Builder.end_group
end
end
2018-09-15 17:21:23 +00:00
macro subject(&block)
let(:subject) {{block}}
end
2018-09-15 17:21:23 +00:00
macro let(name, &block)
let!(%value) {{block}}
2018-09-15 17:21:23 +00:00
@%wrapper : ::Spectator::Internals::ValueWrapper?
2018-09-15 17:21:23 +00:00
def {{name.id}}
if (wrapper = @%wrapper)
wrapper.unsafe_as(::Spectator::Internals::TypedValueWrapper(typeof(%value))).value
else
%value.tap do |value|
@%wrapper = ::Spectator::Internals::TypedValueWrapper(typeof(%value)).new(value)
2018-09-15 17:21:23 +00:00
end
end
end
end
2018-09-15 17:21:23 +00:00
macro let!(name, &block)
def {{name.id}}
{{block.body}}
2018-09-15 17:21:23 +00:00
end
end
2018-09-15 17:21:23 +00:00
macro before_all(&block)
::Spectator::DSL::Builder.add_before_all_hook {{block}}
end
2018-09-15 17:21:23 +00:00
macro before_each(&block)
::Spectator::DSL::Builder.add_before_each_hook {{block}}
end
2018-09-15 17:21:23 +00:00
macro after_all(&block)
::Spectator::DSL::Builder.add_after_all_hook {{block}}
end
2018-09-15 17:21:23 +00:00
macro after_each(&block)
::Spectator::DSL::Builder.add_after_each_hook {{block}}
end
2018-09-15 17:21:23 +00:00
macro around_each(&block)
::Spectator::DSL::Builder.add_around_each_hook {{block}}
end
2018-09-15 17:21:23 +00:00
def include_examples
raise NotImplementedError.new("Spectator::DSL#include_examples")
end
2018-09-15 17:21:23 +00:00
macro it(description, &block)
_spectator_example_wrapper(Wrapper%example, %run) {{block}}
_spectator_example(Example%example, Wrapper%example, ::Spectator::RunnableExample, {{description}}) do
protected def run_instance
@instance.%run
2018-09-15 17:21:23 +00:00
end
end
::Spectator::DSL::Builder.add_example(Example%example)
end
macro pending(description, &block)
_spectator_example_wrapper(Wrapper%example, %run) {{block}}
_spectator_example(Example%example, Wrapper%example, ::Spectator::PendingExample, {{description}})
2018-09-15 17:21:23 +00:00
::Spectator::DSL::Builder.add_example(Example%example)
end
2018-09-15 17:21:23 +00:00
macro it_behaves_like
{% raise NotImplementedError.new("it_behaves_like functionality is not implemented") %}
end
2018-09-15 17:21:23 +00:00
private macro _spectator_described_class(what)
def described_class
{{what}}.tap do |thing|
raise "#{thing} must be a type name to use #described_class or #subject,\
but it is a #{typeof(thing)}" unless thing.is_a?(Class)
end
2018-09-15 17:21:23 +00:00
end
_spectator_implicit_subject
end
private macro _spectator_implicit_subject
def subject
described_class.new
2018-09-15 17:21:23 +00:00
end
end
2018-09-23 22:07:03 +00:00
private macro _spectator_given_collection(class_name, to_a_method_name, collection_method_name)
class {{class_name.id}}
include {{@type.id}}
2018-09-23 22:07:03 +00:00
def {{to_a_method_name.id}}
{{collection_method_name.id}}.to_a
2018-09-23 22:07:03 +00:00
end
end
end
2018-09-23 22:07:03 +00:00
private macro _spectator_example_wrapper(class_name, run_method_name, &block)
class {{class_name.id}}
include ::Spectator::DSL::ExampleDSL
include {{@type.id}}
2018-09-23 22:07:03 +00:00
def initialize(sample_values : ::Spectator::Internals::SampleValues)
super
end
2018-09-23 22:07:03 +00:00
def {{run_method_name.id}}
{{block.body}}
2018-09-23 22:07:03 +00:00
end
end
end
2018-09-23 22:07:03 +00:00
private macro _spectator_example(example_class_name, wrapper_class_name, base_class, description, &block)
class {{example_class_name.id}} < {{base_class.id}}
def initialize(group : ::Spectator::ExampleGroup, sample_values : ::Spectator::Internals::SampleValues)
super
@instance = {{wrapper_class_name.id}}.new(sample_values)
end
2018-09-23 22:07:03 +00:00
{% if block.is_a?(Block) %}
{{block.body}}
{% end %}
2018-09-23 22:07:03 +00:00
def description
{{description.is_a?(StringLiteral) ? description : description.stringify}}
2018-09-23 22:07:03 +00:00
end
end
2018-09-15 17:21:23 +00:00
end
end
end