2018-10-14 23:10:12 +00:00
|
|
|
require "./example_component"
|
2019-09-12 04:21:06 +00:00
|
|
|
require "./test_wrapper"
|
2018-10-14 23:10:12 +00:00
|
|
|
|
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-09-08 16:38:26 +00:00
|
|
|
# Concrete types must implement the `#run_impl` method.
|
2018-10-14 23:10:12 +00:00
|
|
|
abstract class Example < ExampleComponent
|
2019-09-24 02:32:21 +00:00
|
|
|
@finished = false
|
|
|
|
|
2018-10-10 19:05:17 +00:00
|
|
|
# Indicates whether the example has already been run.
|
2019-09-24 02:32:21 +00:00
|
|
|
def finished? : Bool
|
|
|
|
@finished
|
|
|
|
end
|
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.
|
2019-09-22 17:27:18 +00:00
|
|
|
protected getter test_wrapper : TestWrapper
|
2019-01-01 23:40:04 +00:00
|
|
|
|
2019-02-17 19:40:16 +00:00
|
|
|
# Source where the example originated from.
|
2019-09-25 15:12:03 +00:00
|
|
|
def source : Source
|
2019-09-08 16:38:26 +00:00
|
|
|
@test_wrapper.source
|
|
|
|
end
|
|
|
|
|
2020-01-03 01:40:25 +00:00
|
|
|
def description : String | Symbol?
|
2019-09-08 16:38:26 +00:00
|
|
|
@test_wrapper.description
|
|
|
|
end
|
2019-02-14 22:41:26 +00:00
|
|
|
|
2019-09-25 15:12:03 +00:00
|
|
|
def symbolic? : Bool
|
2019-09-12 04:21:06 +00:00
|
|
|
description = @test_wrapper.description
|
2019-09-13 02:16:41 +00:00
|
|
|
description.starts_with?('#') || description.starts_with?('.')
|
2019-09-12 04:21:06 +00:00
|
|
|
end
|
|
|
|
|
2019-09-13 02:16:41 +00:00
|
|
|
abstract def run_impl
|
|
|
|
|
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?
|
2018-10-20 00:50:21 +00:00
|
|
|
run_impl
|
2019-09-06 19:08:41 +00:00
|
|
|
ensure
|
|
|
|
@finished = true
|
2018-10-10 19:05:17 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Creates the base of the example.
|
|
|
|
# The group should be the example group the example belongs to.
|
2019-09-08 16:38:26 +00:00
|
|
|
def initialize(@group, @test_wrapper)
|
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.
|
2019-09-24 02:32:21 +00:00
|
|
|
def example_count : Int
|
2018-10-15 00:37:54 +00:00
|
|
|
1
|
|
|
|
end
|
|
|
|
|
2018-10-15 01:06:02 +00:00
|
|
|
# Retrieve the current example.
|
2019-09-24 02:32:21 +00:00
|
|
|
def [](index : Int) : Example
|
2018-10-15 01:06:02 +00:00
|
|
|
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?
|
2019-09-26 22:49:44 +00:00
|
|
|
io << description
|
2018-09-24 02:24:28 +00:00
|
|
|
end
|
2019-03-23 03:29:20 +00:00
|
|
|
|
|
|
|
# Creates the JSON representation of the example,
|
|
|
|
# which is just its name.
|
|
|
|
def to_json(json : ::JSON::Builder)
|
|
|
|
json.string(to_s)
|
|
|
|
end
|
2018-08-19 07:15:32 +00:00
|
|
|
end
|
|
|
|
end
|