diff --git a/src/spectator.cr b/src/spectator.cr index 81db191..0f23538 100644 --- a/src/spectator.cr +++ b/src/spectator.cr @@ -8,6 +8,14 @@ module Spectator # Current version of the Spectator library. VERSION = {{ `shards version #{__DIR__}`.stringify.chomp }} + # Inserts code to produce a debug message. + # The code will only be injected when `spectator_debug` is defined (`-Dspectator_debug`). + macro debug_out(message) + {% if flag?(:spectator_debug) %} + STDERR.puts {{message}} + {% end %} + end + # Top-level describe method. # All specs in a file must be wrapped in this call. # This takes an argument and a block. diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index 0ac3d93..9071b85 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -36,9 +36,7 @@ module Spectator # The newly created group is returned. # It shouldn't be used outside of this class until a matching `#end_group` is called. def start_group(name, source = nil) : ExampleGroup - {% if flag?(:spectator_debug) %} - puts "Start group: #{name.inspect} @ #{source}" - {% end %} + Spectator.debug_out("Start group: #{name.inspect} @ #{source}") ExampleGroup.new(name, source, current_group).tap do |group| @group_stack << group end @@ -51,9 +49,7 @@ module Spectator # At this point, it is safe to use the group. # All of its examples and sub-groups have been populated. def end_group : ExampleGroup - {% if flag?(:spectator_debug) %} - puts "End group: #{current_group}" - {% end %} + Spectator.debug_out("End group: #{current_group}") raise "Can't pop root group" if root? @group_stack.pop @@ -78,9 +74,7 @@ module Spectator # # The newly created example is returned. def add_example(name, source, context, &block : Example, Context ->) : Example - {% if flag?(:spectator_debug) %} - puts "Add example: #{name} @ #{source}" - {% end %} + Spectator.debug_out("Add example: #{name} @ #{source}") delegate = ExampleContextDelegate.new(context, block) Example.new(delegate, name, source, current_group) # The example is added to the current group by `Example` initializer.