From a56b1e0eb1f2cad4c073d29594ef528c14a7fa99 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sat, 30 Jan 2021 01:16:26 -0700 Subject: [PATCH] Somewhat functional metadata unwrap --- src/spectator/dsl/examples.cr | 2 +- src/spectator/example.cr | 13 +++++++++++-- src/spectator/metadata_example.cr | 12 ++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 src/spectator/metadata_example.cr diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index c8d3d59..7c68053 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -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 diff --git a/src/spectator/example.cr b/src/spectator/example.cr index f5601ae..7ada1e9 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -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. # diff --git a/src/spectator/metadata_example.cr b/src/spectator/metadata_example.cr new file mode 100644 index 0000000..6365d82 --- /dev/null +++ b/src/spectator/metadata_example.cr @@ -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