1package gherkin
2
3import (
4	"fmt"
5	messages "github.com/cucumber/messages-go/v10"
6	"os"
7	"strings"
8)
9
10func ExampleParseGherkinDocument() {
11
12	input := `Feature: Tagged Examples
13
14  Scenario Outline: minimalistic
15    Given the <what>
16
17    @foo
18    Examples:
19      | what |
20      | foo  |
21
22    @bar
23    Examples:
24      | what |
25      | bar  |
26
27  @zap
28  Scenario: ha ok
29`
30	r := strings.NewReader(input)
31
32	gherkinDocument, err := ParseGherkinDocument(r, (&messages.Incrementing{}).NewId)
33	if err != nil {
34		fmt.Fprintf(os.Stdout, "%s\n", err)
35		return
36	}
37	feature := gherkinDocument.Feature
38	//fmt.Fprintf(os.Stdout, "Location: %v\n", feature.Location)
39	fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature.Keyword)
40	fmt.Fprintf(os.Stdout, "Name: %+v\n", feature.Name)
41	fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature.Children))
42
43	scenario1 := feature.Children[0].GetScenario()
44	//fmt.Fprintf(os.Stdout, " 1: Location: %+v\n", scenario1.Location)
45	fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario1.Keyword)
46	fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario1.Name)
47	fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario1.Steps))
48
49	scenario2 := feature.Children[1].GetScenario()
50	//fmt.Fprintf(os.Stdout, " 2: Location: %+v\n", scenario2.Location)
51	fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario2.Keyword)
52	fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario2.Name)
53	fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario2.Steps))
54
55	// Output:
56	// Keyword: Feature
57	// Name: Tagged Examples
58	// Children: length: 2
59	//     Keyword: Scenario Outline
60	//     Name: minimalistic
61	//     Steps: length: 1
62	//     Keyword: Scenario
63	//     Name: ha ok
64	//     Steps: length: 0
65}
66
67func ExampleParseGherkinDocument_multiple() {
68
69	builder := NewAstBuilder((&messages.Incrementing{}).NewId)
70	parser := NewParser(builder)
71	parser.StopAtFirstError(false)
72	matcher := NewMatcher(GherkinDialectsBuildin())
73
74	input1 := `Feature: Test`
75	r1 := strings.NewReader(input1)
76
77	err1 := parser.Parse(NewScanner(r1), matcher)
78	if err1 != nil {
79		fmt.Fprintf(os.Stdout, "%s\n", err1)
80		return
81	}
82	doc1 := builder.GetGherkinDocument()
83	feature1 := doc1.Feature
84	//fmt.Fprintf(os.Stdout, "Location: %+v\n", feature1.Location)
85	fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature1.Keyword)
86	fmt.Fprintf(os.Stdout, "Name: %+v\n", feature1.Name)
87	fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature1.Children))
88	fmt.Fprintf(os.Stdout, "\n")
89
90	input2 := `Feature: Test2`
91	r2 := strings.NewReader(input2)
92
93	err2 := parser.Parse(NewScanner(r2), matcher)
94	if err2 != nil {
95		fmt.Fprintf(os.Stdout, "%s\n", err2)
96		return
97	}
98	doc2 := builder.GetGherkinDocument()
99	feature2 := doc2.Feature
100	//fmt.Fprintf(os.Stdout, "Location: %+v\n", feature2.Location)
101	fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature2.Keyword)
102	fmt.Fprintf(os.Stdout, "Name: %+v\n", feature2.Name)
103	fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature2.Children))
104
105	// Output:
106	// Keyword: Feature
107	// Name: Test
108	// Children: length: 0
109	//
110	// Keyword: Feature
111	// Name: Test2
112	// Children: length: 0
113}
114
115func ExampleParseGherkinDocument_error() {
116
117	builder := NewAstBuilder((&messages.Incrementing{}).NewId)
118	parser := NewParser(builder)
119	parser.StopAtFirstError(false)
120	matcher := NewMatcher(GherkinDialectsBuildin())
121
122	input1 := `# a comment
123Feature: Foo
124  Scenario: Bar
125    Given x
126` + "      ```" + `
127      unclosed docstring`
128	r1 := strings.NewReader(input1)
129
130	err1 := parser.Parse(NewScanner(r1), matcher)
131	if err1 != nil {
132		fmt.Fprintf(os.Stdout, "%s\n", err1)
133	}
134	fmt.Fprintf(os.Stdout, "\n")
135
136	input2 := `Feature: Foo
137  Scenario: Bar
138    Given x
139      """
140      closed docstring
141      """`
142	r2 := strings.NewReader(input2)
143
144	err2 := parser.Parse(NewScanner(r2), matcher)
145	if err2 != nil {
146		fmt.Fprintf(os.Stdout, "%s\n", err2)
147		return
148	}
149	doc2 := builder.GetGherkinDocument()
150	fmt.Fprintf(os.Stdout, "Comments: length: %+v\n", len(doc2.Comments))
151
152	feature2 := doc2.Feature
153	//fmt.Fprintf(os.Stdout, "Location: %+v\n", feature2.Location)
154	fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature2.Keyword)
155	fmt.Fprintf(os.Stdout, "Name: %+v\n", feature2.Name)
156	fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature2.Children))
157	scenario1 := feature2.Children[0].GetScenario()
158	//fmt.Fprintf(os.Stdout, " 1: Location: %+v\n", scenario1.Location)
159	fmt.Fprintf(os.Stdout, "    Keyword: %+v\n", scenario1.Keyword)
160	fmt.Fprintf(os.Stdout, "    Name: %+v\n", scenario1.Name)
161	fmt.Fprintf(os.Stdout, "    Steps: length: %+v\n", len(scenario1.Steps))
162
163	// Output:
164	// Parser errors:
165	// (7:0): unexpected end of file, expected: #DocStringSeparator, #Other
166	//
167	// Comments: length: 0
168	// Keyword: Feature
169	// Name: Foo
170	// Children: length: 1
171	//     Keyword: Scenario
172	//     Name: Bar
173	//     Steps: length: 1
174}
175
176func ExampleParseGherkinDocument_dialect() {
177
178	input := "Egenskap: i18n support"
179	r := strings.NewReader(input)
180
181	gherkinDocument, err := ParseGherkinDocumentForLanguage(r, "no", (&messages.Incrementing{}).NewId)
182	if err != nil {
183		fmt.Fprintf(os.Stdout, "%s\n", err)
184		return
185	}
186	feature := gherkinDocument.Feature
187	//fmt.Fprintf(os.Stdout, "Location: %+v\n", feature.Location)
188	fmt.Fprintf(os.Stdout, "Keyword: %+v\n", feature.Keyword)
189	fmt.Fprintf(os.Stdout, "Name: %+v\n", feature.Name)
190	fmt.Fprintf(os.Stdout, "Children: length: %+v\n", len(feature.Children))
191
192	// Output:
193	//
194	// Keyword: Egenskap
195	// Name: i18n support
196	// Children: length: 0
197}
198