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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
require 'gherkin/parser'
require 'gherkin/token_scanner'
require 'gherkin/token_matcher'
require 'gherkin/ast_builder'
require 'gherkin/errors'
require 'rspec'
module Gherkin
describe Parser do
it "parses a simple feature" do
parser = Parser.new
scanner = TokenScanner.new("Feature: test")
ast = parser.parse(scanner)
expect(ast).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "test",
children: []
},
comments: [],
type: :GherkinDocument
})
end
it "parses a complex feature" do
# This should include every significant type of thing within the language
feature_text = "@feature_tag\n" +
"Feature: feature name\n" +
" feature description\n" +
"\n" +
" Background: background name\n" +
" background description\n" +
" * a step\n" +
"\n" +
" @scenario_tag\n" +
" Scenario: scenario name\n" +
" scenario description\n" +
" * a step with a table\n" +
" | a table |\n" +
"\n" +
" @outline_tag\n" +
" Scenario Outline: outline name\n" +
" outline description\n" +
" * a step with a doc string\n" +
" \"\"\" content_type\n" +
" lots of text\n" +
" \"\"\"\n" +
" # Random file comment\n" +
" @example_tag\n" +
" Examples: examples name\n" +
" examples description\n" +
" | param |\n" +
" | value |\n"
parser = Parser.new
scanner = TokenScanner.new(feature_text)
ast = parser.parse(scanner)
expect(ast).to eq({
feature: {type: :Feature,
tags: [{type: :Tag,
location: {line: 1, column: 1},
name: "@feature_tag"}],
location: {line: 2, column: 1},
language: "en",
keyword: "Feature",
name: "feature name",
description: " feature description",
children: [{type: :Background,
location: {line: 5, column: 4},
keyword: "Background",
name: "background name",
description: " background description",
steps: [{type: :Step,
location: {line: 7, column: 5},
keyword: "* ",
text: "a step"}]},
{type: :Scenario,
tags: [{type: :Tag,
location: {line: 9, column: 3},
name: "@scenario_tag"}],
location: {line: 10, column: 3},
keyword: "Scenario",
name: "scenario name",
description: " scenario description",
steps: [{type: :Step,
location: {line: 12, column: 5},
keyword: "* ",
text: "a step with a table",
argument: {type: :DataTable,
location: {line: 13, column: 7},
rows: [{type: :TableRow,
location: {line: 13, column: 7},
cells: [{type: :TableCell,
location: {line: 13, column: 9},
value: "a table"}]}]}}]},
{type: :ScenarioOutline,
tags: [{type: :Tag,
location: {line: 15, column: 3},
name: "@outline_tag"}],
location: {line: 16, column: 3},
keyword: "Scenario Outline",
name: "outline name",
description: " outline description",
steps: [{type: :Step,
location: {line: 18, column: 5},
keyword: "* ",
text: "a step with a doc string",
argument: {type: :DocString,
location: {line: 19, column: 7},
contentType: "content_type",
content: " lots of text"}}],
examples: [{type: :Examples,
tags: [{type: :Tag,
location: {line: 23, column: 3},
name: "@example_tag"}],
location: {line: 24, column: 3},
keyword: "Examples",
name: "examples name",
description: " examples description",
tableHeader: {type: :TableRow,
location: {line: 26, column: 5},
cells: [{type: :TableCell,
location: {line: 26, column: 7},
value: "param"}]},
tableBody: [{type: :TableRow,
location: {line: 27, column: 5},
cells: [{type: :TableCell,
location: {line: 27, column: 7},
value: "value"}]}]}]}],
},
comments: [{type: :Comment,
location: {line: 22, column: 1},
text: " # Random file comment"}],
type: :GherkinDocument}
)
end
it "parses string feature" do
parser = Parser.new
ast = parser.parse("Feature: test")
expect(ast).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "test",
children: []
},
comments: [],
type: :GherkinDocument
})
end
it "parses io feature" do
parser = Parser.new
ast = parser.parse(StringIO.new("Feature: test"))
expect(ast).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "test",
children: []
},
comments: [],
type: :GherkinDocument
})
end
it "can parse multiple features" do
parser = Parser.new
ast1 = parser.parse(TokenScanner.new("Feature: test"))
ast2 = parser.parse(TokenScanner.new("Feature: test2"))
expect(ast1).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "test",
children: []
},
comments: [],
type: :GherkinDocument
})
expect(ast2).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "test2",
children: []
},
comments: [],
type: :GherkinDocument
})
end
it "can parse feature after parse error" do
parser = Parser.new
matcher = TokenMatcher.new
expect { parser.parse(TokenScanner.new("# a comment\n" +
"Feature: Foo\n" +
" Scenario: Bar\n" +
" Given x\n" +
" ```\n" +
" unclosed docstring\n"),
matcher)
}.to raise_error(ParserError)
ast = parser.parse(TokenScanner.new("Feature: Foo\n" +
" Scenario: Bar\n" +
" Given x\n" +
' """' + "\n" +
" closed docstring\n" +
' """' + "\n"),
matcher)
expect(ast).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "en",
keyword: "Feature",
name: "Foo",
children: [{
:type=>:Scenario,
:tags=>[],
:location=>{:line=>2, :column=>3},
:keyword=>"Scenario",
:name=>"Bar",
:steps=>[{
:type=>:Step,
:location=>{:line=>3, :column=>5},
:keyword=>"Given ",
:text=>"x",
:argument=>{:type=>:DocString,
:location=>{:line=>4, :column=>7},
:content=>"closed docstring"}}]}]
},
comments: [],
type: :GherkinDocument
})
end
it "can change the default language" do
parser = Parser.new
matcher = TokenMatcher.new("no")
scanner = TokenScanner.new("Egenskap: i18n support")
ast = parser.parse(scanner, matcher)
expect(ast).to eq({
feature: {
type: :Feature,
tags: [],
location: {line: 1, column: 1},
language: "no",
keyword: "Egenskap",
name: "i18n support",
children: []
},
comments: [],
type: :GherkinDocument
})
end
end
end
|