Implement example lookup by index

This will be used later for executing tests in random order.
This commit is contained in:
Michael Miller 2018-10-14 19:06:02 -06:00
parent eade6873e4
commit bcb68a5856
3 changed files with 27 additions and 0 deletions

View File

@ -34,6 +34,11 @@ module Spectator
1
end
# Retrieve the current example.
def [](index : Int)
self
end
# 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.

View File

@ -10,5 +10,8 @@ module Spectator
# The number of examples in this instance.
abstract def example_count : Int
# Lookup the example with the specified index.
abstract def [](index : Int) : Example
end
end

View File

@ -31,6 +31,25 @@ module Spectator
children.sum(&.example_count)
end
def [](index : Int) : Example
raise IndexError.new if index < 0
offset = index
found = children.find do |child|
count = child.example_count
if offset < count
true
else
offset -= count
false
end
end
if found
found[offset]
else
raise IndexError.new
end
end
# TODO: Remove this method.
def all_examples
Array(Example).new(example_count).tap do |array|