github.com/getgauge/gauge@v1.6.9/api/lang/codeAction_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 "reflect" 12 "testing" 13 14 "github.com/sourcegraph/go-langserver/pkg/lsp" 15 "github.com/sourcegraph/jsonrpc2" 16 ) 17 18 func TestGetCodeActionForUnimplementedStep(t *testing.T) { 19 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 20 openFilesCache.add(lsp.DocumentURI("foo.spec"), "# spec heading\n## scenario heading\n* foo bar") 21 22 stub := "a stub for unimplemented step" 23 d := []lsp.Diagnostic{ 24 { 25 Range: lsp.Range{ 26 Start: lsp.Position{Line: 2, Character: 0}, 27 End: lsp.Position{Line: 2, Character: 9}, 28 }, 29 Message: "Step implantation not found", 30 Severity: 1, 31 Code: stub, 32 }, 33 } 34 codeActionParams := lsp.CodeActionParams{ 35 TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, 36 Context: lsp.CodeActionContext{Diagnostics: d}, 37 Range: lsp.Range{ 38 Start: lsp.Position{Line: 2, Character: 0}, 39 End: lsp.Position{Line: 2, Character: 9}, 40 }, 41 } 42 b, _ := json.Marshal(codeActionParams) 43 p := json.RawMessage(b) 44 45 want := []lsp.Command{ 46 { 47 Command: generateStepCommand, 48 Title: generateStubTitle, 49 Arguments: []interface{}{stub}, 50 }, 51 { 52 Command: generateConceptCommand, 53 Title: generateConceptTitle, 54 Arguments: []interface{}{concpetInfo{ConceptName: "# foo bar\n* "}}, 55 }, 56 } 57 58 got, err := codeActions(&jsonrpc2.Request{Params: &p}) 59 60 if err != nil { 61 t.Errorf("expected error to be nil. \nGot : %s", err) 62 } 63 64 if !reflect.DeepEqual(got, want) { 65 t.Errorf("want: `%s`,\n got: `%s`", want, got) 66 } 67 } 68 69 func TestGetCodeActionForUnimplementedStepWithParam(t *testing.T) { 70 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 71 openFilesCache.add(lsp.DocumentURI("foo.spec"), "# spec heading\n## scenario heading\n* foo bar \"some\"") 72 73 stub := "a stub for unimplemented step" 74 d := []lsp.Diagnostic{ 75 { 76 Range: lsp.Range{ 77 Start: lsp.Position{Line: 2, Character: 0}, 78 End: lsp.Position{Line: 2, Character: 9}, 79 }, 80 Message: "Step implantation not found", 81 Severity: 1, 82 Code: stub, 83 }, 84 } 85 codeActionParams := lsp.CodeActionParams{ 86 TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, 87 Context: lsp.CodeActionContext{Diagnostics: d}, 88 Range: lsp.Range{ 89 Start: lsp.Position{Line: 2, Character: 0}, 90 End: lsp.Position{Line: 2, Character: 9}, 91 }, 92 } 93 b, _ := json.Marshal(codeActionParams) 94 p := json.RawMessage(b) 95 96 want := []lsp.Command{ 97 { 98 Command: generateStepCommand, 99 Title: generateStubTitle, 100 Arguments: []interface{}{stub}, 101 }, 102 { 103 Command: generateConceptCommand, 104 Title: generateConceptTitle, 105 Arguments: []interface{}{concpetInfo{ConceptName: "# foo bar <arg0>\n* "}}, 106 }, 107 } 108 109 got, err := codeActions(&jsonrpc2.Request{Params: &p}) 110 111 if err != nil { 112 t.Errorf("expected error to be nil. \nGot : %s", err) 113 } 114 115 if !reflect.DeepEqual(got, want) { 116 t.Errorf("want: `%s`,\n got: `%s`", want, got) 117 } 118 } 119 120 func TestGetCodeActionForUnimplementedStepWithTableParam(t *testing.T) { 121 specText := `#Specification Heading 122 123 ##Scenario Heading 124 125 * Step text 126 |Head| 127 |----| 128 |some|` 129 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 130 openFilesCache.add(lsp.DocumentURI("foo.spec"), specText) 131 132 stub := "a stub for unimplemented step" 133 d := []lsp.Diagnostic{ 134 { 135 Range: lsp.Range{ 136 Start: lsp.Position{Line: 4, Character: 0}, 137 End: lsp.Position{Line: 4, Character: 12}, 138 }, 139 Message: "Step implantation not found", 140 Severity: 1, 141 Code: stub, 142 }, 143 } 144 codeActionParams := lsp.CodeActionParams{ 145 TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, 146 Context: lsp.CodeActionContext{Diagnostics: d}, 147 Range: lsp.Range{ 148 Start: lsp.Position{Line: 4, Character: 0}, 149 End: lsp.Position{Line: 4, Character: 12}, 150 }, 151 } 152 b, _ := json.Marshal(codeActionParams) 153 p := json.RawMessage(b) 154 155 want := []lsp.Command{ 156 { 157 Command: generateStepCommand, 158 Title: generateStubTitle, 159 Arguments: []interface{}{stub}, 160 }, 161 { 162 Command: generateConceptCommand, 163 Title: generateConceptTitle, 164 Arguments: []interface{}{concpetInfo{ConceptName: "# Step text <arg0>\n* "}}, 165 }, 166 } 167 168 got, err := codeActions(&jsonrpc2.Request{Params: &p}) 169 170 if err != nil { 171 t.Errorf("expected error to be nil. \nGot : %s", err) 172 } 173 174 if !reflect.DeepEqual(got, want) { 175 t.Errorf("want: `%s`,\n got: `%s`", want, got) 176 } 177 } 178 179 func TestGetCodeActionForUnimplementedStepWithFileParameter(t *testing.T) { 180 specText := `#Specification Heading 181 182 ##Scenario Heading 183 184 * Step text <file:_testdata/dummyFile.txt>` 185 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 186 openFilesCache.add(lsp.DocumentURI("foo.spec"), specText) 187 188 stub := "a stub for unimplemented step" 189 d := []lsp.Diagnostic{ 190 { 191 Range: lsp.Range{ 192 Start: lsp.Position{Line: 4, Character: 0}, 193 End: lsp.Position{Line: 4, Character: 12}, 194 }, 195 Message: "Step implantation not found", 196 Severity: 1, 197 Code: stub, 198 }, 199 } 200 codeActionParams := lsp.CodeActionParams{ 201 TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, 202 Context: lsp.CodeActionContext{Diagnostics: d}, 203 Range: lsp.Range{ 204 Start: lsp.Position{Line: 4, Character: 0}, 205 End: lsp.Position{Line: 4, Character: 12}, 206 }, 207 } 208 b, _ := json.Marshal(codeActionParams) 209 p := json.RawMessage(b) 210 211 want := []lsp.Command{ 212 { 213 Command: generateStepCommand, 214 Title: generateStubTitle, 215 Arguments: []interface{}{stub}, 216 }, 217 { 218 Command: generateConceptCommand, 219 Title: generateConceptTitle, 220 Arguments: []interface{}{concpetInfo{ConceptName: "# Step text <arg0>\n* "}}, 221 }, 222 } 223 224 got, err := codeActions(&jsonrpc2.Request{Params: &p}) 225 226 if err != nil { 227 t.Errorf("expected error to be nil. \nGot : %s", err) 228 } 229 230 if !reflect.DeepEqual(got, want) { 231 t.Errorf("want: `%s`,\n got: `%s`", want, got) 232 } 233 } 234 235 func TestNotToPanicForUnimplementedWithInvalidStartLine(t *testing.T) { 236 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 237 openFilesCache.add(lsp.DocumentURI("foo.spec"), "# spec heading\n## scenario heading\n* foo bar") 238 239 stub := "a stub for unimplemented step" 240 d := []lsp.Diagnostic{ 241 { 242 Range: lsp.Range{ 243 Start: lsp.Position{Line: 0, Character: 0}, 244 End: lsp.Position{Line: 0, Character: 0}, 245 }, 246 Message: "Step implantation not found", 247 Severity: 1, 248 Code: stub, 249 }, 250 } 251 codeActionParams := lsp.CodeActionParams{ 252 TextDocument: lsp.TextDocumentIdentifier{URI: "foo.spec"}, 253 Context: lsp.CodeActionContext{Diagnostics: d}, 254 Range: lsp.Range{ 255 Start: lsp.Position{Line: 0, Character: 0}, 256 End: lsp.Position{Line: 0, Character: 0}, 257 }, 258 } 259 b, _ := json.Marshal(codeActionParams) 260 p := json.RawMessage(b) 261 262 want := []lsp.Command{ 263 { 264 Command: generateStepCommand, 265 Title: generateStubTitle, 266 Arguments: []interface{}{stub}, 267 }, 268 } 269 270 got, err := codeActions(&jsonrpc2.Request{Params: &p}) 271 272 if err != nil { 273 t.Errorf("expected error to be nil. \nGot : %s", err) 274 } 275 276 if !reflect.DeepEqual(got, want) { 277 t.Errorf("want: `%s`,\n got: `%s`", want, got) 278 } 279 }