chore(aliases): Add more tests

This commit is contained in:
Myzel394 2024-09-07 18:10:36 +02:00
parent d56eef43bf
commit 40a570809d
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
4 changed files with 58 additions and 4 deletions

View File

@ -12,7 +12,7 @@ import (
func GetDefinitionLocationForValue( func GetDefinitionLocationForValue(
i indexes.AliasesIndexes, i indexes.AliasesIndexes,
value ast.AliasValueInterface, value ast.AliasValueInterface,
params *protocol.DefinitionParams, documentURI string,
) []protocol.Location { ) []protocol.Location {
switch value.(type) { switch value.(type) {
case ast.AliasValueUser: case ast.AliasValueUser:
@ -22,7 +22,7 @@ func GetDefinitionLocationForValue(
if entry, found := i.Keys[indexes.NormalizeKey(userValue.Value)]; found { if entry, found := i.Keys[indexes.NormalizeKey(userValue.Value)]; found {
return []protocol.Location{ return []protocol.Location{
{ {
URI: params.TextDocument.URI, URI: documentURI,
Range: entry.Key.Location.ToLSPRange(), Range: entry.Key.Location.ToLSPRange(),
}, },
} }

View File

@ -0,0 +1,54 @@
package handlers
import (
"config-lsp/handlers/aliases/ast"
"config-lsp/handlers/aliases/indexes"
"config-lsp/utils"
"testing"
)
func TestGoToDefinitionSimpleExample(
t *testing.T,
) {
input := utils.Dedent(`
alice: root
bob: root
steve: alice@example.com, bob
david: alice
`)
parser := ast.NewAliasesParser()
errors := parser.Parse(input)
if len(errors) > 0 {
t.Fatalf("Unexpected errors: %v", errors)
}
i, errors := indexes.CreateIndexes(parser)
if len(errors) > 0 {
t.Fatalf("Expected no errors, but got: %v", errors)
}
rawEntry, _ := parser.Aliases.Get(uint32(3))
entry := rawEntry.(*ast.AliasEntry)
rawValue := entry.Values.Values[0]
value := rawValue.(ast.AliasValueUser)
locations := GetDefinitionLocationForValue(
i,
value,
"file:///etc/aliases",
)
if !(len(locations) == 1) {
t.Errorf("Expected 1 location, but got %v", len(locations))
}
if !(locations[0].URI == "file:///etc/aliases") {
t.Errorf("Unexpected location: %v", locations[0])
}
if !(locations[0].Range.Start.Line == 0 && locations[0].Range.Start.Character == 0 && locations[0].Range.End.Line == 0 && locations[0].Range.End.Character == 5) {
t.Errorf("Unexpected location: %v", locations[0])
}
}

View File

@ -7,7 +7,7 @@ import (
"testing" "testing"
) )
func TestComplexExample( func TestRenameSimpleExample(
t *testing.T, t *testing.T,
) { ) {
input := utils.Dedent(` input := utils.Dedent(`

View File

@ -32,7 +32,7 @@ func TextDocumentDefinition(context *glsp.Context, params *protocol.DefinitionPa
return handlers.GetDefinitionLocationForValue( return handlers.GetDefinitionLocationForValue(
*d.Indexes, *d.Indexes,
*rawValue, *rawValue,
params, params.TextDocument.URI,
), nil ), nil
} }