Skip to content

Commit

Permalink
Formatting and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
icy-arctic-fox committed Jul 7, 2024
1 parent 8d0802f commit 0a9b70b
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 42 deletions.
22 changes: 11 additions & 11 deletions src/spectator/core/context.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
102 changes: 75 additions & 27 deletions src/spectator/core/hooks.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
20 changes: 16 additions & 4 deletions src/spectator/core/sandbox.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a9b70b

Please sign in to comment.