github.com/mattdotmatt/gauge@v0.3.2-0.20160421115137-425a4cdccb62/parser/parse_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 parser 19 20 import ( 21 "github.com/getgauge/gauge/gauge" 22 . "gopkg.in/check.v1" 23 ) 24 25 func (s *MySuite) TestSimpleStepAfterStepValueExtraction(c *C) { 26 stepText := "a simple step" 27 stepValue, err := ExtractStepValueAndParams(stepText, false) 28 29 args := stepValue.Args 30 c.Assert(err, Equals, nil) 31 c.Assert(len(args), Equals, 0) 32 c.Assert(stepValue.StepValue, Equals, "a simple step") 33 c.Assert(stepValue.ParameterizedStepValue, Equals, "a simple step") 34 } 35 36 func (s *MySuite) TestStepWithColonAfterStepValueExtraction(c *C) { 37 stepText := "a : simple step \"hello\"" 38 stepValue, err := ExtractStepValueAndParams(stepText, false) 39 args := stepValue.Args 40 c.Assert(err, Equals, nil) 41 c.Assert(len(args), Equals, 1) 42 c.Assert(stepValue.StepValue, Equals, "a : simple step {}") 43 c.Assert(stepValue.ParameterizedStepValue, Equals, "a : simple step <hello>") 44 } 45 46 func (s *MySuite) TestSimpleStepAfterStepValueExtractionForStepWithAParam(c *C) { 47 stepText := "Comment <a>" 48 stepValue, err := ExtractStepValueAndParams(stepText, false) 49 50 args := stepValue.Args 51 c.Assert(err, Equals, nil) 52 c.Assert(len(args), Equals, 1) 53 c.Assert(stepValue.StepValue, Equals, "Comment {}") 54 c.Assert(stepValue.ParameterizedStepValue, Equals, "Comment <a>") 55 } 56 57 func (s *MySuite) TestAddingTableParamAfterStepValueExtraction(c *C) { 58 stepText := "a simple step" 59 stepValue, err := ExtractStepValueAndParams(stepText, true) 60 61 args := stepValue.Args 62 c.Assert(err, Equals, nil) 63 c.Assert(len(args), Equals, 1) 64 c.Assert(args[0], Equals, string(gauge.TableArg)) 65 c.Assert(stepValue.StepValue, Equals, "a simple step {}") 66 c.Assert(stepValue.ParameterizedStepValue, Equals, "a simple step <table>") 67 } 68 69 func (s *MySuite) TestAddingTableParamAfterStepValueExtractionForStepWithExistingParam(c *C) { 70 stepText := "a \"param1\" step with multiple params <param2> <file:specialParam>" 71 stepValue, err := ExtractStepValueAndParams(stepText, true) 72 73 args := stepValue.Args 74 c.Assert(err, Equals, nil) 75 c.Assert(len(args), Equals, 4) 76 c.Assert(args[0], Equals, "param1") 77 c.Assert(args[1], Equals, "param2") 78 c.Assert(args[2], Equals, "file:specialParam") 79 c.Assert(args[3], Equals, "table") 80 c.Assert(stepValue.StepValue, Equals, "a {} step with multiple params {} {} {}") 81 c.Assert(stepValue.ParameterizedStepValue, Equals, "a <param1> step with multiple params <param2> <file:specialParam> <table>") 82 } 83 84 func (s *MySuite) TestAfterStepValueExtractionForStepWithExistingParam(c *C) { 85 stepText := "a \"param1\" step with multiple params <param2> <file:specialParam>" 86 stepValue, err := ExtractStepValueAndParams(stepText, false) 87 88 args := stepValue.Args 89 c.Assert(err, Equals, nil) 90 c.Assert(len(args), Equals, 3) 91 c.Assert(args[0], Equals, "param1") 92 c.Assert(args[1], Equals, "param2") 93 c.Assert(args[2], Equals, "file:specialParam") 94 c.Assert(stepValue.StepValue, Equals, "a {} step with multiple params {} {}") 95 c.Assert(stepValue.ParameterizedStepValue, Equals, "a <param1> step with multiple params <param2> <file:specialParam>") 96 } 97 98 func (s *MySuite) TestCreateStepValueFromStep(c *C) { 99 step := &gauge.Step{Value: "simple step with {} and {}", Args: []*gauge.StepArg{staticArg("hello"), dynamicArg("desc")}} 100 stepValue := CreateStepValue(step) 101 102 args := stepValue.Args 103 c.Assert(len(args), Equals, 2) 104 c.Assert(args[0], Equals, "hello") 105 c.Assert(args[1], Equals, "desc") 106 c.Assert(stepValue.StepValue, Equals, "simple step with {} and {}") 107 c.Assert(stepValue.ParameterizedStepValue, Equals, "simple step with <hello> and <desc>") 108 } 109 110 func (s *MySuite) TestCreateStepValueFromStepWithSpecialParams(c *C) { 111 step := &gauge.Step{Value: "a step with {}, {} and {}", Args: []*gauge.StepArg{specialTableArg("hello"), specialStringArg("file:user.txt"), tableArgument()}} 112 stepValue := CreateStepValue(step) 113 114 args := stepValue.Args 115 c.Assert(len(args), Equals, 3) 116 c.Assert(args[0], Equals, "hello") 117 c.Assert(args[1], Equals, "file:user.txt") 118 c.Assert(args[2], Equals, "table") 119 c.Assert(stepValue.StepValue, Equals, "a step with {}, {} and {}") 120 c.Assert(stepValue.ParameterizedStepValue, Equals, "a step with <hello>, <file:user.txt> and <table>") 121 } 122 123 func staticArg(val string) *gauge.StepArg { 124 return &gauge.StepArg{ArgType: gauge.Static, Value: val} 125 } 126 127 func dynamicArg(val string) *gauge.StepArg { 128 return &gauge.StepArg{ArgType: gauge.Dynamic, Value: val} 129 } 130 131 func tableArgument() *gauge.StepArg { 132 return &gauge.StepArg{ArgType: gauge.TableArg} 133 } 134 135 func specialTableArg(val string) *gauge.StepArg { 136 return &gauge.StepArg{ArgType: gauge.SpecialTable, Name: val} 137 } 138 139 func specialStringArg(val string) *gauge.StepArg { 140 return &gauge.StepArg{ArgType: gauge.SpecialString, Name: val} 141 }