Sha256: 350b50bf7b821b9bd44cef99034623ccad4d785091749c7551d4b863e6a43734

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# stolen from autotest-growl
# http://github.com/svoop/autotest-growl

class Autotest
  class ResultsParser

    ##
    # Analyze test result lines and return the numbers in a hash.
    def initialize(autotest)
      @numbers = {}
      lines = autotest.results.map {|s| s.gsub(/(\e.*?m|\n)/, '') }   # remove escape sequences
      lines.reject! {|line| !line.match(/\d+\s+(example|test|scenario|step)s?/) }   # isolate result numbers
      lines.each do |line|
        prefix = nil
        line.scan(/([1-9]\d*)\s(\w+)/) do |number, kind|
          kind.sub!(/s$/, '')   # singularize
          kind.sub!(/failure/, 'failed')   # homogenize
          if prefix
            @numbers["#{prefix}-#{kind}"] = number.to_i
          else
            @numbers[kind] = number.to_i
            prefix = kind
          end
        end
      end
    end

    ##
    # Determine the testing framework used.
    def framework
      case
        when @numbers['test'] then 'test-unit'
        when @numbers['example'] then 'rspec'
        when @numbers['scenario'] then 'cucumber'
      end
    end

    ##
    # Determine whether a result exists at all.
    def exists?
      !@numbers.empty?
    end

    ##
    # Check whether a specific result is present.
    def has?(kind)
      @numbers.has_key?(kind)
    end

    ##
    # Get a plain result number.
    def [](kind)
      @numbers[kind]
    end

    ##
    # Get a labelled result number. The prefix is removed and the label pluralized if necessary.
    def get(kind)
      "#{@numbers[kind]} #{kind.sub(/^.*-/, '')}#{'s' if @numbers[kind] != 1 && !kind.match(/(ed|ing)$/)}" if @numbers[kind]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
autotest-snarl-0.0.4 lib/autotest/results_parser.rb
autotest-snarl-0.0.3 lib/autotest/results_parser.rb
autotest-snarl-0.0.2 lib/autotest/results_parser.rb