2018-10-14 23:10:12 +00:00
|
|
|
require "./example_component"
|
|
|
|
|
2018-08-19 07:15:32 +00:00
|
|
|
module Spectator
|
2018-10-10 19:05:17 +00:00
|
|
|
# Base class for all types of examples.
|
2019-02-18 06:01:43 +00:00
|
|
|
# Concrete types must implement the `#run_impl, `#what`, `#instance`, and `#source` methods.
|
2018-10-14 23:10:12 +00:00
|
|
|
abstract class Example < ExampleComponent
|
2018-10-10 19:05:17 +00:00
|
|
|
# Indicates whether the example has already been run.
|
2018-09-13 03:31:44 +00:00
|
|
|
getter? finished = false
|
2018-10-10 19:05:17 +00:00
|
|
|
|
|
|
|
# Group that the example belongs to.
|
2018-09-23 18:16:38 +00:00
|
|
|
getter group : ExampleGroup
|
2018-09-11 03:51:14 +00:00
|
|
|
|
2019-01-01 23:40:04 +00:00
|
|
|
# Retrieves the internal wrapped instance.
|
|
|
|
abstract def instance
|
|
|
|
|
2019-02-17 19:40:16 +00:00
|
|
|
# Source where the example originated from.
|
|
|
|
abstract def source : Source
|
2019-02-14 22:41:26 +00:00
|
|
|
|
2018-10-10 19:05:17 +00:00
|
|
|
# Runs the example code.
|
|
|
|
# A result is returned, which represents the outcome of the test.
|
|
|
|
# An example can be run only once.
|
|
|
|
# An exception is raised if an attempt is made to run it more than once.
|
|
|
|
def run : Result
|
|
|
|
raise "Attempted to run example more than once (#{self})" if finished?
|
|
|
|
@finished = true
|
2018-10-20 00:50:21 +00:00
|
|
|
run_impl
|
2018-10-10 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Implementation-specific for running the example code.
|
2018-10-20 00:50:21 +00:00
|
|
|
private abstract def run_impl : Result
|
2018-10-10 19:05:17 +00:00
|
|
|
|
|
|
|
# Creates the base of the example.
|
|
|
|
# The group should be the example group the example belongs to.
|
2019-02-18 06:01:43 +00:00
|
|
|
# The *sample_values* are passed to the example code.
|
2018-09-23 23:04:06 +00:00
|
|
|
def initialize(@group, sample_values : Internals::SampleValues)
|
2018-09-21 20:03:09 +00:00
|
|
|
end
|
|
|
|
|
2018-10-15 00:37:54 +00:00
|
|
|
# Indicates there is only one example to run.
|
|
|
|
def example_count
|
|
|
|
1
|
|
|
|
end
|
|
|
|
|
2018-10-15 01:06:02 +00:00
|
|
|
# Retrieve the current example.
|
|
|
|
def [](index : Int)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2018-10-10 19:05:17 +00:00
|
|
|
# String representation of the example.
|
|
|
|
# This consists of the groups the example is in and the description.
|
|
|
|
# The string can be given to end-users to identify the example.
|
2018-09-24 02:24:28 +00:00
|
|
|
def to_s(io)
|
|
|
|
@group.to_s(io)
|
2019-02-17 23:29:38 +00:00
|
|
|
io << ' ' unless symbolic? && @group.symbolic?
|
2018-10-14 17:50:08 +00:00
|
|
|
io << what
|
2018-09-24 02:24:28 +00:00
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|