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