github.com/getgauge/gauge@v1.6.9/conceptExtractor/conceptExtractor_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 conceptExtractor 8 9 import ( 10 "testing" 11 12 "os" 13 14 "github.com/getgauge/gauge-proto/go/gauge_messages" 15 "github.com/getgauge/gauge/config" 16 "github.com/getgauge/gauge/gauge" 17 . "gopkg.in/check.v1" 18 ) 19 20 func Test(t *testing.T) { TestingT(t) } 21 22 type MySuite struct{} 23 24 var _ = Suite(&MySuite{}) 25 26 func (s *MySuite) SetUpTest(c *C) { 27 config.ProjectRoot, _ = os.Getwd() 28 } 29 30 func (s *MySuite) TestExtractConceptWithoutParameters(c *C) { 31 STEP := "step that takes a table" 32 name := "concept" 33 conceptName := &gauge_messages.Step{Name: name} 34 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP}}, "# sdfdsf\nsome comment\n* some step\n## sce\n* step", "") 35 36 c.Assert(err, IsNil) 37 c.Assert(concept, Equals, "# concept\n* step that takes a table\n") 38 c.Assert(conceptText, Equals, "* concept") 39 } 40 41 func (s *MySuite) TestExtractConcept(c *C) { 42 STEP := "step that takes a table \"arg\"" 43 name := "concept with \"arg\"" 44 conceptName := &gauge_messages.Step{Name: name} 45 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP}}, "# sdfdsf\nsome comment\n* some step\n## sce\n* step", "") 46 47 c.Assert(err, IsNil) 48 c.Assert(concept, Equals, "# concept with <arg>\n* step that takes a table <arg>\n") 49 c.Assert(conceptText, Equals, "* concept with \"arg\"") 50 } 51 52 func (s *MySuite) TestExtractConceptWithSkippedParameters(c *C) { 53 STEP := "step that takes a table \"arg\" and \"hello again\" " 54 name := "concept with \"arg\"" 55 conceptName := &gauge_messages.Step{Name: name} 56 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP}}, "# sdfdsf\nsome comment\n* some step\n## sce\n* step", "") 57 58 c.Assert(err, IsNil) 59 c.Assert(concept, Equals, "# concept with <arg>\n* step that takes a table <arg> and \"hello again\"\n") 60 c.Assert(conceptText, Equals, "* concept with \"arg\"") 61 } 62 63 func (s *MySuite) TestExtractConceptWithDynamicAndStaticParameters(c *C) { 64 STEP := "step that takes a table \"arg\" and <hello again> " 65 name := "concept with \"arg\" <hello again>" 66 conceptName := &gauge_messages.Step{Name: name} 67 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP}}, "# sdfdsf\n\n|hello again|name|\n|hey|hello|\n\n## sce\n* step", "") 68 69 c.Assert(err, IsNil) 70 c.Assert(concept, Equals, "# concept with <arg> <hello again>\n* step that takes a table <arg> and <hello again>\n") 71 c.Assert(conceptText, Equals, "* concept with \"arg\" <hello again>") 72 } 73 74 func (s *MySuite) TestExtractConceptWithDynamicAndStaticParametersWithParamChar(c *C) { 75 STEP := "step that takes a table \"arg <hello>\" and <hello again> " 76 name := "concept with \"arg <hello>\" <hello again>" 77 conceptName := &gauge_messages.Step{Name: name} 78 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP}}, "# sdfdsf\n\n|hello again|name|\n|hey|hello|\n\n## sce\n* step", "") 79 80 c.Assert(err, IsNil) 81 c.Assert(concept, Equals, "# concept with <arg {hello}> <hello again>\n* step that takes a table <arg {hello}> and <hello again>\n") 82 c.Assert(conceptText, Equals, "* concept with \"arg <hello>\" <hello again>") 83 } 84 85 func (s *MySuite) TestExtractConceptWithTableAsArg(c *C) { 86 STEP := "step that takes a table" 87 name := "concept with <table1>" 88 conceptName := &gauge_messages.Step{Name: name} 89 tableName := TABLE + "1" 90 table := ` |id|name| 91 |--|----| 92 |1 |foo | 93 |2 |bar | 94 ` 95 concept, conceptText, err := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}, 96 &gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}}, "# sdfdsf\nsome comment\n* some step\n## sce\n* step", "") 97 98 c.Assert(err, IsNil) 99 c.Assert(concept, Equals, "# concept with <table1>\n* step that takes a table <table1>\n* step that takes a table <table1>\n") 100 c.Assert(conceptText, Equals, "* concept with "+` 101 102 |id|name| 103 |--|----| 104 |1 |foo | 105 |2 |bar | 106 `) 107 } 108 109 func (s *MySuite) TestExtractConceptWithTableAsArgAndTableWithDynamicArgs(c *C) { 110 STEP := "step that takes a table" 111 name := "concept with <table1>" 112 conceptName := &gauge_messages.Step{Name: name} 113 tableName := TABLE + "1" 114 table := ` |id|name| 115 |--|----| 116 |1 |hello <foo> | 117 |2 |bar | 118 ` 119 concept, conceptText, _ := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}, 120 &gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}}, "# sdfdsf\n\n|foo|name|\n|hey|hello|\n\n ##helloasdasdasd\n\n* step", "") 121 122 c.Assert(concept, Equals, "# concept with <table1>\n* step that takes a table <table1>\n* step that takes a table <table1>\n") 123 c.Assert(conceptText, Equals, "* concept with "+` 124 125 |id|name | 126 |--|-----------| 127 |1 |hello <foo>| 128 |2 |bar | 129 `) 130 } 131 132 func (s *MySuite) TestExtractConceptWithSkippedTableAsArg(c *C) { 133 STEP := "step that takes a table" 134 name := "concept with <table1>" 135 conceptName := &gauge_messages.Step{Name: name} 136 tableName := TABLE + "1" 137 table := ` |id|name| 138 |--|----| 139 |1 |foo | 140 |2 |bar | 141 ` 142 concept, conceptText, _ := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}, 143 &gauge_messages.Step{Name: STEP, Table: table, ParamTableName: tableName}, &gauge_messages.Step{Name: STEP, Table: table}}, "# sdfdsf\nsome comment\n* some step\n## sce\n* step", "") 144 145 c.Assert(concept, Equals, "# concept with <table1>\n* step that takes a table <table1>\n* step that takes a table <table1>\n* step that takes a table"+` 146 147 |id|name| 148 |--|----| 149 |1 |foo | 150 |2 |bar | 151 `) 152 c.Assert(conceptText, Equals, "* concept with "+` 153 154 |id|name| 155 |--|----| 156 |1 |foo | 157 |2 |bar | 158 `) 159 } 160 161 func (s *MySuite) TestExtractConceptWithTableWithDynamicArgs(c *C) { 162 STEP := "step that takes a table" 163 name := "concept with" 164 conceptName := &gauge_messages.Step{Name: name} 165 table := `|id|name| 166 |--|----| 167 |1 |<foo>| 168 |2 |bar | 169 ` 170 concept, conceptText, _ := getExtractedConcept(conceptName, []*gauge_messages.Step{&gauge_messages.Step{Name: STEP, Table: table}}, 171 "# sdfdsf\n\n|foo|name|\n|hey|hello|\n\n##helloasdasdasd\n\n* step", "") 172 173 c.Assert(concept, Equals, "# concept with <foo>\n* step that takes a table"+` 174 175 |id|name | 176 |--|-----| 177 |1 |<foo>| 178 |2 |bar | 179 `) 180 c.Assert(conceptText, Equals, "* concept with <foo>") 181 } 182 183 func (s *MySuite) TestReplaceText(c *C) { 184 content := `Copyright 2015 ThoughtWorks, Inc. 185 186 This file is part of Gauge. 187 188 Gauge is free software: you can redistribute it and/or modify 189 it under the terms of the GNU General Public License as published by 190 the Free Software Foundation, either version 3 of the License, or 191 (at your option) any later version. 192 193 Gauge is distributed in the hope that it will be useful, 194 but WITHOUT ANY WARRANTY; without even the implied warranty of 195 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 196 GNU General Public License for more details. 197 198 You should have received a copy of the GNU General Public License 199 along with Gauge. If not, see <http://www.gnu.org/licenses/>.` 200 201 replacement := `* concept with 202 |id|name| 203 |--|----| 204 |1 |foo | 205 |2 |bar | 206 ` 207 five := int32(5) 208 ten := int32(10) 209 info := &gauge_messages.TextInfo{StartingLineNo: five, EndLineNo: ten} 210 finalText := replaceText(content, info, replacement) 211 212 c.Assert(finalText, Equals, `Copyright 2015 ThoughtWorks, Inc. 213 214 This file is part of Gauge. 215 216 * concept with 217 |id|name| 218 |--|----| 219 |1 |foo | 220 |2 |bar | 221 222 but WITHOUT ANY WARRANTY; without even the implied warranty of 223 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 224 GNU General Public License for more details. 225 226 You should have received a copy of the GNU General Public License 227 along with Gauge. If not, see <http://www.gnu.org/licenses/>.`) 228 } 229 230 func (s *MySuite) TestIsDuplicateConcept(c *C) { 231 concept := &gauge.Concept{ConceptStep: &gauge.Step{Value: "concept", IsConcept: true}, FileName: "sdfsdf.cpt"} 232 dictionary := gauge.NewConceptDictionary() 233 dictionary.ConceptsMap["sdfsdf.cpt"] = concept 234 235 isDuplicate := isDuplicateConcept(&gauge.Step{Value: "concept"}, dictionary) 236 237 c.Assert(isDuplicate, Equals, true) 238 } 239 240 func (s *MySuite) TestIsDuplicateConceptWithUniqueConcepts(c *C) { 241 concept := &gauge.Concept{ConceptStep: &gauge.Step{Value: "concept", IsConcept: true}, FileName: "sdfsdf.cpt"} 242 dictionary := gauge.NewConceptDictionary() 243 dictionary.ConceptsMap["sdfsdf.cpt"] = concept 244 245 isDuplicate := isDuplicateConcept(&gauge.Step{Value: "concept1"}, dictionary) 246 247 c.Assert(isDuplicate, Equals, false) 248 }