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 1
end end
# Retrieve the current example.
def [](index : Int)
self
end
# String representation of the example. # String representation of the example.
# This consists of the groups the example is in and the description. # This consists of the groups the example is in and the description.
# The string can be given to end-users to identify the example. # 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. # The number of examples in this instance.
abstract def example_count : Int abstract def example_count : Int
# Lookup the example with the specified index.
abstract def [](index : Int) : Example
end end
end end

View file

@ -31,6 +31,25 @@ module Spectator
children.sum(&.example_count) children.sum(&.example_count)
end 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. # TODO: Remove this method.
def all_examples def all_examples
Array(Example).new(example_count).tap do |array| Array(Example).new(example_count).tap do |array|