From 74b78b7ca85af6e9953bbfc2a4dc1d3b460ccb4b Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 12 Feb 2021 22:46:22 -0700 Subject: [PATCH] Rename Source to Location --- .../command_line_arguments_config_source.cr | 8 ++++---- src/spectator/dsl/builder.cr | 20 +++++++++---------- src/spectator/dsl/examples.cr | 4 ++-- src/spectator/dsl/expectations.cr | 12 +++++------ src/spectator/dsl/groups.cr | 4 ++-- src/spectator/dsl/hooks.cr | 6 +++--- src/spectator/dsl/mocks.cr | 14 ++++++------- src/spectator/example.cr | 20 +++++++++---------- src/spectator/example_group_hook.cr | 18 ++++++++--------- src/spectator/example_hook.cr | 18 ++++++++--------- src/spectator/example_procsy_hook.cr | 18 ++++++++--------- src/spectator/expectation.cr | 20 +++++++++---------- src/spectator/formatting/failure_block.cr | 8 ++++---- src/spectator/formatting/failure_command.cr | 2 +- src/spectator/formatting/json_formatter.cr | 2 +- src/spectator/formatting/junit_formatter.cr | 2 +- src/spectator/formatting/junit_test_case.cr | 2 +- src/spectator/formatting/location_timing.cr | 16 +++++++++++++++ src/spectator/formatting/profile_block.cr | 2 +- src/spectator/formatting/source_timing.cr | 16 --------------- src/spectator/formatting/tap_formatter.cr | 2 +- src/spectator/includes.cr | 4 ++-- src/spectator/line_example_filter.cr | 2 +- src/spectator/{source.cr => location.cr} | 12 +++++------ ...e_filter.cr => location_example_filter.cr} | 8 ++++---- src/spectator/mocks/exception_method_stub.cr | 4 ++-- src/spectator/mocks/expect_any_instance.cr | 4 ++-- src/spectator/mocks/generic_method_stub.cr | 6 +++--- src/spectator/mocks/method_stub.cr | 6 +++--- .../mocks/multi_value_method_stub.cr | 4 ++-- src/spectator/mocks/nil_method_stub.cr | 16 +++++++-------- src/spectator/mocks/proc_method_stub.cr | 8 ++++---- src/spectator/mocks/reflection.cr | 4 ++-- src/spectator/mocks/type_registry.cr | 4 ++-- src/spectator/mocks/value_method_stub.cr | 4 ++-- src/spectator/result.cr | 2 +- src/spectator/spec/builder.cr | 16 +++++++-------- src/spectator/spec/node.cr | 8 ++++---- 38 files changed, 163 insertions(+), 163 deletions(-) create mode 100644 src/spectator/formatting/location_timing.cr delete mode 100644 src/spectator/formatting/source_timing.cr rename src/spectator/{source.cr => location.cr} (86%) rename src/spectator/{source_example_filter.cr => location_example_filter.cr} (52%) diff --git a/src/spectator/command_line_arguments_config_source.cr b/src/spectator/command_line_arguments_config_source.cr index 07ea144..1eeba61 100644 --- a/src/spectator/command_line_arguments_config_source.cr +++ b/src/spectator/command_line_arguments_config_source.cr @@ -2,9 +2,9 @@ require "option_parser" require "./config_source" require "./formatting" require "./line_example_filter" +require "./location" +require "./location_example_filter" require "./name_example_filter" -require "./source" -require "./source_example_filter" module Spectator # Generates configuration from the command-line arguments. @@ -130,8 +130,8 @@ module Spectator private def location_option(parser, builder) parser.on("--location FILE:LINE", "Run the example at line 'LINE' in the file 'FILE', multiple allowed") do |location| Log.debug { "Filtering for examples at #{location} (--location '#{location}')" } - source = Source.parse(location) - filter = SourceExampleFilter.new(source) + location = Location.parse(location) + filter = LocationExampleFilter.new(location) builder.add_example_filter(filter) end end diff --git a/src/spectator/dsl/builder.cr b/src/spectator/dsl/builder.cr index df24b2c..3d9ec47 100644 --- a/src/spectator/dsl/builder.cr +++ b/src/spectator/dsl/builder.cr @@ -38,32 +38,32 @@ module Spectator::DSL end # Defines a block of code to execute before any and all examples in the current group. - def before_all(source = nil, label = "before_all", &block) - hook = ExampleGroupHook.new(source: source, label: label, &block) + def before_all(location = nil, label = "before_all", &block) + hook = ExampleGroupHook.new(location: location, label: label, &block) @@builder.before_all(hook) end # Defines a block of code to execute before every example in the current group - def before_each(source = nil, label = "before_each", &block : Example -> _) - hook = ExampleHook.new(source: source, label: label, &block) + def before_each(location = nil, label = "before_each", &block : Example -> _) + hook = ExampleHook.new(location: location, label: label, &block) @@builder.before_each(hook) end # Defines a block of code to execute after any and all examples in the current group. - def after_all(source = nil, label = "after_all", &block) - hook = ExampleGroupHook.new(source: source, label: label, &block) + def after_all(location = nil, label = "after_all", &block) + hook = ExampleGroupHook.new(location: location, label: label, &block) @@builder.after_all(hook) end # Defines a block of code to execute after every example in the current group. - def after_each(source = nil, label = "after_each", &block : Example ->) - hook = ExampleHook.new(source: source, label: label, &block) + def after_each(location = nil, label = "after_each", &block : Example ->) + hook = ExampleHook.new(location: location, label: label, &block) @@builder.after_each(hook) end # Defines a block of code to execute around every example in the current group. - def around_each(source = nil, label = "around_each", &block : Example::Procsy ->) - hook = ExampleProcsyHook.new(source: source, label: label, &block) + def around_each(location = nil, label = "around_each", &block : Example::Procsy ->) + hook = ExampleProcsyHook.new(location: location, label: label, &block) @@builder.around_each(hook) end diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index af25638..3e05c11 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -1,5 +1,5 @@ require "../context" -require "../source" +require "../location" require "./builder" require "./tags" @@ -47,7 +47,7 @@ module Spectator::DSL ::Spectator::DSL::Builder.add_example( _spectator_example_name(\{{what}}), - ::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}), + ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}), new.as(::Spectator::Context), \%tags ) do |example| diff --git a/src/spectator/dsl/expectations.cr b/src/spectator/dsl/expectations.cr index 94a90b7..137b363 100644 --- a/src/spectator/dsl/expectations.cr +++ b/src/spectator/dsl/expectations.cr @@ -1,7 +1,7 @@ require "../block" require "../expectation" require "../expectation_failed" -require "../source" +require "../location" require "../value" module Spectator::DSL @@ -10,7 +10,7 @@ module Spectator::DSL # Immediately fail the current test. # A reason can be specified with *message*. def fail(message = "Example failed", *, _file = __FILE__, _line = __LINE__) - raise ExpectationFailed.new(Source.new(_file, _line), message) + raise ExpectationFailed.new(Location.new(_file, _line), message) end # Starts an expectation. @@ -30,8 +30,8 @@ module Spectator::DSL end %expression = ::Spectator::Value.new(%actual, {{actual.stringify}}) - %source = ::Spectator::Source.new({{actual.filename}}, {{actual.line_number}}) - ::Spectator::Expectation::Target.new(%expression, %source) + %location = ::Spectator::Location.new({{actual.filename}}, {{actual.line_number}}) + ::Spectator::Expectation::Target.new(%expression, %location) end # Starts an expectation. @@ -82,8 +82,8 @@ module Spectator::DSL {% raise "Unexpected block arguments in 'expect' call" %} {% end %} - %source = ::Spectator::Source.new({{block.filename}}, {{block.line_number}}) - ::Spectator::Expectation::Target.new(%block, %source) + %location = ::Spectator::Location.new({{block.filename}}, {{block.line_number}}) + ::Spectator::Expectation::Target.new(%block, %location) end # Short-hand for expecting something of the subject. diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index bf91b75..0fd2028 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -1,4 +1,4 @@ -require "../source" +require "../location" require "./builder" require "./tags" require "./memoize" @@ -41,7 +41,7 @@ module Spectator::DSL ::Spectator::DSL::Builder.start_group( _spectator_group_name(\{{what}}), - ::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}), + ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}), tags ) diff --git a/src/spectator/dsl/hooks.cr b/src/spectator/dsl/hooks.cr index 75dd06c..61fa15f 100644 --- a/src/spectator/dsl/hooks.cr +++ b/src/spectator/dsl/hooks.cr @@ -1,4 +1,4 @@ -require "../source" +require "../location" require "./builder" module Spectator::DSL @@ -16,7 +16,7 @@ module Spectator::DSL end ::Spectator::DSL::Builder.{{type.id}}( - ::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}) + ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}) ) { \%hook } end end @@ -34,7 +34,7 @@ module Spectator::DSL end ::Spectator::DSL::Builder.{{type.id}}( - ::Spectator::Source.new(\{{block.filename}}, \{{block.line_number}}) + ::Spectator::Location.new(\{{block.filename}}, \{{block.line_number}}) ) do |example| example.with_context(\{{@type.name}}) do \{% if block.args.empty? %} diff --git a/src/spectator/dsl/mocks.cr b/src/spectator/dsl/mocks.cr index 049a9f1..7ee1ffc 100644 --- a/src/spectator/dsl/mocks.cr +++ b/src/spectator/dsl/mocks.cr @@ -150,24 +150,24 @@ module Spectator::DSL end macro expect_any_instance_of(type, _source_file = __FILE__, _source_line = __LINE__) - %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) - ::Spectator::Mocks::ExpectAnyInstance({{type}}).new(%source) + %location = ::Spectator::Location.new({{_source_file}}, {{_source_line}}) + ::Spectator::Mocks::ExpectAnyInstance({{type}}).new(%location) end macro receive(method_name, _source_file = __FILE__, _source_line = __LINE__, &block) - %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + %location = ::Spectator::Location.new({{_source_file}}, {{_source_line}}) {% if block.is_a?(Nop) %} - ::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %source) + ::Spectator::Mocks::NilMethodStub.new({{method_name.id.symbolize}}, %location) {% else %} - ::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %source) { {{block.body}} } + ::Spectator::Mocks::ProcMethodStub.create({{method_name.id.symbolize}}, %location) { {{block.body}} } {% end %} end macro receive_messages(_source_file = __FILE__, _source_line = __LINE__, **stubs) - %source = ::Spectator::Source.new({{_source_file}}, {{_source_line}}) + %location = ::Spectator::Location.new({{_source_file}}, {{_source_line}}) %stubs = [] of ::Spectator::Mocks::MethodStub {% for name, value in stubs %} - %stubs << ::Spectator::Mocks::ValueMethodStub.new({{name.id.symbolize}}, %source, {{value}}) + %stubs << ::Spectator::Mocks::ValueMethodStub.new({{name.id.symbolize}}, %location, {{value}}) {% end %} %stubs end diff --git a/src/spectator/example.cr b/src/spectator/example.cr index 3fc713b..c210d2b 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -1,9 +1,9 @@ require "./example_context_delegate" require "./example_group" require "./harness" +require "./location" require "./pending_result" require "./result" -require "./source" require "./spec/node" require "./tags" @@ -27,14 +27,14 @@ module Spectator # The *entrypoint* defines the test code (typically inside *context*). # The *name* describes the purpose of the example. # It can be a `Symbol` to describe a type. - # The *source* tracks where the example exists in source code. + # The *location* tracks where the example exists in source code. # The example will be assigned to *group* if it is provided. # A set of *tags* can be used for filtering and modifying example behavior. # Note: The tags will not be merged with the parent tags. def initialize(@context : Context, @entrypoint : self ->, - name : String? = nil, source : Source? = nil, + name : String? = nil, location : Location? = nil, group : ExampleGroup? = nil, tags = Tags.new) - super(name, source, group, tags) + super(name, location, group, tags) end # Creates a dynamic example. @@ -42,13 +42,13 @@ module Spectator # The block will be given this example instance as an argument. # The *name* describes the purpose of the example. # It can be a `Symbol` to describe a type. - # The *source* tracks where the example exists in source code. + # The *location* tracks where the example exists in source code. # The example will be assigned to *group* if it is provided. # A set of *tags* can be used for filtering and modifying example behavior. # Note: The tags will not be merged with the parent tags. - def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil, + def initialize(name : String? = nil, location : Location? = nil, group : ExampleGroup? = nil, tags = Tags.new, &block : self ->) - super(name, source, group, tags) + super(name, location, group, tags) @context = NullContext.new @entrypoint = block end @@ -140,10 +140,10 @@ module Spectator to_s(io) io << '"' - # Add source if it's available. - if (source = self.source) + # Add location if it's available. + if (location = self.location) io << " @ " - io << source + io << location end io << result diff --git a/src/spectator/example_group_hook.cr b/src/spectator/example_group_hook.cr index 06b8c7d..85f6898 100644 --- a/src/spectator/example_group_hook.cr +++ b/src/spectator/example_group_hook.cr @@ -1,11 +1,11 @@ require "./label" -require "./source" +require "./location" module Spectator # Information about a hook tied to an example group and a proc to invoke it. class ExampleGroupHook # Location of the hook in source code. - getter! source : Source + getter! location : Location # User-defined description of the hook. getter! label : Label @@ -14,14 +14,14 @@ module Spectator # Creates the hook with a proc. # The *proc* will be called when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(@proc : (->), *, @source : Source? = nil, @label : Label = nil) + # A *location* and *label* can be provided for debugging. + def initialize(@proc : (->), *, @location : Location? = nil, @label : Label = nil) end # Creates the hook with a block. # The block will be executed when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(*, @source : Source? = nil, @label : Label = nil, &block : -> _) + # A *location* and *label* can be provided for debugging. + def initialize(*, @location : Location? = nil, @label : Label = nil, &block : -> _) @proc = block end @@ -31,7 +31,7 @@ module Spectator end # Produces the string representation of the hook. - # Includes the source and label if they're not nil. + # Includes the location and label if they're not nil. def to_s(io) io << "example group hook" @@ -40,9 +40,9 @@ module Spectator io << label end - if (source = @source) + if (location = @location) io << " @ " - io << source + io << location end end end diff --git a/src/spectator/example_hook.cr b/src/spectator/example_hook.cr index b61fa7d..1f02445 100644 --- a/src/spectator/example_hook.cr +++ b/src/spectator/example_hook.cr @@ -1,11 +1,11 @@ require "./label" -require "./source" +require "./location" module Spectator # Information about a hook tied to an example and a proc to invoke it. class ExampleHook # Location of the hook in source code. - getter! source : Source + getter! location : Location # User-defined description of the hook. getter! label : Label @@ -14,15 +14,15 @@ module Spectator # Creates the hook with a proc. # The *proc* will be called when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(@proc : (Example ->), *, @source : Source? = nil, @label : Label = nil) + # A *location* and *label* can be provided for debugging. + def initialize(@proc : (Example ->), *, @location : Location? = nil, @label : Label = nil) end # Creates the hook with a block. # The block must take a single argument - the current example. # The block will be executed when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(*, @source : Source? = nil, @label : Label = nil, &block : Example -> _) + # A *location* and *label* can be provided for debugging. + def initialize(*, @location : Location? = nil, @label : Label = nil, &block : Example -> _) @proc = block end @@ -33,7 +33,7 @@ module Spectator end # Produces the string representation of the hook. - # Includes the source and label if they're not nil. + # Includes the location and label if they're not nil. def to_s(io) io << "example hook" @@ -42,9 +42,9 @@ module Spectator io << label end - if (source = @source) + if (location = @location) io << " @ " - io << source + io << location end end end diff --git a/src/spectator/example_procsy_hook.cr b/src/spectator/example_procsy_hook.cr index 0b6ea63..27ccae6 100644 --- a/src/spectator/example_procsy_hook.cr +++ b/src/spectator/example_procsy_hook.cr @@ -1,11 +1,11 @@ require "./label" -require "./source" +require "./location" module Spectator # Information about a hook tied to an example and a proc to invoke it. class ExampleProcsyHook # Location of the hook in source code. - getter! source : Source + getter! location : Location # User-defined description of the hook. getter! label : Label @@ -14,15 +14,15 @@ module Spectator # Creates the hook with a proc. # The *proc* will be called when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(@proc : (Example::Procsy ->), *, @source : Source? = nil, @label : Label = nil) + # A *location* and *label* can be provided for debugging. + def initialize(@proc : (Example::Procsy ->), *, @location : Location? = nil, @label : Label = nil) end # Creates the hook with a block. # The block must take a single argument - the current example wrapped in a procsy. # The block will be executed when the hook is invoked. - # A *source* and *label* can be provided for debugging. - def initialize(*, @source : Source? = nil, @label : Label = nil, &block : Example::Procsy -> _) + # A *location* and *label* can be provided for debugging. + def initialize(*, @location : Location? = nil, @label : Label = nil, &block : Example::Procsy -> _) @proc = block end @@ -38,7 +38,7 @@ module Spectator end # Produces the string representation of the hook. - # Includes the source and label if they're not nil. + # Includes the location and label if they're not nil. def to_s(io) io << "example hook" @@ -47,9 +47,9 @@ module Spectator io << label end - if (source = @source) + if (location = @location) io << " @ " - io << source + io << location end end end diff --git a/src/spectator/expectation.cr b/src/spectator/expectation.cr index 6b38771..e7324dc 100644 --- a/src/spectator/expectation.cr +++ b/src/spectator/expectation.cr @@ -1,5 +1,5 @@ require "./expression" -require "./source" +require "./location" module Spectator # Result of evaluating a matcher on a target. @@ -7,9 +7,9 @@ module Spectator # such as whether it was successful and a description of the operation. struct Expectation # Location of the expectation in source code. - # This can be nil if the source isn't capturable, + # This can be nil if the location isn't capturable, # for instance using the *should* syntax or dynamically created expectations. - getter source : Source? + getter location : Location? # Indicates whether the expectation was met. def satisfied? @@ -48,14 +48,14 @@ module Spectator # Creates the expectation. # The *match_data* comes from the result of calling `Matcher#match`. - # The *source* is the location of the expectation in source code, if available. - def initialize(@match_data : Matchers::MatchData, @source : Source? = nil) + # The *location* is the location of the expectation in source code, if available. + def initialize(@match_data : Matchers::MatchData, @location : Location? = nil) end # Creates the JSON representation of the expectation. def to_json(json : ::JSON::Builder) json.object do - json.field("source") { @source.to_json(json) } + json.field("location") { @location.to_json(json) } json.field("satisfied", satisfied?) if (failed = @match_data.as?(Matchers::FailedMatchData)) failed_to_json(failed, json) @@ -76,15 +76,15 @@ module Spectator end # Stores part of an expectation. - # This covers the actual value (or block) being inspected and its source. + # This covers the actual value (or block) being inspected and its location. # This is the type returned by an `expect` block in the DSL. # It is not intended to be used directly, but instead by chaining methods. # Typically `#to` and `#not_to` are used. struct Target(T) # Creates the expectation target. # The *expression* is the actual value being tested and its label. - # The *source* is the location of where this expectation was defined. - def initialize(@expression : Expression(T), @source : Source) + # The *location* is the location of where this expectation was defined. + def initialize(@expression : Expression(T), @location : Location) end # Asserts that some criteria defined by the matcher is satisfied. @@ -163,7 +163,7 @@ module Spectator # Reports an expectation to the current harness. private def report(match_data : Matchers::MatchData) - expectation = Expectation.new(match_data, @source) + expectation = Expectation.new(match_data, @location) Harness.current.report(expectation) end end diff --git a/src/spectator/formatting/failure_block.cr b/src/spectator/formatting/failure_block.cr index ff24022..e1eb053 100644 --- a/src/spectator/formatting/failure_block.cr +++ b/src/spectator/formatting/failure_block.cr @@ -28,7 +28,7 @@ module Spectator::Formatting title(indent) indent.increase(inner_indent) do content(indent) - source(indent) + location(indent) end end end @@ -103,9 +103,9 @@ module Spectator::Formatting end end - # Produces the source line of the failure block. - private def source(indent) - indent.line(Comment.color(@result.example.source)) + # Produces the location line of the failure block. + private def location(indent) + indent.line(Comment.color(@result.example.location)) end # Gets the number of characters a positive integer spans in base 10. diff --git a/src/spectator/formatting/failure_command.cr b/src/spectator/formatting/failure_command.cr index 1d4247c..27c6896 100644 --- a/src/spectator/formatting/failure_command.cr +++ b/src/spectator/formatting/failure_command.cr @@ -8,7 +8,7 @@ module Spectator::Formatting # Appends the command to the output. def to_s(io) io << "crystal spec " - io << @example.source + io << @example.location end # Colorizes the command instance based on the result. diff --git a/src/spectator/formatting/json_formatter.cr b/src/spectator/formatting/json_formatter.cr index da09f18..369a896 100644 --- a/src/spectator/formatting/json_formatter.cr +++ b/src/spectator/formatting/json_formatter.cr @@ -88,7 +88,7 @@ module Spectator::Formatting @json.object do @json.field("example", result.example) @json.field("time", result.elapsed.total_seconds) - @json.field("source", result.example.source) + @json.field("location", result.example.location) end end end diff --git a/src/spectator/formatting/junit_formatter.cr b/src/spectator/formatting/junit_formatter.cr index 26a6a1a..f1dd60e 100644 --- a/src/spectator/formatting/junit_formatter.cr +++ b/src/spectator/formatting/junit_formatter.cr @@ -55,7 +55,7 @@ module Spectator::Formatting # Adds all of the individual test suite blocks. private def add_test_suites(report) - report.group_by(&.example.source.path).each do |path, results| + report.group_by(&.example.location.path).each do |path, results| JUnitTestSuite.new(path, results).to_xml(@xml) end end diff --git a/src/spectator/formatting/junit_test_case.cr b/src/spectator/formatting/junit_test_case.cr index c4b140e..30eacb3 100644 --- a/src/spectator/formatting/junit_test_case.cr +++ b/src/spectator/formatting/junit_test_case.cr @@ -31,7 +31,7 @@ module Spectator::Formatting # Java-ified class name created from the spec. private def classname - path = result.example.source.path + path = result.example.location.path file = File.basename(path) ext = File.extname(file) name = file[0...-(ext.size)] diff --git a/src/spectator/formatting/location_timing.cr b/src/spectator/formatting/location_timing.cr new file mode 100644 index 0000000..7e2c8a8 --- /dev/null +++ b/src/spectator/formatting/location_timing.cr @@ -0,0 +1,16 @@ +module Spectator::Formatting + # Produces the timing line in a profile block. + # This contains the length of time, and the example's location. + private struct LocationTiming + # Creates the location timing line. + def initialize(@span : Time::Span, @location : Location) + end + + # Appends the location timing information to the output. + def to_s(io) + io << HumanTime.new(@span).colorize.bold + io << ' ' + io << @location + end + end +end diff --git a/src/spectator/formatting/profile_block.cr b/src/spectator/formatting/profile_block.cr index d17cf86..ba7943f 100644 --- a/src/spectator/formatting/profile_block.cr +++ b/src/spectator/formatting/profile_block.cr @@ -21,7 +21,7 @@ module Spectator::Formatting private def entry(indent, result) indent.line(result.example) indent.increase do - indent.line(SourceTiming.new(result.elapsed, result.example.source)) + indent.line(LocationTiming.new(result.elapsed, result.example.location)) end end end diff --git a/src/spectator/formatting/source_timing.cr b/src/spectator/formatting/source_timing.cr deleted file mode 100644 index 527fa17..0000000 --- a/src/spectator/formatting/source_timing.cr +++ /dev/null @@ -1,16 +0,0 @@ -module Spectator::Formatting - # Produces the timing line in a profile block. - # This contains the length of time, and the example's source. - private struct SourceTiming - # Creates the source timing line. - def initialize(@span : Time::Span, @source : Source) - end - - # Appends the source timing information to the output. - def to_s(io) - io << HumanTime.new(@span).colorize.bold - io << ' ' - io << @source - end - end -end diff --git a/src/spectator/formatting/tap_formatter.cr b/src/spectator/formatting/tap_formatter.cr index 816da4f..018d879 100644 --- a/src/spectator/formatting/tap_formatter.cr +++ b/src/spectator/formatting/tap_formatter.cr @@ -51,7 +51,7 @@ module Spectator::Formatting indent.line(result.example) indent.increase do @io << "# " - indent.line(SourceTiming.new(result.elapsed, result.example.source)) + indent.line(LocationTiming.new(result.elapsed, result.example.location)) end end end diff --git a/src/spectator/includes.cr b/src/spectator/includes.cr index 9f73ade..e931000 100644 --- a/src/spectator/includes.cr +++ b/src/spectator/includes.cr @@ -37,6 +37,8 @@ require "./label" require "./lazy" require "./lazy_wrapper" require "./line_example_filter" +require "./location" +require "./location_example_filter" require "./matchers" require "./mocks" require "./name_example_filter" @@ -47,8 +49,6 @@ require "./pending_result" require "./profile" require "./report" require "./result" -require "./source" -require "./source_example_filter" require "./spec" require "./tags" require "./test_context" diff --git a/src/spectator/line_example_filter.cr b/src/spectator/line_example_filter.cr index 85cd739..dac4bcf 100644 --- a/src/spectator/line_example_filter.cr +++ b/src/spectator/line_example_filter.cr @@ -7,7 +7,7 @@ module Spectator # Checks whether the example satisfies the filter. def includes?(example) : Bool - @line == example.source.line + @line == example.location.line end end end diff --git a/src/spectator/source.cr b/src/spectator/location.cr similarity index 86% rename from src/spectator/source.cr rename to src/spectator/location.cr index b09d5df..2c6f444 100644 --- a/src/spectator/source.cr +++ b/src/spectator/location.cr @@ -1,19 +1,19 @@ require "json" module Spectator - # Define the file and line number something originated from. - struct Source + # Defines the file and line number a piece of code originated from. + struct Location # Absolute file path. getter file : String # Line number in the file. getter line : Int32 - # Creates the source. + # Creates the location. def initialize(@file, @line) end - # Parses a source from a string. + # Parses a location from a string. # The *string* should be in the form: # ```text # FILE:LINE @@ -50,7 +50,7 @@ module Spectator end end - # String representation of the source. + # String representation of the location. # This is formatted as: # ```text # FILE:LINE @@ -61,7 +61,7 @@ module Spectator io << line end - # Creates the JSON representation of the source. + # Creates the JSON representation of the location. def to_json(json : ::JSON::Builder) json.string(to_s) end diff --git a/src/spectator/source_example_filter.cr b/src/spectator/location_example_filter.cr similarity index 52% rename from src/spectator/source_example_filter.cr rename to src/spectator/location_example_filter.cr index d3ad4dc..116d1d9 100644 --- a/src/spectator/source_example_filter.cr +++ b/src/spectator/location_example_filter.cr @@ -1,14 +1,14 @@ module Spectator # Filter that matches examples in a given file and line. - class SourceExampleFilter < ExampleFilter + class LocationExampleFilter < ExampleFilter # Creates the filter. - # The *source* indicates which file and line the example must be on. - def initialize(@source : Source) + # The *location* indicates which file and line the example must be on. + def initialize(@location : Location) end # Checks whether the example satisfies the filter. def includes?(example) : Bool - @source === example.source + @location === example.location end end end diff --git a/src/spectator/mocks/exception_method_stub.cr b/src/spectator/mocks/exception_method_stub.cr index 96342bc..2f223fd 100644 --- a/src/spectator/mocks/exception_method_stub.cr +++ b/src/spectator/mocks/exception_method_stub.cr @@ -3,8 +3,8 @@ require "./generic_method_stub" module Spectator::Mocks class ExceptionMethodStub(ExceptionType) < GenericMethodStub(Nil) - def initialize(name, source, @exception : ExceptionType, args = nil) - super(name, source, args) + def initialize(name, location, @exception : ExceptionType, args = nil) + super(name, location, args) end def call(_args : GenericArguments(T, NT), &_original : -> RT) forall T, NT, RT diff --git a/src/spectator/mocks/expect_any_instance.cr b/src/spectator/mocks/expect_any_instance.cr index 0ccee05..607c425 100644 --- a/src/spectator/mocks/expect_any_instance.cr +++ b/src/spectator/mocks/expect_any_instance.cr @@ -2,7 +2,7 @@ require "./registry" module Spectator::Mocks struct ExpectAnyInstance(T) - def initialize(@source : Source) + def initialize(@location : Location) end def to(stub : MethodStub) : Nil @@ -10,7 +10,7 @@ module Spectator::Mocks Harness.current.mocks.expect(T, stub) value = Value.new(stub.name, stub.to_s) matcher = Matchers::ReceiveTypeMatcher.new(value, stub.arguments?) - partial = Expectations::ExpectationPartial.new(actual, @source) + partial = Expectations::ExpectationPartial.new(actual, @location) partial.to_eventually(matcher) end diff --git a/src/spectator/mocks/generic_method_stub.cr b/src/spectator/mocks/generic_method_stub.cr index dc8eba6..db4ff8f 100644 --- a/src/spectator/mocks/generic_method_stub.cr +++ b/src/spectator/mocks/generic_method_stub.cr @@ -7,8 +7,8 @@ module Spectator::Mocks abstract class GenericMethodStub(ReturnType) < MethodStub getter! arguments : Arguments - def initialize(name, source, @args : Arguments? = nil) - super(name, source) + def initialize(name, location, @args : Arguments? = nil) + super(name, location) end def callable?(call : MethodCall) : Bool @@ -25,7 +25,7 @@ module Spectator::Mocks io << " : " io << ReturnType io << " at " - io << @source + io << @location end end end diff --git a/src/spectator/mocks/method_stub.cr b/src/spectator/mocks/method_stub.cr index 492fc3f..8419501 100644 --- a/src/spectator/mocks/method_stub.cr +++ b/src/spectator/mocks/method_stub.cr @@ -1,13 +1,13 @@ -require "../source" +require "../location" require "./method_call" module Spectator::Mocks abstract class MethodStub getter name : Symbol - getter source : Source + getter location : Location - def initialize(@name, @source) + def initialize(@name, @location) end def callable?(call : MethodCall) : Bool diff --git a/src/spectator/mocks/multi_value_method_stub.cr b/src/spectator/mocks/multi_value_method_stub.cr index c623a4c..f025271 100644 --- a/src/spectator/mocks/multi_value_method_stub.cr +++ b/src/spectator/mocks/multi_value_method_stub.cr @@ -5,8 +5,8 @@ module Spectator::Mocks class MultiValueMethodStub(ReturnType) < GenericMethodStub(ReturnType) @index = 0 - def initialize(name, source, @values : ReturnType, args = nil) - super(name, source, args) + def initialize(name, location, @values : ReturnType, args = nil) + super(name, location, args) raise ArgumentError.new("Values must have at least one item") if @values.size < 1 end diff --git a/src/spectator/mocks/nil_method_stub.cr b/src/spectator/mocks/nil_method_stub.cr index 592d3b6..19b437a 100644 --- a/src/spectator/mocks/nil_method_stub.cr +++ b/src/spectator/mocks/nil_method_stub.cr @@ -13,36 +13,36 @@ module Spectator::Mocks end def and_return(value) - ValueMethodStub.new(@name, @source, value, @args) + ValueMethodStub.new(@name, @location, value, @args) end def and_return(*values) - MultiValueMethodStub.new(@name, @source, values.to_a, @args) + MultiValueMethodStub.new(@name, @location, values.to_a, @args) end def and_raise(exception_type : Exception.class) - ExceptionMethodStub.new(@name, @source, exception_type.new, @args) + ExceptionMethodStub.new(@name, @location, exception_type.new, @args) end def and_raise(exception : Exception) - ExceptionMethodStub.new(@name, @source, exception, @args) + ExceptionMethodStub.new(@name, @location, exception, @args) end def and_raise(message : String) - ExceptionMethodStub.new(@name, @source, Exception.new(message), @args) + ExceptionMethodStub.new(@name, @location, Exception.new(message), @args) end def and_raise(exception_type : Exception.class, *args) forall T - ExceptionMethodStub.new(@name, @source, exception_type.new(*args), @args) + ExceptionMethodStub.new(@name, @location, exception_type.new(*args), @args) end def with(*args : *T, **opts : **NT) forall T, NT args = GenericArguments.new(args, opts) - NilMethodStub.new(@name, @source, args) + NilMethodStub.new(@name, @location, args) end def and_call_original - OriginalMethodStub.new(@name, @source, @args) + OriginalMethodStub.new(@name, @location, @args) end end end diff --git a/src/spectator/mocks/proc_method_stub.cr b/src/spectator/mocks/proc_method_stub.cr index 1d55db3..de3a68d 100644 --- a/src/spectator/mocks/proc_method_stub.cr +++ b/src/spectator/mocks/proc_method_stub.cr @@ -3,12 +3,12 @@ require "./generic_method_stub" module Spectator::Mocks class ProcMethodStub(ReturnType) < GenericMethodStub(ReturnType) - def initialize(name, source, @proc : -> ReturnType, args = nil) - super(name, source, args) + def initialize(name, location, @proc : -> ReturnType, args = nil) + super(name, location, args) end - def self.create(name, source, args = nil, &block : -> T) forall T - ProcMethodStub.new(name, source, block, args) + def self.create(name, location, args = nil, &block : -> T) forall T + ProcMethodStub.new(name, location, block, args) end def call(_args : GenericArguments(T, NT), &_original : -> RT) forall T, NT, RT diff --git a/src/spectator/mocks/reflection.cr b/src/spectator/mocks/reflection.cr index cf014e1..9a03fd9 100644 --- a/src/spectator/mocks/reflection.cr +++ b/src/spectator/mocks/reflection.cr @@ -4,7 +4,7 @@ module Spectator::Mocks module Reflection private macro _spectator_reflect {% for meth in @type.methods %} - %source = ::Spectator::Source.new({{meth.filename}}, {{meth.line_number}}) + %location = ::Spectator::Location.new({{meth.filename}}, {{meth.line_number}}) %args = ::Spectator::Mocks::GenericArguments.create( {% for arg, i in meth.args %} {% matcher = if arg.restriction @@ -19,7 +19,7 @@ module Spectator::Mocks {{matcher}}{% if i < meth.args.size %},{% end %} {% end %} ) - ::Spectator::Mocks::TypeRegistry.add({{@type.id.stringify}}, {{meth.name.symbolize}}, %source, %args) + ::Spectator::Mocks::TypeRegistry.add({{@type.id.stringify}}, {{meth.name.symbolize}}, %location, %args) {% end %} end end diff --git a/src/spectator/mocks/type_registry.cr b/src/spectator/mocks/type_registry.cr index 4f960f3..631fec9 100644 --- a/src/spectator/mocks/type_registry.cr +++ b/src/spectator/mocks/type_registry.cr @@ -6,14 +6,14 @@ module Spectator::Mocks @@entries = {} of Key => Deque(MethodStub) - def add(type_name : String, method_name : Symbol, source : Source, args : Arguments) : Nil + def add(type_name : String, method_name : Symbol, location : Location, args : Arguments) : Nil key = {type_name, method_name} list = if @@entries.has_key?(key) @@entries[key] else @@entries[key] = Deque(MethodStub).new end - list << NilMethodStub.new(method_name, source, args) + list << NilMethodStub.new(method_name, location, args) end def exists?(type_name : String, call : MethodCall) : Bool diff --git a/src/spectator/mocks/value_method_stub.cr b/src/spectator/mocks/value_method_stub.cr index 43bfcaa..d3e7f5e 100644 --- a/src/spectator/mocks/value_method_stub.cr +++ b/src/spectator/mocks/value_method_stub.cr @@ -3,8 +3,8 @@ require "./generic_method_stub" module Spectator::Mocks class ValueMethodStub(ReturnType) < GenericMethodStub(ReturnType) - def initialize(name, source, @value : ReturnType, args = nil) - super(name, source, args) + def initialize(name, location, @value : ReturnType, args = nil) + super(name, location, args) end def call(_args : GenericArguments(T, NT), &_original : -> RT) forall T, NT, RT diff --git a/src/spectator/result.cr b/src/spectator/result.cr index d85bf39..2c775ef 100644 --- a/src/spectator/result.cr +++ b/src/spectator/result.cr @@ -31,7 +31,7 @@ module Spectator # Adds the common fields for a result to a JSON builder. private def add_json_fields(json : ::JSON::Builder) json.field("name", example) - json.field("location", example.source) + json.field("location", example.location) json.field("result", to_s) json.field("time", elapsed.total_seconds) json.field("expectations", expectations) diff --git a/src/spectator/spec/builder.cr b/src/spectator/spec/builder.cr index f9a4ced..3d340a7 100644 --- a/src/spectator/spec/builder.cr +++ b/src/spectator/spec/builder.cr @@ -52,16 +52,16 @@ module Spectator # This should be a symbol when describing a type - the type name is represented as a symbol. # Otherwise, a string should be used. # - # The *source* optionally defined where the group originates in source code. + # The *location* optionally defined where the group originates in source code. # # A set of *tags* can be used for filtering and modifying example behavior. # For instance, adding a "pending" tag will mark tests as pending and skip execution. # # 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, tags = Tags.new) : ExampleGroup - Log.trace { "Start group: #{name.inspect} @ #{source}; tags: #{tags}" } - ExampleGroup.new(name, source, current_group, tags).tap do |group| + def start_group(name, location = nil, tags = Tags.new) : ExampleGroup + Log.trace { "Start group: #{name.inspect} @ #{location}; tags: #{tags}" } + ExampleGroup.new(name, location, current_group, tags).tap do |group| @group_stack << group end end @@ -86,7 +86,7 @@ module Spectator # This should be a string or nil. # When nil, the example's name will be populated by the first expectation run inside of the test code. # - # The *source* optionally defined where the example originates in source code. + # The *location* optionally defined where the example originates in source code. # # The *context* is an instance of the context the test code should run in. # See `Context` for more information. @@ -100,9 +100,9 @@ module Spectator # It is expected that the test code runs when the block is called. # # The newly created example is returned. - def add_example(name, source, context, tags = Tags.new, &block : Example -> _) : Example - Log.trace { "Add example: #{name} @ #{source}; tags: #{tags}" } - Example.new(context, block, name, source, current_group, tags) + def add_example(name, location, context, tags = Tags.new, &block : Example -> _) : Example + Log.trace { "Add example: #{name} @ #{location}; tags: #{tags}" } + Example.new(context, block, name, location, current_group, tags) # The example is added to the current group by `Example` initializer. end diff --git a/src/spectator/spec/node.cr b/src/spectator/spec/node.cr index c499f6f..4bbde0c 100644 --- a/src/spectator/spec/node.cr +++ b/src/spectator/spec/node.cr @@ -1,5 +1,5 @@ require "../label" -require "../source" +require "../location" require "../tags" module Spectator @@ -9,7 +9,7 @@ module Spectator # but can be anything that should be iterated over when running the spec. abstract class Node # Location of the node in source code. - getter! source : Source + getter! location : Location # User-provided name or description of the node. # This does not include the group name or descriptions. @@ -41,10 +41,10 @@ module Spectator # Creates the node. # The *name* describes the purpose of the node. # It can be a `Symbol` to describe a type. - # The *source* tracks where the node exists in source code. + # The *location* tracks where the node exists in source code. # The node will be assigned to *group* if it is provided. # A set of *tags* can be used for filtering and modifying example behavior. - def initialize(@name : Label = nil, @source : Source? = nil, + def initialize(@name : Label = nil, @location : Location? = nil, group : ExampleGroup? = nil, @tags : Tags = Tags.new) # Ensure group is linked. group << self if group