Use separate context for example name interpolation

This simplifies some code.
This commit is contained in:
Michael Miller 2022-12-20 20:43:47 -07:00
parent 8c3900adcb
commit d46698d81a
No known key found for this signature in database
GPG key ID: AC78B32D30CE34A2
4 changed files with 3 additions and 37 deletions

View file

@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Expectations using 'should' syntax report file and line where the 'should' keyword is instead of the test start.
- String interpolation for example names/labels uses a separate context than the one used by the test.
## [0.11.5] - 2022-12-18
### Added

View file

@ -125,9 +125,7 @@ module Spectator::DSL
{% if what.is_a?(StringLiteral) || what.is_a?(NilLiteral) %}
{{what}}
{% elsif what.is_a?(StringInterpolation) %}
->(example : ::Spectator::Example) do
example.with_context(\{{@type.name}}) { {{what}} }
end
{{@type.name}}.new.eval { {{what}} }
{% else %}
{{what.stringify}}
{% end %}

View file

@ -28,8 +28,6 @@ module Spectator
# Is pending if the example hasn't run.
getter result : Result = PendingResult.new("Example not run")
@name_proc : Proc(Example, String)?
# Creates the example.
# An instance to run the test code in is given by *context*.
# The *entrypoint* defines the test code (typically inside *context*).
@ -47,24 +45,6 @@ module Spectator
group << self if group
end
# Creates the example.
# An instance to run the test code in is given by *context*.
# The *entrypoint* defines the test code (typically inside *context*).
# The *name* describes the purpose of the example.
# It can be a proc to be evaluated in the context of the example.
# The *location* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
# A set of *metadata* can be used for filtering and modifying example behavior.
# Note: The metadata will not be merged with the parent metadata.
def initialize(@context : Context, @entrypoint : self ->,
@name_proc : Example -> String, location : Location? = nil,
@group : ExampleGroup? = nil, metadata = nil)
super(nil, location, metadata)
# Ensure group is linked.
group << self if group
end
# Creates a dynamic example.
# A block provided to this method will be called as-if it were the test code for the example.
# The block will be given this example instance as an argument.
@ -118,10 +98,6 @@ module Spectator
begin
@result = Harness.run do
if proc = @name_proc
self.name = proc.call(self)
end
@group.try(&.call_before_all)
if (parent = @group)
parent.call_around_each(procsy).call

View file

@ -8,7 +8,7 @@ module Spectator
# Constructs examples.
# Call `#build` to produce an `Example`.
class ExampleBuilder < NodeBuilder
@name : Proc(Example, String) | String?
@name : String?
# Creates the builder.
# A proc provided by *context_builder* is used to create a unique `Context` for each example produced by `#build`.
@ -18,15 +18,6 @@ module Spectator
@name : String? = nil, @location : Location? = nil, @metadata : Metadata? = nil)
end
# Creates the builder.
# A proc provided by *context_builder* is used to create a unique `Context` for each example produced by `#build`.
# The *entrypoint* indicates the proc used to invoke the test code in the example.
# The *name* is an interpolated string that runs in the context of the example.
# *location*, and *metadata* will be applied to the `Example` produced by `#build`.
def initialize(@context_builder : -> Context, @entrypoint : Example ->,
@name : Example -> String, @location : Location? = nil, @metadata : Metadata? = nil)
end
# Constructs an example with previously defined attributes and context.
# The *parent* is an already constructed example group to nest the new example under.
# It can be nil if the new example won't have a parent.