Add symbolic attribute to example components

This is used to determine whether a type or method is referenced.
This commit is contained in:
Michael Miller 2019-02-17 16:27:41 -07:00
parent 968775a5c3
commit 8339784492
6 changed files with 36 additions and 3 deletions

View File

@ -16,7 +16,8 @@ module Spectator::DSL
# ```
# The value would be "String" for the describe block
# and "with an empty string" for the context block.
def initialize(@what : String)
# Use a `Symbol` when referencing a type name.
def initialize(@what : Symbol | String)
end
# Builds the example group.

View File

@ -271,7 +271,15 @@ module Spectator::DSL
# Start a new group.
::Spectator::DSL::Builder.start_group(
{{what.is_a?(StringLiteral) ? what : what.stringify}}
{% if what.is_a?(StringLiteral) %}
{% if what.starts_with?("#") || what.starts_with?(".") %}
{{what.id.symbolize}}
{% else %}
{{what}}
{% end %}
{% else %}
{{what.symbolize}}
{% end %}
)
# Nest the block's content in the module.
@ -1487,6 +1495,11 @@ module Spectator::DSL
# Retrieves the underlying, wrapped test code.
getter instance
# Indicates whether the example references a method.
def symbolic?
{{what.starts_with?('#') ? true : false}}
end
# Add the block's content if one was provided.
{% if block.is_a?(Block) %}
{{block.body}}

View File

@ -14,6 +14,11 @@ module Spectator
"DUMMY"
end
# Dummy symbolic flag.
def symbolic?
false
end
# Dummy source.
def source
Source.new(__FILE__, __LINE__)

View File

@ -13,5 +13,8 @@ module Spectator
# Lookup the example with the specified index.
abstract def [](index : Int) : Example
# Indicates that the component references a type or method.
abstract def symbolic? : Bool
end
end

View File

@ -5,7 +5,8 @@ module Spectator
# This group can be nested under other groups.
class NestedExampleGroup < ExampleGroup
# Description from the user of the group's contents.
getter what : String
# This is a symbol when referencing a type.
getter what : Symbol | String
# Group that this is nested in.
getter parent : ExampleGroup
@ -21,6 +22,11 @@ module Spectator
super(hooks, conditions)
end
# Indicates wheter the group references a type.
def symbolic?
@what.is_a?(Symbol)
end
# Runs all of the `before_all` hooks.
# This should run prior to any examples in the group.
# The hooks will be run only once.

View File

@ -9,6 +9,11 @@ module Spectator
"ROOT"
end
# Indicates that the group is symbolic.
def symbolic?
true
end
# Does nothing.
# This prevents the root group
# from showing up in output.