shard-spectator/src/spectator/example.cr

54 lines
1.5 KiB
Crystal
Raw Normal View History

2020-09-06 16:31:23 +00:00
require "./example_context_delegate"
require "./example_group"
require "./example_node"
require "./result"
require "./source"
2018-08-19 07:15:32 +00:00
module Spectator
# Standard example that runs a test case.
class Example < ExampleNode
# Indicates whether the example already ran.
getter? finished : Bool = false
# Retrieves the result of the last time the example ran.
2020-10-17 17:51:16 +00:00
# TODO: Make result not nil and default to pending.
getter! result : Result
# Creates the example.
# The *delegate* contains the test context and method that runs the test case.
# The *name* describes the purpose of the example.
# It can be a `Symbol` to describe a type.
# The *source* tracks where the example exists in source code.
# The example will be assigned to *group* if it is provided.
2020-09-06 16:31:23 +00:00
def initialize(@delegate : ExampleContextDelegate,
2020-09-27 00:14:59 +00:00
name : String? = nil, source : Source? = nil, group : ExampleGroup? = nil)
super(name, source, group)
end
# Executes the test case.
# Returns the result of the execution.
# The result will also be stored in `#result`.
def run : Result
2020-10-17 17:46:21 +00:00
Log.debug { "Running example #{self}" }
2020-09-27 15:10:27 +00:00
@delegate.call(self)
raise NotImplementedError.new("#run")
end
# Exposes information about the example useful for debugging.
def inspect(io)
2020-10-17 17:51:16 +00:00
# Full example name.
io << '"'
to_s(io)
io << '"'
# Add source if it's available.
if (s = source)
io << " @ "
io << s
end
# TODO: Add result.
end
2018-08-19 07:15:32 +00:00
end
end