github.com/getgauge/gauge@v1.6.9/gauge/scenario.go (about) 1 /*---------------------------------------------------------------- 2 * Copyright (c) ThoughtWorks, Inc. 3 * Licensed under the Apache License, Version 2.0 4 * See LICENSE in the project root for license information. 5 *----------------------------------------------------------------*/ 6 7 package gauge 8 9 import ( 10 "strings" 11 ) 12 13 type Scenario struct { 14 Heading *Heading 15 Steps []*Step 16 Comments []*Comment 17 Tags *Tags 18 Items []Item 19 DataTable DataTable 20 SpecDataTableRow Table 21 SpecDataTableRowIndex int 22 ScenarioDataTableRow Table 23 ScenarioDataTableRowIndex int 24 Span *Span 25 } 26 27 // Span represents scope of Scenario based on line number 28 type Span struct { 29 Start int 30 End int 31 } 32 33 func (s *Span) isInRange(lineNumber int) bool { 34 return s.Start <= lineNumber && s.End >= lineNumber 35 } 36 37 func (scenario *Scenario) AddHeading(heading *Heading) { 38 heading.HeadingType = ScenarioHeading 39 scenario.Heading = heading 40 } 41 42 func (scenario *Scenario) AddStep(step *Step) { 43 scenario.Steps = append(scenario.Steps, step) 44 scenario.AddItem(step) 45 } 46 47 func (scenario *Scenario) AddTags(tags *Tags) { 48 scenario.Tags = tags 49 scenario.AddItem(tags) 50 } 51 52 func (scenario *Scenario) AddExternalDataTable(externalTable *DataTable) { 53 scenario.DataTable = *externalTable 54 scenario.AddItem(externalTable) 55 } 56 func (scenario *Scenario) NTags() int { 57 if scenario.Tags == nil { 58 return 0 59 } 60 return len(scenario.Tags.Values()) 61 } 62 63 func (scenario *Scenario) AddComment(comment *Comment) { 64 scenario.Comments = append(scenario.Comments, comment) 65 scenario.AddItem(comment) 66 } 67 68 func (scenario *Scenario) AddDataTable(table *Table) { 69 scenario.DataTable.Table = table 70 scenario.AddItem(&scenario.DataTable) 71 } 72 73 func (scenario *Scenario) InSpan(lineNumber int) bool { 74 return scenario.Span.isInRange(lineNumber) 75 } 76 77 func (scenario *Scenario) renameSteps(oldStep *Step, newStep *Step, orderMap map[int]int) ([]*StepDiff, bool) { 78 isRefactored := false 79 diffs := []*StepDiff{} 80 isConcept := false 81 for _, step := range scenario.Steps { 82 diff, refactor := step.Rename(oldStep, newStep, isRefactored, orderMap, &isConcept) 83 if diff != nil { 84 diffs = append(diffs, diff) 85 } 86 if refactor { 87 isRefactored = refactor 88 } 89 } 90 return diffs, isRefactored 91 } 92 93 func (scenario *Scenario) AddItem(itemToAdd Item) { 94 if scenario.Items == nil { 95 scenario.Items = make([]Item, 0) 96 } 97 scenario.Items = append(scenario.Items, itemToAdd) 98 } 99 100 func (scenario *Scenario) LatestStep() *Step { 101 return scenario.Steps[len(scenario.Steps)-1] 102 } 103 104 func (scenario *Scenario) UsesArgsInSteps(args ...string) bool { 105 return UsesArgs(scenario.Steps, args...) 106 } 107 108 // skipcq CRT-P0003 109 func (scenario Scenario) Kind() TokenKind { 110 return ScenarioKind 111 } 112 113 func (scn *Scenario) HasAnyHeading(headings []string) bool { 114 for _, heading := range headings { 115 if strings.Compare(scn.Heading.Value, heading) == 0 { 116 return true 117 } 118 } 119 return false 120 }