github.com/getgauge/gauge@v1.6.9/api/lang/stubImplementation_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 lang 8 9 import ( 10 "encoding/json" 11 "os" 12 "path/filepath" 13 "reflect" 14 "testing" 15 "time" 16 17 "github.com/getgauge/common" 18 gm "github.com/getgauge/gauge-proto/go/gauge_messages" 19 "github.com/getgauge/gauge/runner" 20 "github.com/getgauge/gauge/util" 21 "github.com/sourcegraph/go-langserver/pkg/lsp" 22 "github.com/sourcegraph/jsonrpc2" 23 ) 24 25 func TestGetImplementationFilesShouldReturnFilePaths(t *testing.T) { 26 27 var params = struct { 28 Concept bool 29 }{} 30 31 b, _ := json.Marshal(params) 32 p := json.RawMessage(b) 33 responses := map[gm.Message_MessageType]interface{}{} 34 responses[gm.Message_ImplementationFileListResponse] = &gm.ImplementationFileListResponse{ 35 ImplementationFilePaths: []string{"file"}, 36 } 37 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 38 implFiles, err := getImplFiles(&jsonrpc2.Request{Params: &p}) 39 40 if err != nil { 41 t.Fatalf("Got error %s", err.Error()) 42 } 43 44 want := []string{"file"} 45 46 if !reflect.DeepEqual(implFiles, want) { 47 t.Errorf("want: `%s`,\n got: `%s`", want, implFiles) 48 } 49 } 50 51 func TestGetImplementationFilesShouldReturnFilePathsIfParamIsNil(t *testing.T) { 52 responses := map[gm.Message_MessageType]interface{}{} 53 responses[gm.Message_ImplementationFileListResponse] = &gm.ImplementationFileListResponse{ 54 ImplementationFilePaths: []string{"file"}, 55 } 56 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 57 implFiles, err := getImplFiles(&jsonrpc2.Request{}) 58 59 if err != nil { 60 t.Fatalf("Got error %s", err.Error()) 61 } 62 63 want := []string{"file"} 64 65 if !reflect.DeepEqual(implFiles, want) { 66 t.Errorf("want: `%s`,\n got: `%s`", want, implFiles) 67 } 68 } 69 70 func TestGetImplementationFilesShouldReturnEmptyArrayForNoImplementationFiles(t *testing.T) { 71 var params = struct { 72 Concept bool 73 }{} 74 75 b, _ := json.Marshal(params) 76 p := json.RawMessage(b) 77 78 responses := map[gm.Message_MessageType]interface{}{} 79 responses[gm.Message_ImplementationFileListResponse] = &gm.ImplementationFileListResponse{ 80 ImplementationFilePaths: nil, 81 } 82 83 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 84 85 implFiles, err := getImplFiles(&jsonrpc2.Request{Params: &p}) 86 87 if err != nil { 88 t.Fatalf("Got error %s", err.Error()) 89 } 90 91 want := []string{} 92 93 if !reflect.DeepEqual(implFiles, want) { 94 t.Errorf("want: `%s`,\n got: `%s`", want, implFiles) 95 } 96 } 97 98 func TestGetImplementationFilesShouldReturnEmptyArrayForNoConceptFiles(t *testing.T) { 99 type cptParam struct { 100 Concept bool `json:"concept"` 101 } 102 103 params := cptParam{Concept: true} 104 105 b, _ := json.Marshal(params) 106 p := json.RawMessage(b) 107 108 util.GetConceptFiles = func() []string { 109 return nil 110 } 111 112 cptFiles, err := getImplFiles(&jsonrpc2.Request{Params: &p}) 113 114 if err != nil { 115 t.Fatalf("Got error %s", err.Error()) 116 } 117 118 want := []string{} 119 120 if !reflect.DeepEqual(cptFiles, want) { 121 t.Errorf("want: `%s`,\n got: `%s`", want, cptFiles) 122 } 123 } 124 125 func TestGetImplementationFilesShouldReturnFilePathsForConcept(t *testing.T) { 126 type implFileParam struct { 127 Concept bool `json:"concept"` 128 } 129 130 params := implFileParam{Concept: true} 131 132 b, _ := json.Marshal(params) 133 p := json.RawMessage(b) 134 135 util.GetConceptFiles = func() []string { 136 return []string{"file.cpt"} 137 } 138 139 implFiles, err := getImplFiles(&jsonrpc2.Request{Params: &p}) 140 141 if err != nil { 142 t.Fatalf("Got error %s", err.Error()) 143 } 144 145 want := []string{"file.cpt"} 146 147 if !reflect.DeepEqual(implFiles, want) { 148 t.Errorf("want: `%s`,\n got: `%s`", want, implFiles) 149 } 150 } 151 152 func TestPutStubImplementationShouldReturnFileDiff(t *testing.T) { 153 type stubImpl struct { 154 ImplementationFilePath string `json:"implementationFilePath"` 155 Codes []string `json:"codes"` 156 } 157 cwd, _ := os.Getwd() 158 dummyFilePath := filepath.Join(filepath.Join(cwd, "_testdata"), "dummyFile.txt") 159 stubImplParams := stubImpl{ImplementationFilePath: dummyFilePath, Codes: []string{"code"}} 160 161 b, _ := json.Marshal(stubImplParams) 162 p := json.RawMessage(b) 163 responses := map[gm.Message_MessageType]interface{}{} 164 responses[gm.Message_FileDiff] = &gm.FileDiff{ 165 FilePath: "file", 166 TextDiffs: []*gm.TextDiff{ 167 { 168 Span: &gm.Span{ 169 Start: 1, 170 StartChar: 2, 171 End: 3, 172 EndChar: 4, 173 }, 174 Content: "file content", 175 }, 176 }, 177 } 178 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 179 180 stubImplResponse, err := putStubImpl(&jsonrpc2.Request{Params: &p}) 181 182 if err != nil { 183 t.Fatalf("Got error %s", err.Error()) 184 } 185 186 var want lsp.WorkspaceEdit 187 want.Changes = make(map[string][]lsp.TextEdit) 188 uri := util.ConvertPathToURI("file") 189 textEdit := lsp.TextEdit{ 190 NewText: "file content", 191 Range: lsp.Range{ 192 Start: lsp.Position{Line: 1, Character: 2}, 193 End: lsp.Position{Line: 3, Character: 4}, 194 }, 195 } 196 want.Changes[string(uri)] = append(want.Changes[string(uri)], textEdit) 197 198 if !reflect.DeepEqual(stubImplResponse, want) { 199 t.Errorf("want: `%v`,\n got: `%v`", want, stubImplResponse) 200 } 201 } 202 203 func TestGenerateConceptShouldReturnFileDiff(t *testing.T) { 204 cwd, _ := os.Getwd() 205 testData := filepath.Join(cwd, "_testdata") 206 207 extractConcpetParam := concpetInfo{ 208 ConceptName: "# foo bar\n* ", 209 ConceptFile: "New File", 210 Dir: testData, 211 } 212 b, _ := json.Marshal(extractConcpetParam) 213 p := json.RawMessage(b) 214 215 response, err := generateConcept(&jsonrpc2.Request{Params: &p}) 216 217 if err != nil { 218 t.Fatalf("Got error %s", err.Error()) 219 } 220 221 var want lsp.WorkspaceEdit 222 want.Changes = make(map[string][]lsp.TextEdit) 223 uri := util.ConvertPathToURI(filepath.Join(testData, "concept1.cpt")) 224 textEdit := lsp.TextEdit{ 225 NewText: "# foo bar\n* ", 226 Range: lsp.Range{ 227 Start: lsp.Position{Line: 0, Character: 0}, 228 End: lsp.Position{Line: 0, Character: 0}, 229 }, 230 } 231 want.Changes[string(uri)] = append(want.Changes[string(uri)], textEdit) 232 233 if !reflect.DeepEqual(want, response) { 234 t.Errorf("want: `%v`,\n got: `%v`", want, response) 235 } 236 } 237 238 func TestGenerateConceptWithParam(t *testing.T) { 239 cwd, _ := os.Getwd() 240 testData := filepath.Join(cwd, "_testdata") 241 242 extractConcpetParam := concpetInfo{ 243 ConceptName: "# foo bar <some>\n* ", 244 ConceptFile: "New File", 245 Dir: testData, 246 } 247 b, _ := json.Marshal(extractConcpetParam) 248 p := json.RawMessage(b) 249 250 response, err := generateConcept(&jsonrpc2.Request{Params: &p}) 251 252 if err != nil { 253 t.Fatalf("Got error %s", err.Error()) 254 } 255 256 var want lsp.WorkspaceEdit 257 want.Changes = make(map[string][]lsp.TextEdit) 258 uri := util.ConvertPathToURI(filepath.Join(testData, "concept1.cpt")) 259 textEdit := lsp.TextEdit{ 260 NewText: "# foo bar <some>\n* ", 261 Range: lsp.Range{ 262 Start: lsp.Position{Line: 0, Character: 0}, 263 End: lsp.Position{Line: 0, Character: 0}, 264 }, 265 } 266 want.Changes[string(uri)] = append(want.Changes[string(uri)], textEdit) 267 268 if !reflect.DeepEqual(want, response) { 269 t.Errorf("want: `%v`,\n got: `%v`", want, response) 270 } 271 } 272 273 func TestGenerateConceptInExistingFile(t *testing.T) { 274 cwd, _ := os.Getwd() 275 testData := filepath.Join(cwd, "_testdata") 276 cptFile := filepath.Join(testData, "some.cpt") 277 278 extractConcpetParam := concpetInfo{ 279 ConceptName: "# foo bar <some>\n* ", 280 ConceptFile: cptFile, 281 Dir: testData, 282 } 283 b, _ := json.Marshal(extractConcpetParam) 284 p := json.RawMessage(b) 285 286 response, err := generateConcept(&jsonrpc2.Request{Params: &p}) 287 288 if err != nil { 289 t.Fatalf("Got error %s", err.Error()) 290 } 291 292 var want lsp.WorkspaceEdit 293 want.Changes = make(map[string][]lsp.TextEdit) 294 textEdit := lsp.TextEdit{ 295 NewText: "# concept heading\n* with a step\n\n# foo bar <some>\n* ", 296 Range: lsp.Range{ 297 Start: lsp.Position{Line: 0, Character: 0}, 298 End: lsp.Position{Line: 2, Character: 0}, 299 }, 300 } 301 uri := string(util.ConvertPathToURI(cptFile)) 302 303 want.Changes[uri] = append(want.Changes[uri], textEdit) 304 305 if !reflect.DeepEqual(want, response) { 306 t.Errorf("want: `%v`,\n got: `%v`", want, response) 307 } 308 } 309 310 func TestGenerateConceptInNewFileWhenDefaultExisits(t *testing.T) { 311 cwd, _ := os.Getwd() 312 testData := filepath.Join(cwd, "_testdata") 313 314 cptFile := filepath.Join(testData, "concept1.cpt") 315 err := os.WriteFile(cptFile, []byte(""), common.NewFilePermissions) 316 if err != nil { 317 t.Fatalf("Unable to create Concept %s: %s", cptFile, err.Error()) 318 } 319 320 defer func() { 321 err := common.Remove(cptFile) 322 if err != nil { 323 t.Fatalf("Unable to delete Concept %s: %s", cptFile, err.Error()) 324 } 325 }() 326 327 extractConcpetParam := concpetInfo{ 328 ConceptName: "# foo bar <some>\n* ", 329 ConceptFile: "New File", 330 Dir: testData, 331 } 332 b, _ := json.Marshal(extractConcpetParam) 333 p := json.RawMessage(b) 334 335 response, err := generateConcept(&jsonrpc2.Request{Params: &p}) 336 337 if err != nil { 338 t.Fatalf("Got error %s", err.Error()) 339 } 340 341 uri := util.ConvertPathToURI(filepath.Join(testData, "concept2.cpt")) 342 343 var want lsp.WorkspaceEdit 344 want.Changes = make(map[string][]lsp.TextEdit) 345 346 textEdit := lsp.TextEdit{ 347 NewText: "# foo bar <some>\n* ", 348 Range: lsp.Range{ 349 Start: lsp.Position{Line: 0, Character: 0}, 350 End: lsp.Position{Line: 0, Character: 0}, 351 }, 352 } 353 want.Changes[string(uri)] = append(want.Changes[string(uri)], textEdit) 354 355 if !reflect.DeepEqual(want, response) { 356 t.Errorf("want: `%v`,\n got: `%v`", want, response) 357 } 358 }