| 1 |
package gherkin |
| 2 |
|
| 3 |
import ( |
| 4 |
"reflect" |
| 5 |
"strings" |
| 6 |
) |
| 7 |
|
| 8 |
// Feature represents the top-most construct in a Gherkin document. A feature |
| 9 |
// contains one or more scenarios, which in turn contains multiple steps. |
| 10 |
type Feature struct { |
| 11 |
// The filename where the feature was defined |
| 12 |
Filename string |
| 13 |
|
| 14 |
// The line number where the feature was defined |
| 15 |
Line int |
| 16 |
|
| 17 |
// The feature's title. |
| 18 |
Title string |
| 19 |
|
| 20 |
// A longer description of the feature. This is not used during runtime. |
| 21 |
Description string |
| 22 |
|
| 23 |
// Any tags associated with this feature. |
| 24 |
Tags []string |
| 25 |
|
| 26 |
// Any background scenario data that is executed prior to scenarios. |
| 27 |
Background Scenario |
| 28 |
|
| 29 |
// The scenarios associated with this feature. |
| 30 |
Scenarios []Scenario |
| 31 |
|
| 32 |
// The longest line length in the feature (including title) |
| 33 |
longestLine int |
| 34 |
} |
| 35 |
|
| 36 |
// Scenario represents a scenario (or background) of a given feature. |
| 37 |
type Scenario struct { |
| 38 |
// The filename where the scenario was defined |
| 39 |
Filename string |
| 40 |
|
| 41 |
// The line number where the scenario was defined |
| 42 |
Line int |
| 43 |
|
| 44 |
// The scenario's title. For backgrounds, this is the empty string. |
| 45 |
Title string |
| 46 |
|
| 47 |
// Any tags associated with this scenario. |
| 48 |
Tags []string |
| 49 |
|
| 50 |
// All steps associated with the scenario. |
| 51 |
Steps []Step |
| 52 |
|
| 53 |
// Contains all scenario outline example data, if provided. |
| 54 |
Examples StringData |
| 55 |
|
| 56 |
// The longest line length in the scenario (including title) |
| 57 |
longestLine int |
| 58 |
} |
| 59 |
|
| 60 |
// Step represents an individual step making up a gucumber scenario. |
| 61 |
type Step struct { |
| 62 |
// The filename where the step was defined |
| 63 |
Filename string |
| 64 |
|
| 65 |
// The line number where the step was defined |
| 66 |
Line int |
| 67 |
|
| 68 |
// The step's "type" (Given, When, Then, And, ...) |
| 69 |
// |
| 70 |
// Note that this field is normalized to the English form (e.g., "Given"). |
| 71 |
Type StepType |
| 72 |
|
| 73 |
// The text contained in the step (minus the "Type" prefix). |
| 74 |
Text string |
| 75 |
|
| 76 |
// Argument represents multi-line argument data attached to a step. |
| 77 |
Argument StringData |
| 78 |
} |
| 79 |
|
| 80 |
// StringData is multi-line docstring text attached to a step. |
| 81 |
type StringData string |
| 82 |
|
| 83 |
// TabularData is tabular text data attached to a step. |
| 84 |
type TabularData [][]string |
| 85 |
|
| 86 |
// TabularDataMap is tabular text data attached to a step organized in map |
| 87 |
// form of the header name and its associated row data. |
| 88 |
type TabularDataMap map[string][]string |
| 89 |
|
| 90 |
// StepType represents a given step type. |
| 91 |
type StepType string |
| 92 |
|
| 93 |
// ToTable turns StringData type into a TabularData type |
| 94 |
func (s StringData) ToTable() TabularData { |
| 95 |
var tabData TabularData |
| 96 |
lines := strings.Split(string(s), "\n") |
| 97 |
for _, line := range lines { |
| 98 |
row := strings.Split(line, "|") |
| 99 |
row = row[1 : len(row)-1] |
| 100 |
for i, c := range row { |
| 101 |
row[i] = strings.TrimSpace(c) |
| 102 |
} |
| 103 |
tabData = append(tabData, row) |
| 104 |
} |
| 105 |
return tabData |
| 106 |
} |
| 107 |
|
| 108 |
// IsTabular returns whether the argument data is a table |
| 109 |
func (s StringData) IsTabular() bool { |
| 110 |
return len(s) > 0 && s[0] == '|' |
| 111 |
} |
| 112 |
|
| 113 |
// ToMap converts a regular table to a map of header names to their row data. |
| 114 |
// For example: |
| 115 |
// |
| 116 |
// t := TabularData{[]string{"header1", "header2"}, []string{"col1", "col2"}} |
| 117 |
// t.ToMap() |
| 118 |
// // Output: |
| 119 |
// // map[string][]string{ |
| 120 |
// // "header1": []string{"col1"}, |
| 121 |
// // "header2": []string{"col2"}, |
| 122 |
// // } |
| 123 |
func (t TabularData) ToMap() TabularDataMap { |
| 124 |
m := TabularDataMap{} |
| 125 |
if len(t) > 1 { |
| 126 |
for _, th := range t[0] { |
| 127 |
m[th] = []string{} |
| 128 |
} |
| 129 |
for _, tr := range t[1:] { |
| 130 |
for c, td := range tr { |
| 131 |
m[t[0][c]] = append(m[t[0][c]], td) |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
return m |
| 136 |
} |
| 137 |
|
| 138 |
// NumRows returns the number of rows in a table map |
| 139 |
func (t TabularDataMap) NumRows() int { |
| 140 |
if len(t) == 0 { |
| 141 |
return 0 |
| 142 |
} |
| 143 |
return len(t[reflect.ValueOf(t).MapKeys()[0].String()]) |
| 144 |
} |
| 145 |
|
| 146 |
// LongestLine returns the longest step line in a scenario. |
| 147 |
func (s *Scenario) LongestLine() int { |
| 148 |
if s.longestLine == 0 { |
| 149 |
s.longestLine = len("Scenario: " + s.Title) |
| 150 |
for _, step := range s.Steps { |
| 151 |
if l := len(string(step.Type) + " " + step.Text); l > s.longestLine { |
| 152 |
s.longestLine = l |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
return s.longestLine |
| 157 |
} |
| 158 |
|
| 159 |
// LongestLine returns the longest step line in a feature. |
| 160 |
func (f *Feature) LongestLine() int { |
| 161 |
if f.longestLine == 0 { |
| 162 |
f.longestLine = len("Feature: " + f.Title) |
| 163 |
for _, s := range f.Scenarios { |
| 164 |
if l := s.LongestLine(); l > f.longestLine { |
| 165 |
f.longestLine = l |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
return f.longestLine |
| 170 |
} |
| 171 |
|
| 172 |
// FilterMatched returns true if the set of input filters match the feature's tags. |
| 173 |
func (f *Feature) FilterMatched(filters ...string) bool { |
| 174 |
return matchTags(f.Tags, filters) |
| 175 |
} |
| 176 |
|
| 177 |
// FilterMatched returns true if the set of input filters match the feature's tags. |
| 178 |
func (s *Scenario) FilterMatched(f *Feature, filters ...string) bool { |
| 179 |
t := []string{} |
| 180 |
t = append(t, f.Tags...) |
| 181 |
t = append(t, s.Tags...) |
| 182 |
return matchTags(t, filters) |
| 183 |
} |
| 184 |
|
| 185 |
func matchTags(tags []string, filters []string) bool { |
| 186 |
if len(filters) == 0 { // no filters means everything passes |
| 187 |
return true |
| 188 |
} |
| 189 |
for _, f := range filters { |
| 190 |
if matchFilter(f, tags) { |
| 191 |
return true // if any filter matches we succeed |
| 192 |
} |
| 193 |
} |
| 194 |
return false |
| 195 |
} |
| 196 |
|
| 197 |
func matchFilter(filter string, tags []string) bool { |
| 198 |
parts := strings.Split(filter, ",") |
| 199 |
for _, part := range parts { // all parts must match |
| 200 |
part = strings.TrimSpace(part) |
| 201 |
if part == "" { |
| 202 |
continue |
| 203 |
} |
| 204 |
|
| 205 |
if part[0] == '~' { // filter has to NOT match any tags |
| 206 |
for _, t := range tags { |
| 207 |
if part[1:] == string(t) { // tag matched, this should not happen |
| 208 |
return false |
| 209 |
} |
| 210 |
} |
| 211 |
// nothing matched, we can continue on |
| 212 |
} else { |
| 213 |
result := false |
| 214 |
for _, t := range tags { |
| 215 |
if part == string(t) { // found a match in a tag |
| 216 |
result = true |
| 217 |
break |
| 218 |
} |
| 219 |
} |
| 220 |
if !result { // no matches, this filter failed |
| 221 |
return false |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
return true |
| 226 |
} |