Somewhat functional metadata unwrap

This commit is contained in:
Michael Miller 2021-01-30 01:16:26 -07:00
parent 466a388558
commit a56b1e0eb1
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
3 changed files with 24 additions and 3 deletions

View file

@ -53,7 +53,7 @@ module Spectator::DSL
\{% if block.args.empty? %}
\%test
\{% else %}
\%test(example)
\%test(example.unwrap_metadata(typeof(\{{@type.name}}.\%metadata)))
\{% end %}
end
end

View file

@ -1,6 +1,7 @@
require "./example_context_delegate"
require "./example_group"
require "./harness"
require "./metadata_example"
require "./pending_result"
require "./result"
require "./source"
@ -26,8 +27,9 @@ module Spectator
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
def initialize(@context : Context, @entrypoint : self ->,
name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil)
name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil, metadata = NamedTuple.new)
super(name, source, group)
@metadata = Wrapper.new(metadata)
end
# Creates a dynamic example.
@ -37,9 +39,11 @@ module Spectator
# It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil, &block : self ->)
def initialize(name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil,
metadata = NamedTuple.new, &block : self ->)
@context = NullContext.new
@entrypoint = block
@metadata = Wrapper.new(metadata)
end
# Executes the test case.
@ -93,6 +97,11 @@ module Spectator
with context yield
end
protected def unwrap_metadata(klass)
metadata = @metadata.get(klass)
MetadataExample.new(self, metadata)
end
# Casts the example's test context to a specific type.
# This is an advanced method intended for internal usage only.
#

View file

@ -0,0 +1,12 @@
require "./example"
module Spectator
class MetadataExample(Metadata)
getter metadata : Metadata
def initialize(@example : Example, @metadata : Metadata)
end
forward_missing_to @example
end
end