forked from cucumber/cucumber-ruby
/
feature.rb
110 lines (93 loc) · 2.9 KB
/
feature.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require 'cucumber/ast/names'
module Cucumber
module Ast
# Represents the root node of a parsed feature.
class Feature #:nodoc:
include Names
attr_accessor :language
attr_writer :features, :background
attr_reader :file, :feature_elements
def initialize(background, comment, tags, keyword, title, description, feature_elements)
@background, @comment, @tags, @keyword, @title, @description, @feature_elements = background, comment, tags, keyword, title, description, feature_elements
end
attr_reader :gherkin_statement
def gherkin_statement(statement=nil)
@gherkin_statement ||= statement
end
def init
@background.feature = self if @background
@background.init if @background
@feature_elements.each do |feature_element|
feature_element.init
feature_element.feature = self
end
end
def add_feature_element(feature_element)
@feature_elements << feature_element
end
def accept(visitor)
return if Cucumber.wants_to_quit
init
visitor.visit_comment(@comment) unless @comment.empty?
visitor.visit_tags(@tags)
visitor.visit_feature_name(@keyword, indented_name)
visitor.visit_background(@background) if @background
@feature_elements.each do |feature_element|
visitor.visit_feature_element(feature_element)
end
end
def indented_name
indent = ""
name.split("\n").map do |l|
s = "#{indent}#{l}"
indent = " "
s
end.join("\n")
end
def source_tags
@tags.tags
end
def source_tag_names
source_tags.map { |tag| tag.name }
end
def accept_hook?(hook)
@tags.accept_hook?(hook)
end
def next_feature_element(feature_element, &proc)
init
index = @feature_elements.index(feature_element)
next_one = @feature_elements[index+1]
proc.call(next_one) if next_one
end
def backtrace_line(step_name, line)
"#{file_colon_line(line)}:in `#{step_name}'"
end
def file=(file)
file = file.gsub(/\//, '\\') if Cucumber::WINDOWS && file && !ENV['CUCUMBER_FORWARD_SLASH_PATHS']
@file = file
end
def file_colon_line(line)
"#{@file}:#{line}"
end
def short_name
first_line = name.split(/\n/)[0]
if first_line =~ /#{language.keywords('feature')}:(.*)/
$1.strip
else
first_line
end
end
def to_sexp
init
sexp = [:feature, @file, name]
comment = @comment.to_sexp
sexp += [comment] if comment
tags = @tags.to_sexp
sexp += tags if tags.any?
sexp += [@background.to_sexp] if @background
sexp += @feature_elements.map{|fe| fe.to_sexp}
sexp
end
end
end
end