github.com/getgauge/gauge@v1.6.9/api/lang/references_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 "time" 14 15 "github.com/getgauge/gauge-proto/go/gauge_messages" 16 "github.com/getgauge/gauge/runner" 17 "github.com/getgauge/gauge/util" 18 "github.com/sourcegraph/go-langserver/pkg/lsp" 19 "github.com/sourcegraph/jsonrpc2" 20 ) 21 22 func TestStepReferences(t *testing.T) { 23 provider = &dummyInfoProvider{} 24 specText := `Specification Heading 25 ===================== 26 27 Scenario Heading 28 ---------------- 29 30 * Say <hello> to <gauge>` 31 32 uri := util.ConvertPathToURI("foo.spec") 33 openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)} 34 openFilesCache.add(uri, specText) 35 36 b, _ := json.Marshal([]string{"Say {} to {}"}) 37 params := json.RawMessage(b) 38 want := []lsp.Location{ 39 {URI: uri, Range: lsp.Range{ 40 Start: lsp.Position{Line: 6, Character: 0}, 41 End: lsp.Position{Line: 6, Character: 24}, 42 }}, 43 } 44 got, err := stepReferences(&jsonrpc2.Request{Params: ¶ms}) 45 if err != nil { 46 t.Fatalf("Got error %s", err.Error()) 47 } 48 if !reflect.DeepEqual(got, want) { 49 t.Errorf("get step references failed, want: `%v`, got: `%v`", want, got) 50 } 51 } 52 53 func TestStepValueAtShouldGive(t *testing.T) { 54 provider = &dummyInfoProvider{} 55 params := lsp.TextDocumentPositionParams{ 56 TextDocument: lsp.TextDocumentIdentifier{URI: "step_impl.js"}, 57 Position: lsp.Position{ 58 Line: 3, 59 Character: 3, 60 }, 61 } 62 63 b, _ := json.Marshal(params) 64 p := json.RawMessage(b) 65 66 responses := map[gauge_messages.Message_MessageType]interface{}{} 67 responses[gauge_messages.Message_StepPositionsResponse] = &gauge_messages.StepPositionsResponse{ 68 StepPositions: []*gauge_messages.StepPositionsResponse_StepPosition{ 69 { 70 Span: &gauge_messages.Span{Start: 2, End: 4}, 71 StepValue: "Step value at line {} and character {}", 72 }, 73 }, 74 } 75 lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30} 76 77 stepValue, err := stepValueAt(&jsonrpc2.Request{Params: &p}) 78 79 if err != nil { 80 t.Fatalf("Got error %s", err.Error()) 81 } 82 83 want := "Step value at line {} and character {}" 84 85 if !reflect.DeepEqual(stepValue, want) { 86 t.Errorf("want: `%s`,\n got: `%s`", want, stepValue) 87 } 88 }