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? %} \{% if block.args.empty? %}
\%test \%test
\{% else %} \{% else %}
\%test(example) \%test(example.unwrap_metadata(typeof(\{{@type.name}}.\%metadata)))
\{% end %} \{% end %}
end end
end end

View file

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