github.com/nycdavid/zeus@v0.0.0-20201208104106-9ba439429e03/rubygem/lib/zeus/m/test_collection.rb (about)

     1  require "forwardable"
     2  
     3  module Zeus
     4    module M
     5      ### Custom wrapper around an array of test methods
     6      # In charge of some smart querying, filtering, sorting, etc on the the
     7      # test methods
     8      class TestCollection
     9        include Enumerable
    10        extend Forwardable
    11        # This should act like an array, so forward some common methods over to the
    12        # internal collection
    13        def_delegators :@collection, :size, :<<, :each, :empty?
    14  
    15        def initialize(collection = nil)
    16          @collection = collection || []
    17        end
    18  
    19        # Slice out tests that may be within the given line.
    20        # Returns a new TestCollection with the results.
    21        def within(line)
    22          # Into a new collection, filter only the tests that...
    23          self.class.new(select do |test|
    24            # are within the given boundary for this method
    25            # or include everything if the line given is nil (no line)
    26            line.nil? || (test.start_line..test.end_line).include?(line)
    27          end)
    28        end
    29  
    30        # Used to line up method names in `#sprintf` when `m` aborts
    31        def column_size
    32          # Boil down the collection of test methods to the name of the method's
    33          # size, then find the largest one
    34          @column_size ||= map { |test| test.name.to_s.size }.max
    35        end
    36  
    37        # Be considerate when printing out tests and pre-sort them by line number
    38        def by_line_number(&block)
    39          # On each member of the collection, sort by line number and yield
    40          # the block into the sorted collection
    41          sort_by(&:start_line).each(&block)
    42        end
    43  
    44        def contains? test_name
    45          @collection.each do |test|
    46            return true if test_name.match(test.name)
    47          end
    48          false
    49        end
    50      end
    51    end
    52  end