From 0a9b70b185622eeeed859bce7189c4571517b4a4 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 6 Jul 2024 22:13:13 -0600 Subject: [PATCH] Formatting and cleanup --- src/spectator/core/context.cr | 22 ++++---- src/spectator/core/hooks.cr | 102 +++++++++++++++++++++++++--------- src/spectator/core/sandbox.cr | 20 +++++-- 3 files changed, 102 insertions(+), 42 deletions(-) diff --git a/src/spectator/core/context.cr b/src/spectator/core/context.cr index d78d0d7e..9a5e0325 100644 --- a/src/spectator/core/context.cr +++ b/src/spectator/core/context.cr @@ -7,19 +7,15 @@ module Spectator include Hooks abstract def add_child(child : Item) - - def add_example(description = nil, *, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : Example - location = LocationRange.new(file, line, end_line) - example = Example.new(description.try &.to_s, location, &block) - add_child(example) - example - end end macro alias_example_group_to(name) module ::Spectator::Core::Context - def {{name.id}}(description = nil, *, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &) - location = LocationRange.new(file, line, end_line) + def {{name.id}}(description = nil, *, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, &) + location = LocationRange.new(source_file, source_line, source_end_line) group = ExampleGroup.new(description.try &.to_s, location) add_child(group) with group yield group @@ -33,8 +29,12 @@ module Spectator macro alias_example_to(name) module ::Spectator::Core::Context - def {{name.id}}(description = nil, *, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : Example - location = LocationRange.new(file, line, end_line) + def {{name.id}}(description = nil, *, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : Example + location = LocationRange.new(source_file, source_line, source_end_line) example = Example.new(description.try &.to_s, location, &block) add_child(example) example diff --git a/src/spectator/core/hooks.cr b/src/spectator/core/hooks.cr index 5af01160..fc7fcb3f 100644 --- a/src/spectator/core/hooks.cr +++ b/src/spectator/core/hooks.cr @@ -3,65 +3,113 @@ require "./example_hook" module Spectator::Core module Hooks - def before_each(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : ExampleHook(Example) - location = LocationRange.new(file, line, end_line) - hook = ExampleHook(Example).new(:before, location, &block) + private macro create_example_hook(position) + end + + private macro create_context_hook(position) + location = LocationRange.new(source_file, source_line, source_end_line) + ContextHook.new({{position}}, location, &block) + end + + def before_each(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) hooks = @before_each ||= [] of ExampleHook(Example) + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ExampleHook(Example).new(:before, location, &block) hooks << hook hook end - private getter critical_before_each_hooks = 0 + @before_each_priority_hooks = 0 - private def before_each!(&block : Example ->) : Nil - hook = ExampleHook(Example).new(:before, &block) + private def before_each!(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) hooks = @before_each ||= [] of ExampleHook(Example) - hooks.insert(@critical_before_each_hooks, hook) - @critical_before_each_hooks += 1 + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ExampleHook(Example).new(:before, location, &block) + hooks.insert(@before_each_priority_hooks, hook) + @before_each_priority_hooks += 1 + hook end - def before(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : ExampleHook(Example) - before_each(file: file, line: line, end_line: end_line, &block) + def before(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) + before_each(source_file: source_file, source_line: source_line, source_end_line: source_end_line, &block) end - def after_each(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : ExampleHook(Example) - location = LocationRange.new(file, line, end_line) - hook = ExampleHook(Example).new(:after, location, &block) + def after_each(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) hooks = @after_each ||= [] of ExampleHook(Example) + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ExampleHook(Example).new(:after, location, &block) hooks << hook hook end - def after(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : Example ->) : ExampleHook(Example) - after_each(file: file, line: line, end_line: end_line, &block) + def after(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) + after_each(source_file: source_file, source_line: source_line, source_end_line: source_end_line, &block) end - private getter critical_after_each_hooks = 0 + @after_each_priority_hooks = 0 - private def after_each!(&block : Example ->) : Nil - hook = ExampleHook(Example).new(:after, &block) + private def after_each!(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : Example ->) : ExampleHook(Example) hooks = @after_each ||= [] of ExampleHook(Example) - hooks.insert(@critical_after_each_hooks, hook) - @critical_after_each_hooks += 1 + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ExampleHook(Example).new(:after, location, &block) + hooks.insert(@after_each_priority_hooks, hook) + @after_each_priority_hooks += 1 + hook end - def before_all(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : ->) : ContextHook - location = LocationRange.new(file, line, end_line) - hook = ContextHook.new(:before, location, &block) + def before_all(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : ->) : ContextHook hooks = @before_all ||= [] of ContextHook + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ContextHook.new(:before, location, &block) hooks << hook hook end - def after_all(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &block : ->) : ContextHook - location = LocationRange.new(file, line, end_line) - hook = ContextHook.new(:after, location, &block) + def after_all(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + &block : ->) : ContextHook hooks = @after_all ||= [] of ContextHook + location = LocationRange.new(source_file, source_line, source_end_line) + hook = ContextHook.new(:after, location, &block) hooks << hook hook end - def around_each(*, file = __FILE__, line = __LINE__, end_line = __END_LINE__, & : Example::Procsy ->) : ExampleHook(Example::Procsy) + def around_each(*, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, + & : Example::Procsy ->) : ExampleHook(Example::Procsy) location = LocationRange.new(file, line, end_line) hook = ExampleHook(Example::Procsy).new(:around, location, &block) hooks = @around_each ||= [] of ExampleHook(Example::Procsy) diff --git a/src/spectator/core/sandbox.cr b/src/spectator/core/sandbox.cr index 65e7cbaf..f2b96652 100644 --- a/src/spectator/core/sandbox.cr +++ b/src/spectator/core/sandbox.cr @@ -31,14 +31,26 @@ module Spectator sandbox.current_example = example end - def self.context(description = nil, *, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &) - sandbox.root_example_group.context(description, file: file, line: line, end_line: end_line) do |group| + def self.context(description = nil, *, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, &) + sandbox.root_example_group.context(description, + source_file: source_file, + source_line: source_line, + source_end_line: source_end_line) do |group| with group yield group end end - def self.describe(description = nil, *, file = __FILE__, line = __LINE__, end_line = __END_LINE__, &) - sandbox.root_example_group.describe(description, file: file, line: line, end_line: end_line) do |group| + def self.describe(description = nil, *, + source_file = __FILE__, + source_line = __LINE__, + source_end_line = __END_LINE__, &) + sandbox.root_example_group.describe(description, + source_file: source_file, + source_line: source_line, + source_end_line: source_end_line) do |group| with group yield group end end