Class: CucumberJunitToJson::Parsers::FeatureParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cucumber_junit_to_json/parsers/feature_parser.rb

Overview

Abstract representation of a cucumber feature file parser

Constant Summary collapse

Error =
Class.new(RuntimeError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path_to_features) ⇒ FeatureParser

Returns a new instance of FeatureParser.

Raises:



14
15
16
17
18
# File 'lib/cucumber_junit_to_json/parsers/feature_parser.rb', line 14

def initialize(path_to_features)
  STDERR.puts 'warning: no junit directory given' if path_to_features.empty?
  raise Error, "no such dir(s): #{path_to_features}" unless Dir.exist?(path_to_features)
  @path_to_features = path_to_features
end

Instance Attribute Details

#path_to_featuresObject

Returns the value of attribute path_to_features.



12
13
14
# File 'lib/cucumber_junit_to_json/parsers/feature_parser.rb', line 12

def path_to_features
  @path_to_features
end

Instance Method Details

#tags_and_line_number_matching(file, text) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/cucumber_junit_to_json/parsers/feature_parser.rb', line 20

def tags_and_line_number_matching(file, text)
  tags, line = text_and_line_number_before_match(file, text)
  tag_objects = []
  tags.split(' ').each do |tag|
    if tag.strip.start_with?('@')
      tag_objects.push(CucumberJunitToJson::Models::Tag.new(tag, line - 1))
    end
  end
  [tag_objects, line]
end

#text_and_line_number_before_match(file, text) ⇒ Object

Raises:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cucumber_junit_to_json/parsers/feature_parser.rb', line 31

def text_and_line_number_before_match(file, text)
  count = 0
  prev_line_text = ''
  File.open(file, 'r') do |f|
    f.each_line do |line|
      count += 1
      if line =~ /<\S+>/
        return prev_line_text, count if line.similar(text) >= 76
      elsif line =~ /#{text}/
        return prev_line_text, count
      end
      prev_line_text = line
    end
  end
  raise Error, "Could not find #{text} in #{file}"
end

#text_and_line_number_matching(file, text) ⇒ Object

Raises:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cucumber_junit_to_json/parsers/feature_parser.rb', line 48

def text_and_line_number_matching(file, text)
  count = 0
  File.open(file, 'r') do |f|
    f.each_line do |line|
      count += 1
      if line =~ /<\S+>/
        return line, count if line.similar(text) >= 76
      elsif line =~ /#{text}/
        return line, count
      end
    end
  end
  raise Error, "Could not find #{text} in #{file}"
end