Move Node out of Spec namespace

This commit is contained in:
Michael Miller 2021-05-07 20:09:33 -06:00
parent f24d634ccb
commit 6bea36d8b6
No known key found for this signature in database
GPG key ID: F9A0C5C65B162436
7 changed files with 96 additions and 98 deletions

View file

@ -65,7 +65,7 @@ module Spectator::DSL
# Inserts the correct representation of a example's name. # Inserts the correct representation of a example's name.
# If *what* is a string, then it is dropped in as-is. # If *what* is a string, then it is dropped in as-is.
# For anything else, it is stringified. # For anything else, it is stringified.
# This is intended to be used to convert a description from the spec DSL to `Spec::Node#name`. # This is intended to be used to convert a description from the spec DSL to `Node#name`.
private macro _spectator_example_name(what) private macro _spectator_example_name(what)
{% if what.is_a?(StringLiteral) || {% if what.is_a?(StringLiteral) ||
what.is_a?(StringInterpolation) || what.is_a?(StringInterpolation) ||

View file

@ -56,7 +56,7 @@ module Spectator::DSL
# If *what* appears to be a type name, it will be symbolized. # If *what* appears to be a type name, it will be symbolized.
# If it's a string, then it is dropped in as-is. # If it's a string, then it is dropped in as-is.
# For anything else, it is stringified. # For anything else, it is stringified.
# This is intended to be used to convert a description from the spec DSL to `Spec::Node#name`. # This is intended to be used to convert a description from the spec DSL to `Node#name`.
private macro _spectator_group_name(what) private macro _spectator_group_name(what)
{% if (what.is_a?(Generic) || {% if (what.is_a?(Generic) ||
what.is_a?(Path) || what.is_a?(Path) ||

View file

@ -2,14 +2,14 @@ require "./example_context_delegate"
require "./example_group" require "./example_group"
require "./harness" require "./harness"
require "./location" require "./location"
require "./node"
require "./pending_result" require "./pending_result"
require "./result" require "./result"
require "./spec/node"
require "./tags" require "./tags"
module Spectator module Spectator
# Standard example that runs a test case. # Standard example that runs a test case.
class Example < Spec::Node class Example < Node
# Currently running example. # Currently running example.
class_getter! current : Example class_getter! current : Example

View file

@ -1,15 +1,15 @@
require "./events" require "./events"
require "./example_procsy_hook" require "./example_procsy_hook"
require "./spec/node" require "./node"
module Spectator module Spectator
# Collection of examples and sub-groups. # Collection of examples and sub-groups.
class ExampleGroup < Spec::Node class ExampleGroup < Node
include Enumerable(Spec::Node) include Enumerable(Node)
include Events include Events
include Iterable(Spec::Node) include Iterable(Node)
@nodes = [] of Spec::Node @nodes = [] of Node
group_event before_all do |hooks| group_event before_all do |hooks|
Log.trace { "Processing before_all hooks for #{self}" } Log.trace { "Processing before_all hooks for #{self}" }
@ -65,7 +65,7 @@ module Spectator
# Removes the specified *node* from the group. # Removes the specified *node* from the group.
# The node will be unassigned from this group. # The node will be unassigned from this group.
def delete(node : Spec::Node) def delete(node : Node)
# Only remove from the group if it is associated with this group. # Only remove from the group if it is associated with this group.
return unless node.group == self return unless node.group == self
@ -92,7 +92,7 @@ module Spectator
# Assigns the node to this group. # Assigns the node to this group.
# If the node already belongs to a group, # If the node already belongs to a group,
# it will be removed from the previous group before adding it to this group. # it will be removed from the previous group before adding it to this group.
def <<(node : Spec::Node) def <<(node : Node)
# Remove from existing group if the node is part of one. # Remove from existing group if the node is part of one.
if (previous = node.group?) if (previous = node.group?)
previous.delete(node) previous.delete(node)

View file

@ -1,6 +1,6 @@
require "./example" require "./example"
require "./example_group" require "./example_group"
require "./spec/node" require "./node"
module Spectator module Spectator
# Iterates through all examples in a group and its nested groups. # Iterates through all examples in a group and its nested groups.
@ -9,12 +9,12 @@ module Spectator
# Stack that contains the iterators for each group. # Stack that contains the iterators for each group.
# A stack is used to track where in the tree this iterator is. # A stack is used to track where in the tree this iterator is.
@stack : Array(Iterator(Spec::Node)) @stack : Array(Iterator(Node))
# Creates a new iterator. # Creates a new iterator.
# The *group* is the example group to iterate through. # The *group* is the example group to iterate through.
def initialize(@group : ExampleGroup) def initialize(@group : ExampleGroup)
iter = @group.each.as(Iterator(Spec::Node)) iter = @group.each.as(Iterator(Node))
@stack = [iter] @stack = [iter]
end end
@ -39,7 +39,7 @@ module Spectator
# Restart the iterator at the beginning. # Restart the iterator at the beginning.
def rewind def rewind
# Same code as `#initialize`, but return self. # Same code as `#initialize`, but return self.
iter = @group.each.as(Iterator(Spec::Node)) iter = @group.each.as(Iterator(Node))
@stack = [iter] @stack = [iter]
self self
end end

81
src/spectator/node.cr Normal file
View file

@ -0,0 +1,81 @@
require "./label"
require "./location"
require "./tags"
module Spectator
# A single item in a test spec.
# This is commonly an `Example` or `ExampleGroup`,
# but can be anything that should be iterated over when running the spec.
abstract class Node
# Location of the node in source code.
getter! location : Location
# User-provided name or description of the node.
# This does not include the group name or descriptions.
# Use `#to_s` to get the full name.
#
# This value will be nil if no name was provided.
# In this case, and the node is a runnable example,
# the name should be set to the description
# of the first matcher that runs in the test case.
#
# If this value is a `Symbol`, the user specified a type for the name.
getter! name : Label
# Updates the name of the node.
protected def name=(@name : String)
end
# Group the node belongs to.
getter! group : ExampleGroup
# User-defined keywords used for filtering and behavior modification.
getter tags : Tags
# Assigns the node to the specified *group*.
# This is an internal method and should only be called from `ExampleGroup`.
# `ExampleGroup` manages the association of nodes to groups.
protected setter group : ExampleGroup?
# Creates the node.
# The *name* describes the purpose of the node.
# It can be a `Symbol` to describe a type.
# The *location* tracks where the node exists in source code.
# The node will be assigned to *group* if it is provided.
# A set of *tags* can be used for filtering and modifying example behavior.
def initialize(@name : Label = nil, @location : Location? = nil,
group : ExampleGroup? = nil, @tags : Tags = Tags.new)
# Ensure group is linked.
group << self if group
end
# Indicates whether the node has completed.
abstract def finished? : Bool
# Checks if the node has been marked as pending.
# Pending items should be skipped during execution.
def pending?
tags.includes?(:pending)
end
# Constructs the full name or description of the node.
# This prepends names of groups this node is part of.
def to_s(io)
name = @name
# Prefix with group's full name if the node belongs to a group.
if (group = @group)
group.to_s(io)
# Add padding between the node names
# only if the names don't appear to be symbolic.
# Skip blank group names (like the root group).
io << ' ' unless !group.name? || # ameba:disable Style/NegatedConditionsInUnless
(group.name?.is_a?(Symbol) && name.is_a?(String) &&
(name.starts_with?('#') || name.starts_with?('.')))
end
name.to_s(io)
end
end
end

View file

@ -1,83 +0,0 @@
require "../label"
require "../location"
require "../tags"
module Spectator
class Spec
# A single item in a test spec.
# This is commonly an `Example` or `ExampleGroup`,
# but can be anything that should be iterated over when running the spec.
abstract class Node
# Location of the node in source code.
getter! location : Location
# User-provided name or description of the node.
# This does not include the group name or descriptions.
# Use `#to_s` to get the full name.
#
# This value will be nil if no name was provided.
# In this case, and the node is a runnable example,
# the name should be set to the description
# of the first matcher that runs in the test case.
#
# If this value is a `Symbol`, the user specified a type for the name.
getter! name : Label
# Updates the name of the node.
protected def name=(@name : String)
end
# Group the node belongs to.
getter! group : ExampleGroup
# User-defined keywords used for filtering and behavior modification.
getter tags : Tags
# Assigns the node to the specified *group*.
# This is an internal method and should only be called from `ExampleGroup`.
# `ExampleGroup` manages the association of nodes to groups.
protected setter group : ExampleGroup?
# Creates the node.
# The *name* describes the purpose of the node.
# It can be a `Symbol` to describe a type.
# The *location* tracks where the node exists in source code.
# The node will be assigned to *group* if it is provided.
# A set of *tags* can be used for filtering and modifying example behavior.
def initialize(@name : Label = nil, @location : Location? = nil,
group : ExampleGroup? = nil, @tags : Tags = Tags.new)
# Ensure group is linked.
group << self if group
end
# Indicates whether the node has completed.
abstract def finished? : Bool
# Checks if the node has been marked as pending.
# Pending items should be skipped during execution.
def pending?
tags.includes?(:pending)
end
# Constructs the full name or description of the node.
# This prepends names of groups this node is part of.
def to_s(io)
name = @name
# Prefix with group's full name if the node belongs to a group.
if (group = @group)
group.to_s(io)
# Add padding between the node names
# only if the names don't appear to be symbolic.
# Skip blank group names (like the root group).
io << ' ' unless !group.name? || # ameba:disable Style/NegatedConditionsInUnless
(group.name?.is_a?(Symbol) && name.is_a?(String) &&
(name.starts_with?('#') || name.starts_with?('.')))
end
name.to_s(io)
end
end
end
end