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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
require 'cucumber/core/gherkin/writer/helpers'
require 'cucumber/core/gherkin/document'
module Cucumber
module Core
module Gherkin
module Writer
NEW_LINE = ''
def gherkin(uri = nil, &source)
uri ||= 'features/test.feature'
builder = Gherkin.new(uri, &source)
builder.build
end
class Gherkin
def initialize(uri, &source)
@uri, @source = uri, source
end
def comment(line)
comment_lines << "# #{line}"
end
def comment_lines
@comment_lines ||= []
end
def feature(*args, &source)
@feature = Feature.new(comment_lines, *args).tap do |builder|
builder.instance_exec(&source) if source
end
self
end
def build
instance_exec(&@source)
Document.new(@uri, @feature.build.join("\n"))
end
end
class Feature
include HasElements
include HasOptionsInitializer
include HasDescription
include Indentation.level(0)
default_keyword 'Feature'
elements :background, :scenario, :scenario_outline
def build(source = [])
elements.inject(source + statements) { |acc, el| el.build(acc) + [NEW_LINE] }
end
private
def language
options[:language]
end
def statements
prepare_statements language_statement,
comments_statement,
tag_statement,
name_statement,
description_statement,
NEW_LINE
end
def language_statement
"# language: #{language}" if language
end
end
class Background
include HasElements
include HasOptionsInitializer
include HasDescription
include Indentation.level 2
default_keyword 'Background'
elements :step
private
def statements
prepare_statements comments_statement, tag_statement, name_statement, description_statement
end
end
class Scenario
include HasElements
include HasOptionsInitializer
include HasDescription
include Indentation.level 2
default_keyword 'Scenario'
elements :step
private
def statements
prepare_statements comments_statement,
tag_statement,
name_statement,
description_statement
end
end
class ScenarioOutline
include HasElements
include HasOptionsInitializer
include HasDescription
include Indentation.level 2
default_keyword 'Scenario Outline'
elements :step, :examples
private
def statements
prepare_statements comments_statement, tag_statement, name_statement, description_statement
end
end
class Step
include HasElements
include HasOptionsInitializer
include Indentation.level 4
default_keyword 'Given'
elements :table
def doc_string(string, content_type='')
elements << DocString.new(string, content_type)
end
private
def statements
prepare_statements comments_statement, name_statement
end
def name_statement
"#{keyword} #{name}"
end
end
class Table
include Indentation.level(6)
include HasRows
def initialize(*)
end
def build(source)
source + statements
end
private
def statements
row_statements
end
end
class DocString
include Indentation.level(6)
attr_reader :strings, :content_type
private :strings, :content_type
def initialize(string, content_type)
@strings = string.split("\n").map(&:strip)
@content_type = content_type
end
def build(source)
source + statements
end
private
def statements
prepare_statements doc_string_statement
end
def doc_string_statement
[
%["""#{content_type}],
strings,
'"""'
]
end
end
class Examples
include HasOptionsInitializer
include HasRows
include HasDescription
include Indentation.level(4)
default_keyword 'Examples'
def build(source)
source + statements
end
private
def statements
prepare_statements NEW_LINE,
comments_statement,
tag_statement,
name_statement,
description_statement,
row_statements(2)
end
end
end
end
end
end
|