github.com/getgauge/gauge@v1.6.9/api/lang/definition_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  	"testing"
    12  	"time"
    13  
    14  	gm "github.com/getgauge/gauge-proto/go/gauge_messages"
    15  	"github.com/getgauge/gauge/runner"
    16  
    17  	"github.com/getgauge/gauge/util"
    18  	"github.com/sourcegraph/go-langserver/pkg/lsp"
    19  	"github.com/sourcegraph/jsonrpc2"
    20  )
    21  
    22  func TestConceptDefinitionInSpecFile(t *testing.T) {
    23  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
    24  	uri := lsp.DocumentURI(util.ConvertPathToURI("uri.spec"))
    25  	openFilesCache.add(uri, "# Specification \n## Scenario \n * concept1")
    26  
    27  	conUri := lsp.DocumentURI(util.ConvertPathToURI("concept_uri.cpt"))
    28  	openFilesCache.add(conUri, "# Concept \n* a step \n \n # Another Concept \n*concept1")
    29  
    30  	provider = &dummyInfoProvider{}
    31  	position := lsp.Position{Line: 2, Character: len(" * conce")}
    32  	b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}, Position: position})
    33  	p := json.RawMessage(b)
    34  
    35  	got, err := definition(&jsonrpc2.Request{Params: &p})
    36  	if err != nil {
    37  		t.Errorf("Failed to find definition, err: `%v`", err)
    38  	}
    39  
    40  	want := lsp.Location{URI: conUri, Range: lsp.Range{Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 0, Character: 10}}}
    41  	if got != want {
    42  		t.Errorf("Wrong definition found, got: `%v`, want: `%v`", got, want)
    43  	}
    44  }
    45  
    46  func TestConceptDefinitionInConceptFile(t *testing.T) {
    47  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
    48  	uri := lsp.DocumentURI(util.ConvertPathToURI("concept_uri.cpt"))
    49  	openFilesCache.add(uri, "# Concept \n* a step \n \n # Another Concept \n*concept1")
    50  	provider = &dummyInfoProvider{}
    51  	position := lsp.Position{Line: 4, Character: len("*conce")}
    52  	b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}, Position: position})
    53  	p := json.RawMessage(b)
    54  
    55  	got, err := definition(&jsonrpc2.Request{Params: &p})
    56  	if err != nil {
    57  		t.Errorf("Failed to find definition, err: `%v`", err)
    58  	}
    59  	want := lsp.Location{URI: uri, Range: lsp.Range{Start: lsp.Position{Line: 0, Character: 0}, End: lsp.Position{Line: 0, Character: 10}}}
    60  	if got != want {
    61  		t.Errorf("Wrong definition found, got: `%v`, want: `%v`", got, want)
    62  	}
    63  }
    64  
    65  func TestExternalStepDefinition(t *testing.T) {
    66  	openFilesCache = &files{cache: make(map[lsp.DocumentURI][]string)}
    67  	uri := lsp.DocumentURI(util.ConvertPathToURI("spec_uri.spec"))
    68  	openFilesCache.add(uri, "# Specification \n\n## Scenario\n\n* a step")
    69  	provider = &dummyInfoProvider{}
    70  	position := lsp.Position{Line: 4, Character: len("* a step")}
    71  	b, _ := json.Marshal(lsp.TextDocumentPositionParams{TextDocument: lsp.TextDocumentIdentifier{URI: uri}, Position: position})
    72  	p := json.RawMessage(b)
    73  	responses := map[gm.Message_MessageType]interface{}{}
    74  	responses[gm.Message_StepNameResponse] = &gm.StepNameResponse{
    75  		HasAlias:      false,
    76  		IsExternal:    true,
    77  		IsStepPresent: true,
    78  	}
    79  
    80  	lRunner.runner = &runner.GrpcRunner{LegacyClient: &mockClient{responses: responses}, Timeout: time.Second * 30}
    81  	_, err := definition(&jsonrpc2.Request{Params: &p})
    82  	if err == nil {
    83  		t.Errorf("expected error to not be nil.")
    84  	}
    85  	expected := `implementation source not found: Step implementation referred from an external project or library`
    86  	if err.Error() != expected {
    87  		t.Errorf("Expected: `%s`\nGot: `%s`", expected, err.Error())
    88  	}
    89  }