diff --git a/src/spectator.cr b/src/spectator.cr index 3f941c0..4c2f0fc 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -1,6 +1,26 @@ +require "./spectator/*" + +def expect(actual : T) forall T + pp actual +end + # TODO: Write documentation for `Spectator` module Spectator VERSION = "0.1.0" - # TODO: Put your code here + @@top_level_groups = [] of ExampleGroup + + def self.describe(type : T.class) : Nil forall T + group = DSL.new(type.to_s) + with group yield + @@top_level_groups << group._spec_build + end + + at_exit do + @@top_level_groups.each do |group| + group.examples.each do |example| + example.run + end + end + end end diff --git a/src/spectator/dsl.cr b/src/spectator/dsl.cr new file mode 100644 index 0000000..891bfe8 --- /dev/null +++ b/src/spectator/dsl.cr @@ -0,0 +1,76 @@ +require "./example_group" + +private macro _spec_add_example(example) + @examples << example +end + +module Spectator + class DSL + @examples = [] of Spectator::Example + + protected def initialize(@type : String) + end + + def describe + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#describe") + end + + def context + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#context") + end + + def it(description : String, &block) : Nil + example = Spectator::Example.new(description, block) + _spec_add_example(example) + end + + def it_behaves_like + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#it_behaves_like") + end + + def subject + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#subject") + end + + def subject! + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#subject!") + end + + def let + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#let") + end + + def let! + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#let!") + end + + def before_all + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#before_all") + end + + def before_each + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#before_each") + end + + def after_all + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#after_all") + end + + def after_each + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#after_each") + end + + def around_each + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#around_each") + end + + def include_examples + raise NotImplementedError.new("Spectator::DSL::ExampleGroupDSL#include_examples") + end + + # :nodoc: + protected def _spec_build : Spectator::ExampleGroup + Spectator::ExampleGroup.new(@examples) + end + end +end diff --git a/src/spectator/example.cr b/src/spectator/example.cr new file mode 100644 index 0000000..4c97fe9 --- /dev/null +++ b/src/spectator/example.cr @@ -0,0 +1,10 @@ +module Spectator + class Example + def initialize(@description : String, @block : ->) + end + + def run + @block.call + end + end +end diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr new file mode 100644 index 0000000..bbccd4b --- /dev/null +++ b/src/spectator/example_group.cr @@ -0,0 +1,10 @@ +require "./example" + +module Spectator + class ExampleGroup + protected getter examples : Array(Example) + + def initialize(@examples) + end + end +end