github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/gauge/step_test.go (about) 1 // Copyright 2015 ThoughtWorks, Inc. 2 3 // This file is part of Gauge. 4 5 // Gauge is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 10 // Gauge is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 15 // You should have received a copy of the GNU General Public License 16 // along with Gauge. If not, see <http://www.gnu.org/licenses/>. 17 18 package gauge 19 20 import ( 21 "github.com/getgauge/gauge/gauge_messages" 22 "github.com/golang/protobuf/proto" 23 . "gopkg.in/check.v1" 24 ) 25 26 func (s *MySuite) TestPopulateFragmentsForSimpleStep(c *C) { 27 step := &Step{Value: "This is a simple step"} 28 29 step.PopulateFragments() 30 31 c.Assert(len(step.Fragments), Equals, 1) 32 fragment := step.Fragments[0] 33 c.Assert(fragment.GetText(), Equals, "This is a simple step") 34 c.Assert(fragment.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 35 } 36 37 func (s *MySuite) TestGetArgForStep(c *C) { 38 lookup := new(ArgLookup) 39 lookup.AddArgName("param1") 40 lookup.AddArgValue("param1", &StepArg{Value: "value1", ArgType: Static}) 41 step := &Step{Lookup: *lookup} 42 43 c.Assert(step.GetArg("param1").Value, Equals, "value1") 44 } 45 46 func (s *MySuite) TestGetArgForConceptStep(c *C) { 47 lookup := new(ArgLookup) 48 lookup.AddArgName("param1") 49 lookup.AddArgValue("param1", &StepArg{Value: "value1", ArgType: Static}) 50 concept := &Step{Lookup: *lookup, IsConcept: true} 51 stepLookup := new(ArgLookup) 52 stepLookup.AddArgName("param1") 53 stepLookup.AddArgValue("param1", &StepArg{Value: "param1", ArgType: Dynamic}) 54 step := &Step{Parent: concept, Lookup: *stepLookup} 55 56 c.Assert(step.GetArg("param1").Value, Equals, "value1") 57 } 58 59 func (s *MySuite) TestPopulateFragmentsForStepWithParameters(c *C) { 60 arg1 := &StepArg{Value: "first", ArgType: Static} 61 arg2 := &StepArg{Value: "second", ArgType: Dynamic, Name: "second"} 62 argTable := new(Table) 63 headers := []string{"header1", "header2"} 64 row1 := []string{"row1", "row2"} 65 argTable.AddHeaders(headers) 66 argTable.AddRowValues(row1) 67 arg3 := &StepArg{ArgType: SpecialString, Value: "text from file", Name: "file:foo.txt"} 68 arg4 := &StepArg{Table: *argTable, ArgType: TableArg} 69 stepArgs := []*StepArg{arg1, arg2, arg3, arg4} 70 step := &Step{Value: "{} step with {} and {}, {}", Args: stepArgs} 71 72 step.PopulateFragments() 73 74 c.Assert(len(step.Fragments), Equals, 7) 75 fragment1 := step.Fragments[0] 76 c.Assert(fragment1.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 77 c.Assert(fragment1.GetParameter().GetValue(), Equals, "first") 78 c.Assert(fragment1.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Static) 79 80 fragment2 := step.Fragments[1] 81 c.Assert(fragment2.GetText(), Equals, " step with ") 82 c.Assert(fragment2.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 83 84 fragment3 := step.Fragments[2] 85 c.Assert(fragment3.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 86 c.Assert(fragment3.GetParameter().GetValue(), Equals, "second") 87 c.Assert(fragment3.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Dynamic) 88 89 fragment4 := step.Fragments[3] 90 c.Assert(fragment4.GetText(), Equals, " and ") 91 c.Assert(fragment4.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 92 93 fragment5 := step.Fragments[4] 94 c.Assert(fragment5.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 95 c.Assert(fragment5.GetParameter().GetValue(), Equals, "text from file") 96 c.Assert(fragment5.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Special_String) 97 c.Assert(fragment5.GetParameter().GetName(), Equals, "file:foo.txt") 98 99 fragment6 := step.Fragments[5] 100 c.Assert(fragment6.GetText(), Equals, ", ") 101 c.Assert(fragment6.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 102 103 fragment7 := step.Fragments[6] 104 c.Assert(fragment7.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 105 c.Assert(fragment7.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Table) 106 protoTable := fragment7.GetParameter().GetTable() 107 c.Assert(protoTable.GetHeaders().GetCells(), DeepEquals, headers) 108 c.Assert(len(protoTable.GetRows()), Equals, 1) 109 c.Assert(protoTable.GetRows()[0].GetCells(), DeepEquals, row1) 110 } 111 112 func (s *MySuite) TestUpdatePropertiesFromAnotherStep(c *C) { 113 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "arg value", ArgType: Dynamic}} 114 fragments := []*gauge_messages.Fragment{&gauge_messages.Fragment{Text: proto.String("foo")}} 115 originalStep := &Step{LineNo: 12, 116 Value: "foo {}", 117 LineText: "foo <bar>", 118 Args: argsInStep, 119 IsConcept: false, 120 Fragments: fragments, 121 HasInlineTable: false} 122 123 destinationStep := new(Step) 124 destinationStep.CopyFrom(originalStep) 125 126 c.Assert(destinationStep, DeepEquals, originalStep) 127 } 128 129 func (s *MySuite) TestUpdatePropertiesFromAnotherConcept(c *C) { 130 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "arg value", ArgType: Dynamic}} 131 argLookup := new(ArgLookup) 132 argLookup.AddArgName("name") 133 argLookup.AddArgName("id") 134 fragments := []*gauge_messages.Fragment{&gauge_messages.Fragment{Text: proto.String("foo")}} 135 conceptSteps := []*Step{&Step{Value: "step 1"}} 136 originalConcept := &Step{ 137 LineNo: 12, 138 Value: "foo {}", 139 LineText: "foo <bar>", 140 Args: argsInStep, 141 IsConcept: true, 142 Lookup: *argLookup, 143 Fragments: fragments, 144 ConceptSteps: conceptSteps, 145 HasInlineTable: false} 146 147 destinationConcept := new(Step) 148 destinationConcept.CopyFrom(originalConcept) 149 150 c.Assert(destinationConcept, DeepEquals, originalConcept) 151 } 152 153 func (s *MySuite) TestRenameStep(c *C) { 154 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "value", ArgType: Static}, &StepArg{Name: "arg2", Value: "value1", ArgType: Static}} 155 originalStep := &Step{ 156 LineNo: 12, 157 Value: "step with {}", 158 Args: argsInStep, 159 IsConcept: false, 160 HasInlineTable: false} 161 162 argsInStep = []*StepArg{&StepArg{Name: "arg2", Value: "value1", ArgType: Static}, &StepArg{Name: "arg1", Value: "value", ArgType: Static}} 163 newStep := &Step{ 164 LineNo: 12, 165 Value: "step from {} {}", 166 Args: argsInStep, 167 IsConcept: false, 168 HasInlineTable: false} 169 orderMap := make(map[int]int) 170 orderMap[0] = 1 171 orderMap[1] = 0 172 IsConcept := false 173 isRefactored := originalStep.Rename(*originalStep, *newStep, false, orderMap, &IsConcept) 174 175 c.Assert(isRefactored, Equals, true) 176 c.Assert(originalStep.Value, Equals, "step from {} {}") 177 c.Assert(originalStep.Args[0].Name, Equals, "arg2") 178 c.Assert(originalStep.Args[1].Name, Equals, "arg1") 179 } 180 181 func (s *MySuite) TestGetLineTextForStep(c *C) { 182 step := &Step{LineText: "foo"} 183 184 c.Assert(step.getLineText(), Equals, "foo") 185 } 186 187 func (s *MySuite) TestGetLineTextForStepWithTable(c *C) { 188 step := &Step{ 189 LineText: "foo", 190 HasInlineTable: true} 191 192 c.Assert(step.getLineText(), Equals, "foo <table>") 193 } 194 195 func (s *MySuite) TestReplaceArgsWithDynamicForSpecialParam(c *C) { 196 arg1 := &StepArg{Name: "table:first/first.csv", ArgType: SpecialString, Value: "text from file"} 197 arg2 := &StepArg{Name: "file:second/second.txt", ArgType: SpecialTable, Value: "text from file"} 198 199 stepArgs := []*StepArg{arg1, arg2} 200 step := &Step{Value: "step with {} and {}", Args: stepArgs} 201 202 step.ReplaceArgsWithDynamic(stepArgs) 203 c.Assert(step.Args[0].ArgType, Equals, Dynamic) 204 c.Assert(step.Args[0].Value, Equals, "first/first.csv") 205 c.Assert(step.Args[1].ArgType, Equals, Dynamic) 206 c.Assert(step.Args[1].Value, Equals, "second/second.txt") 207 } 208 209 func (s *MySuite) TestReplaceArgs(c *C) { 210 arg1 := &StepArg{Name: "first", ArgType: Static, Value: "text from file"} 211 arg2 := &StepArg{Name: "second", ArgType: Static, Value: "text from file"} 212 213 stepArgs := []*StepArg{arg1, arg2} 214 step := &Step{Value: "step with {} and {}", Args: stepArgs} 215 216 step.ReplaceArgsWithDynamic(stepArgs) 217 c.Assert(step.Args[0].ArgType, Equals, Dynamic) 218 c.Assert(step.Args[0].Value, Equals, "text from file") 219 c.Assert(step.Args[1].ArgType, Equals, Dynamic) 220 c.Assert(step.Args[1].Value, Equals, "text from file") 221 }