From 6bea36d8b64fc743b6f3e989d988d06290b6be8e Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 7 May 2021 20:09:33 -0600 Subject: [PATCH] Move Node out of Spec namespace --- src/spectator/dsl/examples.cr | 2 +- src/spectator/dsl/groups.cr | 2 +- src/spectator/example.cr | 4 +- src/spectator/example_group.cr | 14 +++--- src/spectator/example_iterator.cr | 8 +-- src/spectator/node.cr | 81 ++++++++++++++++++++++++++++++ src/spectator/spec/node.cr | 83 ------------------------------- 7 files changed, 96 insertions(+), 98 deletions(-) create mode 100644 src/spectator/node.cr delete mode 100644 src/spectator/spec/node.cr diff --git a/src/spectator/dsl/examples.cr b/src/spectator/dsl/examples.cr index 5d8b88b..9cc9c85 100644 --- a/src/spectator/dsl/examples.cr +++ b/src/spectator/dsl/examples.cr @@ -65,7 +65,7 @@ module Spectator::DSL # Inserts the correct representation of a example's name. # If *what* is a string, then it is dropped in as-is. # 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) {% if what.is_a?(StringLiteral) || what.is_a?(StringInterpolation) || diff --git a/src/spectator/dsl/groups.cr b/src/spectator/dsl/groups.cr index 0fd2028..fa78ade 100644 --- a/src/spectator/dsl/groups.cr +++ b/src/spectator/dsl/groups.cr @@ -56,7 +56,7 @@ module Spectator::DSL # If *what* appears to be a type name, it will be symbolized. # If it's a string, then it is dropped in as-is. # 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) {% if (what.is_a?(Generic) || what.is_a?(Path) || diff --git a/src/spectator/example.cr b/src/spectator/example.cr index c210d2b..2ec9ea0 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -2,14 +2,14 @@ require "./example_context_delegate" require "./example_group" require "./harness" require "./location" +require "./node" require "./pending_result" require "./result" -require "./spec/node" require "./tags" module Spectator # Standard example that runs a test case. - class Example < Spec::Node + class Example < Node # Currently running example. class_getter! current : Example diff --git a/src/spectator/example_group.cr b/src/spectator/example_group.cr index c5f00c6..29e9b46 100644 --- a/src/spectator/example_group.cr +++ b/src/spectator/example_group.cr @@ -1,15 +1,15 @@ require "./events" require "./example_procsy_hook" -require "./spec/node" +require "./node" module Spectator # Collection of examples and sub-groups. - class ExampleGroup < Spec::Node - include Enumerable(Spec::Node) + class ExampleGroup < Node + include Enumerable(Node) include Events - include Iterable(Spec::Node) + include Iterable(Node) - @nodes = [] of Spec::Node + @nodes = [] of Node group_event before_all do |hooks| Log.trace { "Processing before_all hooks for #{self}" } @@ -65,7 +65,7 @@ module Spectator # Removes the specified *node* from the 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. return unless node.group == self @@ -92,7 +92,7 @@ module Spectator # Assigns the node to this group. # If the node already belongs to a 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. if (previous = node.group?) previous.delete(node) diff --git a/src/spectator/example_iterator.cr b/src/spectator/example_iterator.cr index abcfd33..f28a8bf 100644 --- a/src/spectator/example_iterator.cr +++ b/src/spectator/example_iterator.cr @@ -1,6 +1,6 @@ require "./example" require "./example_group" -require "./spec/node" +require "./node" module Spectator # 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. # 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. # The *group* is the example group to iterate through. def initialize(@group : ExampleGroup) - iter = @group.each.as(Iterator(Spec::Node)) + iter = @group.each.as(Iterator(Node)) @stack = [iter] end @@ -39,7 +39,7 @@ module Spectator # Restart the iterator at the beginning. def rewind # Same code as `#initialize`, but return self. - iter = @group.each.as(Iterator(Spec::Node)) + iter = @group.each.as(Iterator(Node)) @stack = [iter] self end diff --git a/src/spectator/node.cr b/src/spectator/node.cr new file mode 100644 index 0000000..587af76 --- /dev/null +++ b/src/spectator/node.cr @@ -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 diff --git a/src/spectator/spec/node.cr b/src/spectator/spec/node.cr deleted file mode 100644 index 4bbde0c..0000000 --- a/src/spectator/spec/node.cr +++ /dev/null @@ -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