shard-spectator/src/spectator/nested_example_group.cr

55 lines
1.6 KiB
Crystal
Raw Normal View History

require "./example_group"
module Spectator
2018-11-20 21:04:14 +00:00
# A collection of examples and other example groups.
# This group can be nested under other groups.
class NestedExampleGroup < ExampleGroup
2018-11-20 21:04:14 +00:00
# Description from the user of the group's contents.
# This is a symbol when referencing a type.
2019-09-26 22:49:44 +00:00
getter description : Symbol | String
2019-09-26 22:23:13 +00:00
getter source : Source
2018-11-20 21:04:14 +00:00
# Group that this is nested in.
getter parent : ExampleGroup
2018-11-20 21:04:14 +00:00
# Creates a new example group.
2019-09-26 22:49:44 +00:00
# The *description* argument is a description from the user.
# The *parent* should contain this group.
2018-11-20 21:04:14 +00:00
# After creating this group, the parent's children should be updated.
# The parent's children must contain this group,
# otherwise there may be unexpected behavior.
# The *hooks* are stored to be triggered later.
2019-09-26 22:49:44 +00:00
def initialize(@description, @source, @parent, context)
2019-09-18 02:57:59 +00:00
super(context)
end
# Indicates wheter the group references a type.
def symbolic? : Bool
2019-09-26 22:49:44 +00:00
@description.is_a?(Symbol)
end
2018-11-20 21:04:14 +00:00
# Creates a string representation of the group.
2019-09-26 22:49:44 +00:00
# The string consists of `#description` appended to the parent.
2018-11-20 21:04:14 +00:00
# This results in a string like:
# ```text
# Foo#bar does something
# ```
2018-11-20 21:04:14 +00:00
# for the following structure:
# ```
# describe Foo do
# describe "#bar" do
# it "does something" do
# # ...
# end
# end
# end
# ```
def to_s(io)
parent.to_s(io)
io << ' ' unless (symbolic? || parent.is_a?(RootExampleGroup)) && parent.symbolic?
2019-09-26 22:49:44 +00:00
io << description
end
end
end