github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/parser/stepParser_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) TestParsingSimpleStep(c *C) { 26 parser := new(SpecParser) 27 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("sample step").String() 28 29 tokens, err := parser.GenerateTokens(specText, "") 30 31 c.Assert(err, IsNil) 32 c.Assert(len(tokens), Equals, 3) 33 34 stepToken := tokens[2] 35 c.Assert(stepToken.Kind, Equals, gauge.StepKind) 36 c.Assert(stepToken.Value, Equals, "sample step") 37 } 38 39 func (s *MySuite) TestParsingEmptyStepTextShouldThrowError(c *C) { 40 parser := new(SpecParser) 41 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("").String() 42 43 _, errs := parser.GenerateTokens(specText, "foo.spec") 44 45 c.Assert(len(errs) > 0, Equals, true) 46 c.Assert(errs[0].Error(), Equals, "foo.spec:3 Step should not be blank => ''") 47 } 48 49 func (s *MySuite) TestParsingStepWithParams(c *C) { 50 parser := new(SpecParser) 51 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("enter user \"john\"").String() 52 53 tokens, err := parser.GenerateTokens(specText, "") 54 55 c.Assert(err, IsNil) 56 c.Assert(len(tokens), Equals, 3) 57 58 stepToken := tokens[2] 59 c.Assert(stepToken.Kind, Equals, gauge.StepKind) 60 c.Assert(stepToken.Value, Equals, "enter user {static}") 61 c.Assert(len(stepToken.Args), Equals, 1) 62 c.Assert(stepToken.Args[0], Equals, "john") 63 } 64 65 func (s *MySuite) TestParsingStepWithParametersWithQuotes(c *C) { 66 parser := new(SpecParser) 67 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("\"param \\\"in quote\\\"\" step ").step("another * step with \"john 12 *-_{} \\\\ './;[]\" and \"second\"").String() 68 69 tokens, err := parser.GenerateTokens(specText, "") 70 71 c.Assert(err, IsNil) 72 c.Assert(len(tokens), Equals, 4) 73 74 firstStepToken := tokens[2] 75 c.Assert(firstStepToken.Kind, Equals, gauge.StepKind) 76 c.Assert(firstStepToken.Value, Equals, "{static} step") 77 c.Assert(len(firstStepToken.Args), Equals, 1) 78 c.Assert(firstStepToken.Args[0], Equals, "param \"in quote\"") 79 80 secondStepToken := tokens[3] 81 c.Assert(secondStepToken.Kind, Equals, gauge.StepKind) 82 c.Assert(secondStepToken.Value, Equals, "another * step with {static} and {static}") 83 c.Assert(len(secondStepToken.Args), Equals, 2) 84 c.Assert(secondStepToken.Args[0], Equals, "john 12 *-_{} \\ './;[]") 85 c.Assert(secondStepToken.Args[1], Equals, "second") 86 87 } 88 89 func (s *MySuite) TestParsingStepWithUnmatchedOpeningQuote(c *C) { 90 parser := new(SpecParser) 91 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("sample step \"param").String() 92 93 _, errs := parser.GenerateTokens(specText, "foo.spec") 94 95 c.Assert(len(errs) > 0, Equals, true) 96 c.Assert(errs[0].Error(), Equals, "foo.spec:3 String not terminated => 'sample step \"param'") 97 } 98 99 func (s *MySuite) TestParsingStepWithEscaping(c *C) { 100 parser := new(SpecParser) 101 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("step with \\").String() 102 103 tokens, err := parser.GenerateTokens(specText, "") 104 105 c.Assert(err, IsNil) 106 stepToken := tokens[2] 107 c.Assert(stepToken.Value, Equals, "step with") 108 } 109 110 func (s *MySuite) TestParsingExceptionIfStepContainsReservedChars(c *C) { 111 parser := new(SpecParser) 112 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("step with {braces}").String() 113 114 _, errs := parser.GenerateTokens(specText, "foo.spec") 115 116 c.Assert(len(errs) > 0, Equals, true) 117 c.Assert(errs[0].Error(), Equals, "foo.spec:3 '{' is a reserved character and should be escaped => 'step with {braces}'") 118 } 119 120 func (s *MySuite) TestParsingStepContainsEscapedReservedChars(c *C) { 121 parser := new(SpecParser) 122 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("step with \\{braces\\}").String() 123 124 tokens, err := parser.GenerateTokens(specText, "") 125 126 c.Assert(err, IsNil) 127 stepToken := tokens[2] 128 c.Assert(stepToken.Value, Equals, "step with {braces}") 129 } 130 131 func (s *MySuite) TestParsingSimpleStepWithDynamicParameter(c *C) { 132 parser := new(SpecParser) 133 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("Step with \"static param\" and <name1>").String() 134 135 tokens, err := parser.GenerateTokens(specText, "") 136 c.Assert(err, IsNil) 137 c.Assert(len(tokens), Equals, 3) 138 139 stepToken := tokens[2] 140 c.Assert(stepToken.Value, Equals, "Step with {static} and {dynamic}") 141 c.Assert(stepToken.Args[0], Equals, "static param") 142 c.Assert(stepToken.Args[1], Equals, "name1") 143 } 144 145 func (s *MySuite) TestParsingStepWithUnmatchedDynamicParameterCharacter(c *C) { 146 parser := new(SpecParser) 147 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("Step with \"static param\" and <name1").String() 148 149 _, errs := parser.GenerateTokens(specText, "foo.spec") 150 151 c.Assert(len(errs) > 0, Equals, true) 152 c.Assert(errs[0].Error(), Equals, "foo.spec:3 Dynamic parameter not terminated => 'Step with \"static param\" and <name1'") 153 154 } 155 156 func (s *MySuite) TestParsingContext(c *C) { 157 parser := new(SpecParser) 158 specText := SpecBuilder().specHeading("Spec heading with hash ").step("Context with \"param\"").scenarioHeading("Scenario Heading").String() 159 160 tokens, err := parser.GenerateTokens(specText, "") 161 162 c.Assert(err, IsNil) 163 contextToken := tokens[1] 164 c.Assert(contextToken.Kind, Equals, gauge.StepKind) 165 c.Assert(contextToken.Value, Equals, "Context with {static}") 166 c.Assert(contextToken.Args[0], Equals, "param") 167 } 168 169 func (s *MySuite) TestParsingThrowsErrorWhenStepIsPresentWithoutStep(c *C) { 170 parser := new(SpecParser) 171 specText := SpecBuilder().step("step without spec heading").String() 172 173 tokens, err := parser.GenerateTokens(specText, "") 174 175 c.Assert(err, IsNil) 176 c.Assert(tokens[0].Kind, Equals, gauge.StepKind) 177 c.Assert(tokens[0].Value, Equals, "step without spec heading") 178 179 } 180 181 func (s *MySuite) TestParsingStepWithSimpleSpecialParameter(c *C) { 182 parser := new(SpecParser) 183 specText := SpecBuilder().specHeading("Spec heading with hash ").scenarioHeading("Scenario Heading").step("Step with special parameter <table:user.csv>").String() 184 185 tokens, err := parser.GenerateTokens(specText, "") 186 187 c.Assert(err, IsNil) 188 c.Assert(len(tokens), Equals, 3) 189 190 c.Assert(tokens[2].Kind, Equals, gauge.StepKind) 191 c.Assert(tokens[2].Value, Equals, "Step with special parameter {special}") 192 c.Assert(len(tokens[2].Args), Equals, 1) 193 c.Assert(tokens[2].Args[0], Equals, "table:user.csv") 194 } 195 196 func (s *MySuite) TestParsingStepWithSpecialParametersWithWhiteSpaces(c *C) { 197 parser := new(SpecParser) 198 specText := SpecBuilder().specHeading("Spec heading with hash ").step("Step with \"first\" and special parameter <table : user.csv>").step("Another with <name> and <file :something.txt>").String() 199 200 tokens, err := parser.GenerateTokens(specText, "") 201 202 c.Assert(err, IsNil) 203 c.Assert(len(tokens), Equals, 3) 204 205 c.Assert(tokens[1].Kind, Equals, gauge.StepKind) 206 c.Assert(tokens[1].Value, Equals, "Step with {static} and special parameter {special}") 207 c.Assert(len(tokens[1].Args), Equals, 2) 208 c.Assert(tokens[1].Args[0], Equals, "first") 209 c.Assert(tokens[1].Args[1], Equals, "table : user.csv") 210 211 c.Assert(tokens[2].Kind, Equals, gauge.StepKind) 212 c.Assert(tokens[2].Value, Equals, "Another with {dynamic} and {special}") 213 c.Assert(len(tokens[2].Args), Equals, 2) 214 c.Assert(tokens[2].Args[0], Equals, "name") 215 c.Assert(tokens[2].Args[1], Equals, "file :something.txt") 216 } 217 218 func (s *MySuite) TestParsingStepWithStaticParamHavingEscapeChar(c *C) { 219 tokenValue, args, err := processStepText(`step "a\nb" only`) 220 c.Assert(err, IsNil) 221 c.Assert(args[0], Equals, "a\nb") 222 c.Assert(tokenValue, Equals, "step {static} only") 223 } 224 225 func (s *MySuite) TestParsingStepWithStaticParamHavingDifferentEscapeChar(c *C) { 226 tokenValue, args, err := processStepText(`step "foo bar \"hello\" all \"he\n\n\tyy \"foo\"hjhj\"" only`) 227 c.Assert(err, IsNil) 228 c.Assert(args[0], Equals, "foo bar \"hello\" all \"he\n\n\tyy \"foo\"hjhj\"") 229 c.Assert(tokenValue, Equals, "step {static} only") 230 } 231 232 func (s *MySuite) TestParsingStepWithStaticParamHavingNestedEscapeSequences(c *C) { 233 tokenValue, args, err := processStepText(`step "foo \t tab \n \"a\"dd r \\n" only`) 234 c.Assert(err, IsNil) 235 c.Assert(args[0], Equals, "foo \t tab \n \"a\"dd r \\n") 236 c.Assert(tokenValue, Equals, "step {static} only") 237 } 238 239 func (s *MySuite) TestParsingStepWithSlash(c *C) { 240 tokenValue, args, err := processStepText(`step foo \ only`) 241 c.Assert(err, IsNil) 242 c.Assert(len(args), Equals, 0) 243 c.Assert(tokenValue, Equals, "step foo \\ only") 244 } 245 246 func (s *MySuite) TestParsingStepWithTab(c *C) { 247 tokenValue, args, err := processStepText("step foo \t only") 248 c.Assert(err, IsNil) 249 c.Assert(len(args), Equals, 0) 250 c.Assert(tokenValue, Equals, "step foo \t only") 251 }