github.com/getgauge/gauge@v1.6.9/gauge/step_test.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 "github.com/getgauge/gauge-proto/go/gauge_messages" 11 . "gopkg.in/check.v1" 12 ) 13 14 func (s *MySuite) TestPopulateFragmentsForSimpleStep(c *C) { 15 step := &Step{Value: "This is a simple step"} 16 17 step.PopulateFragments() 18 19 c.Assert(len(step.Fragments), Equals, 1) 20 fragment := step.Fragments[0] 21 c.Assert(fragment.GetText(), Equals, "This is a simple step") 22 c.Assert(fragment.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 23 } 24 25 func (s *MySuite) TestGetArgForStep(c *C) { 26 lookup := new(ArgLookup) 27 lookup.AddArgName("param1") 28 err := lookup.AddArgValue("param1", &StepArg{Value: "value1", ArgType: Static}) 29 c.Assert(err, IsNil) 30 31 step := &Step{Lookup: *lookup} 32 stepArg, err := step.GetArg("param1") 33 c.Assert(err, IsNil) 34 c.Assert(stepArg.Value, Equals, "value1") 35 } 36 37 func (s *MySuite) TestGetArgForConceptStep(c *C) { 38 lookup := new(ArgLookup) 39 lookup.AddArgName("param1") 40 err := lookup.AddArgValue("param1", &StepArg{Value: "value1", ArgType: Static}) 41 c.Assert(err, IsNil) 42 43 concept := &Step{Lookup: *lookup, IsConcept: true} 44 stepLookup := new(ArgLookup) 45 stepLookup.AddArgName("param1") 46 err = stepLookup.AddArgValue("param1", &StepArg{Value: "param1", ArgType: Dynamic}) 47 c.Assert(err, IsNil) 48 49 step := &Step{Parent: concept, Lookup: *stepLookup} 50 51 stepArg, err := step.GetArg("param1") 52 c.Assert(err, IsNil) 53 c.Assert(stepArg.Value, Equals, "value1") 54 } 55 56 func (s *MySuite) TestPopulateFragmentsForStepWithParameters(c *C) { 57 arg1 := &StepArg{Value: "first", ArgType: Static} 58 arg2 := &StepArg{Value: "second", ArgType: Dynamic, Name: "second"} 59 argTable := new(Table) 60 headers := []string{"header1", "header2"} 61 row1 := []string{"row1", "row2"} 62 argTable.AddHeaders(headers) 63 argTable.AddRowValues(argTable.CreateTableCells(row1)) 64 arg3 := &StepArg{ArgType: SpecialString, Value: "text from file", Name: "file:foo.txt"} 65 arg4 := &StepArg{Table: *argTable, ArgType: TableArg} 66 stepArgs := []*StepArg{arg1, arg2, arg3, arg4} 67 step := &Step{Value: "{} step with {} and {}, {}", Args: stepArgs} 68 69 step.PopulateFragments() 70 71 c.Assert(len(step.Fragments), Equals, 7) 72 fragment1 := step.Fragments[0] 73 c.Assert(fragment1.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 74 c.Assert(fragment1.GetParameter().GetValue(), Equals, "first") 75 c.Assert(fragment1.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Static) 76 77 fragment2 := step.Fragments[1] 78 c.Assert(fragment2.GetText(), Equals, " step with ") 79 c.Assert(fragment2.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 80 81 fragment3 := step.Fragments[2] 82 c.Assert(fragment3.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 83 c.Assert(fragment3.GetParameter().GetValue(), Equals, "second") 84 c.Assert(fragment3.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Dynamic) 85 86 fragment4 := step.Fragments[3] 87 c.Assert(fragment4.GetText(), Equals, " and ") 88 c.Assert(fragment4.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 89 90 fragment5 := step.Fragments[4] 91 c.Assert(fragment5.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 92 c.Assert(fragment5.GetParameter().GetValue(), Equals, "text from file") 93 c.Assert(fragment5.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Special_String) 94 c.Assert(fragment5.GetParameter().GetName(), Equals, "file:foo.txt") 95 96 fragment6 := step.Fragments[5] 97 c.Assert(fragment6.GetText(), Equals, ", ") 98 c.Assert(fragment6.GetFragmentType(), Equals, gauge_messages.Fragment_Text) 99 100 fragment7 := step.Fragments[6] 101 c.Assert(fragment7.GetFragmentType(), Equals, gauge_messages.Fragment_Parameter) 102 c.Assert(fragment7.GetParameter().GetParameterType(), Equals, gauge_messages.Parameter_Table) 103 protoTable := fragment7.GetParameter().GetTable() 104 c.Assert(protoTable.GetHeaders().GetCells(), DeepEquals, headers) 105 c.Assert(len(protoTable.GetRows()), Equals, 1) 106 c.Assert(protoTable.GetRows()[0].GetCells(), DeepEquals, row1) 107 } 108 109 func (s *MySuite) TestUpdatePropertiesFromAnotherStep(c *C) { 110 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "arg value", ArgType: Dynamic}} 111 fragments := []*gauge_messages.Fragment{&gauge_messages.Fragment{Text: "foo"}} 112 originalStep := &Step{ 113 LineNo: 0, 114 Value: "foo {}", 115 LineText: "foo <bar>", 116 Args: argsInStep, 117 IsConcept: false, 118 Fragments: fragments, 119 HasInlineTable: false} 120 121 destinationStep := new(Step) 122 destinationStep.CopyFrom(originalStep) 123 124 c.Assert(destinationStep, DeepEquals, originalStep) 125 } 126 127 func (s *MySuite) TestUpdatePropertiesFromAnotherConcept(c *C) { 128 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "arg value", ArgType: Dynamic}} 129 argLookup := new(ArgLookup) 130 argLookup.AddArgName("name") 131 argLookup.AddArgName("id") 132 fragments := []*gauge_messages.Fragment{&gauge_messages.Fragment{Text: "foo"}} 133 conceptSteps := []*Step{&Step{Value: "step 1"}} 134 originalConcept := &Step{ 135 LineNo: 0, 136 Value: "foo {}", 137 LineText: "foo <bar>", 138 Args: argsInStep, 139 IsConcept: true, 140 Lookup: *argLookup, 141 Fragments: fragments, 142 ConceptSteps: conceptSteps, 143 HasInlineTable: false, 144 } 145 146 destinationConcept := new(Step) 147 destinationConcept.CopyFrom(originalConcept) 148 149 c.Assert(destinationConcept, DeepEquals, originalConcept) 150 } 151 152 func (s *MySuite) TestRenameStep(c *C) { 153 argsInStep := []*StepArg{&StepArg{Name: "arg1", Value: "value", ArgType: Static}, &StepArg{Name: "arg2", Value: "value1", ArgType: Static}} 154 originalStep := &Step{ 155 LineNo: 12, 156 Value: "step with {}", 157 Args: argsInStep, 158 IsConcept: false, 159 HasInlineTable: false} 160 161 argsInStep = []*StepArg{&StepArg{Name: "arg2", Value: "value1", ArgType: Static}, &StepArg{Name: "arg1", Value: "value", ArgType: Static}} 162 newStep := &Step{ 163 LineNo: 12, 164 Value: "step from {} {}", 165 Args: argsInStep, 166 IsConcept: false, 167 HasInlineTable: false} 168 orderMap := make(map[int]int) 169 orderMap[0] = 1 170 orderMap[1] = 0 171 IsConcept := false 172 diff, isRefactored := originalStep.Rename(originalStep, newStep, false, orderMap, &IsConcept) 173 174 c.Assert(isRefactored, Equals, true) 175 c.Assert(originalStep.Value, Equals, "step from {} {}") 176 c.Assert(originalStep.Args[0].Name, Equals, "arg2") 177 c.Assert(originalStep.Args[1].Name, Equals, "arg1") 178 c.Assert(diff.OldStep.Value, Equals, "step with {}") 179 c.Assert(diff.NewStep.Value, Equals, "step from {} {}") 180 } 181 182 func (s *MySuite) TestRenameConcept(c *C) { 183 originalStep := &Step{ 184 LineNo: 3, 185 Value: "concept with text file", 186 IsConcept: true, 187 HasInlineTable: false} 188 189 argsInStep := []*StepArg{&StepArg{Name: "file:foo.txt", Value: "text from file", ArgType: SpecialString}} 190 newStep := &Step{ 191 LineNo: 3, 192 Value: "concept with text file {}", 193 Args: argsInStep, 194 IsConcept: true, 195 HasInlineTable: false} 196 orderMap := make(map[int]int) 197 orderMap[0] = -1 198 IsConcept := true 199 diff, isRefactored := originalStep.Rename(originalStep, newStep, false, orderMap, &IsConcept) 200 c.Assert(isRefactored, Equals, true) 201 c.Assert(originalStep.Value, Equals, "concept with text file {}") 202 c.Assert(originalStep.Args[0].Name, Equals, "arg0") 203 c.Assert(newStep.Args[0].Name, Equals, "file:foo.txt") 204 c.Assert(diff.OldStep.Value, Equals, "concept with text file") 205 c.Assert(diff.NewStep.Value, Equals, "concept with text file {}") 206 } 207 208 func (s *MySuite) TestGetLineTextForStep(c *C) { 209 step := &Step{LineText: "foo"} 210 211 c.Assert(step.GetLineText(), Equals, "foo") 212 } 213 214 func (s *MySuite) TestGetLineTextForStepWithTable(c *C) { 215 step := &Step{ 216 LineText: "foo", 217 HasInlineTable: true} 218 219 c.Assert(step.GetLineText(), Equals, "foo <table>") 220 } 221 222 func (s *MySuite) TestReplaceArgsWithDynamicForSpecialParam(c *C) { 223 arg1 := &StepArg{Name: "table:first/first.csv", ArgType: SpecialString, Value: "text from file"} 224 arg2 := &StepArg{Name: "file:second/second.txt", ArgType: SpecialTable, Value: "text from file"} 225 226 stepArgs := []*StepArg{arg1, arg2} 227 step := &Step{Value: "step with {} and {}", Args: stepArgs} 228 229 step.ReplaceArgsWithDynamic(stepArgs) 230 c.Assert(step.Args[0].ArgType, Equals, Dynamic) 231 c.Assert(step.Args[0].Name, Equals, "first/first.csv") 232 c.Assert(step.Args[1].ArgType, Equals, Dynamic) 233 c.Assert(step.Args[1].Name, Equals, "second/second.txt") 234 } 235 236 func (s *MySuite) TestReplaceArgs(c *C) { 237 arg1 := &StepArg{Name: "first", ArgType: Static, Value: "text from file"} 238 arg2 := &StepArg{Name: "second", ArgType: Static, Value: "text from file"} 239 240 stepArgs := []*StepArg{arg1, arg2} 241 step := &Step{Value: "step with {} and {}", Args: stepArgs} 242 243 step.ReplaceArgsWithDynamic(stepArgs) 244 c.Assert(step.Args[0].ArgType, Equals, Dynamic) 245 c.Assert(step.Args[0].Name, Equals, "text from file") 246 c.Assert(step.Args[1].ArgType, Equals, Dynamic) 247 c.Assert(step.Args[1].Name, Equals, "text from file") 248 } 249 250 func (s *MySuite) TestUsageDynamicArgs(c *C) { 251 252 dArgs := []string{"first", "second"} 253 254 sArg := &StepArg{ArgType: Dynamic, Value: "first"} 255 256 step := &Step{Value: "step with {}", Args: []*StepArg{sArg}} 257 258 usesDynamicArgs := step.UsesDynamicArgs(dArgs...) 259 260 c.Assert(usesDynamicArgs, Equals, true) 261 262 } 263 264 func (s *MySuite) TestStepDoesNotUsageDynamicArgs(c *C) { 265 266 dArgs := []string{"first", "second"} 267 268 sArg := &StepArg{ArgType: Dynamic, Value: "third"} 269 270 step := &Step{Value: "step with {}", Args: []*StepArg{sArg}} 271 272 usesDynamicArgs := step.UsesDynamicArgs(dArgs...) 273 274 c.Assert(usesDynamicArgs, Equals, false) 275 276 } 277 278 func (s *MySuite) TestInlineTableUsageDynamicArgs(c *C) { 279 headers := []string{"header"} 280 cells := [][]TableCell{ 281 { 282 { 283 CellType: Dynamic, 284 Value: "first", 285 }, 286 }, 287 } 288 table := NewTable(headers, cells, 1) 289 290 dArgs := []string{"first", "second"} 291 292 sArg := &StepArg{Name: "hello", ArgType: TableArg, Table: *table} 293 294 step := &Step{Value: "step with {}", Args: []*StepArg{sArg}} 295 296 usesDynamicArgs := step.UsesDynamicArgs(dArgs...) 297 298 c.Assert(usesDynamicArgs, Equals, true) 299 300 } 301 302 func (s *MySuite) TestLastArgs(c *C) { 303 headers := []string{"header"} 304 cells := [][]TableCell{ 305 { 306 { 307 CellType: Dynamic, 308 Value: "first", 309 }, 310 }, 311 } 312 313 table := NewTable(headers, cells, 1) 314 315 dArg := &StepArg{Name: "hello", ArgType: TableArg, Table: *table} 316 317 step := &Step{Value: "step with {}", Args: []*StepArg{dArg}} 318 319 la := step.GetLastArg() 320 321 c.Assert(la, DeepEquals, dArg) 322 323 }