mirror of
https://github.com/Myzel394/config-lsp.git
synced 2025-06-18 23:15:26 +02:00
144 lines
3.9 KiB
Go
144 lines
3.9 KiB
Go
package tree
|
|
|
|
import (
|
|
"config-lsp/utils"
|
|
"testing"
|
|
)
|
|
|
|
func TestValidSimpleExampleWorks(
|
|
t *testing.T,
|
|
) {
|
|
input := utils.Dedent(`
|
|
1.2.3.4 hello.com
|
|
`)
|
|
|
|
parser := CreateNewHostsParser()
|
|
errors := parser.Parse(input)
|
|
|
|
if len(errors) != 0 {
|
|
t.Errorf("Expected no errors, but got %v", errors)
|
|
}
|
|
|
|
if !(len(parser.Tree.Entries) == 1) {
|
|
t.Errorf("Expected 1 entry, but got %v", len(parser.Tree.Entries))
|
|
}
|
|
|
|
if parser.Tree.Entries[1].IPAddress == nil {
|
|
t.Errorf("Expected IP address to be present, but got nil")
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].IPAddress.Value == "1.2.3.4") {
|
|
t.Errorf("Expected IP address to be 1.2.3.4, but got %v", parser.Tree.Entries[1].IPAddress.Value)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Hostname.Value == "hello.com") {
|
|
t.Errorf("Expected hostname to be hello.com, but got %v", parser.Tree.Entries[1].Hostname.Value)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Aliases == nil) {
|
|
t.Errorf("Expected no aliases, but got %v", parser.Tree.Entries[1].Aliases)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Location.Start.Line == 1) {
|
|
t.Errorf("Expected line to be 1, but got %v", parser.Tree.Entries[1].Location.Start.Line)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Location.Start.Character == 0) {
|
|
t.Errorf("Expected start to be 0, but got %v", parser.Tree.Entries[1].Location.Start)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Location.End.Character == 16) {
|
|
t.Errorf("Expected end to be 16, but got %v", parser.Tree.Entries[1].Location.End.Character)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].IPAddress.Location.Start.Line == 1) {
|
|
t.Errorf("Expected IP address line to be 1, but got %v", parser.Tree.Entries[1].IPAddress.Location.Start.Line)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].IPAddress.Location.Start.Character == 0) {
|
|
t.Errorf("Expected IP address start to be 0, but got %v", parser.Tree.Entries[1].IPAddress.Location.Start.Character)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].IPAddress.Location.End.Character == 6) {
|
|
t.Errorf("Expected IP address end to be 6, but got %v", parser.Tree.Entries[1].IPAddress.Location.End.Character)
|
|
}
|
|
|
|
if !(len(parser.CommentLines) == 0) {
|
|
t.Errorf("Expected no comment lines, but got %v", len(parser.CommentLines))
|
|
}
|
|
}
|
|
|
|
func TestValidComplexExampleWorks(
|
|
t *testing.T,
|
|
) {
|
|
input := utils.Dedent(`
|
|
|
|
# This is a comment
|
|
1.2.3.4 hello.com test.com # This is another comment
|
|
5.5.5.5 test.com
|
|
1.2.3.4 example.com check.com
|
|
`)
|
|
|
|
parser := CreateNewHostsParser()
|
|
errors := parser.Parse(input)
|
|
|
|
if len(errors) != 0 {
|
|
t.Errorf("Expected no errors, but got %v", errors)
|
|
}
|
|
|
|
if !(len(parser.Tree.Entries) == 3) {
|
|
t.Errorf("Expected 3 entries, but got %v", len(parser.Tree.Entries))
|
|
}
|
|
|
|
if parser.Tree.Entries[3].IPAddress == nil {
|
|
t.Errorf("Expected IP address to be present, but got nil")
|
|
}
|
|
|
|
if !(parser.Tree.Entries[3].IPAddress.Value == "1.2.3.4") {
|
|
t.Errorf("Expected IP address to be 1.2.3.4, but got %v", parser.Tree.Entries[2].IPAddress.Value)
|
|
}
|
|
|
|
if !(len(parser.CommentLines) == 1) {
|
|
t.Errorf("Expected 1 comment line, but got %v", len(parser.CommentLines))
|
|
}
|
|
|
|
if !(utils.KeyExists(parser.CommentLines, 2)) {
|
|
t.Errorf("Expected comment line 2 to exist, but it does not")
|
|
}
|
|
}
|
|
|
|
func TestInvalidExampleWorks(
|
|
t *testing.T,
|
|
) {
|
|
input := utils.Dedent(`
|
|
1.2.3.4
|
|
`)
|
|
|
|
parser := CreateNewHostsParser()
|
|
errors := parser.Parse(input)
|
|
|
|
if len(errors) == 0 {
|
|
t.Errorf("Expected errors, but got none")
|
|
}
|
|
|
|
if !(len(parser.Tree.Entries) == 1) {
|
|
t.Errorf("Expected 1 entries, but got %v", len(parser.Tree.Entries))
|
|
}
|
|
|
|
if !(len(parser.CommentLines) == 0) {
|
|
t.Errorf("Expected no comment lines, but got %v", len(parser.CommentLines))
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].IPAddress.Value == "1.2.3.4") {
|
|
t.Errorf("Expected IP address to be nil, but got %v", parser.Tree.Entries[1].IPAddress)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Hostname == nil) {
|
|
t.Errorf("Expected hostname to be nil, but got %v", parser.Tree.Entries[1].Hostname)
|
|
}
|
|
|
|
if !(parser.Tree.Entries[1].Aliases == nil) {
|
|
t.Errorf("Expected aliases to be nil, but got %v", parser.Tree.Entries[1].Aliases)
|
|
}
|
|
}
|