Test internal/grammar, and allow the root to import it

This commit is contained in:
2026-10-05 12:06:43 +02:00
committed by Lillem4n
parent 46895151c8
commit 41292f9d89
9 changed files with 170 additions and 33 deletions
+40
View File
@@ -0,0 +1,40 @@
package grammar
import (
"reflect"
"testing"
)
func TestSplitPathKeepsSelectorsAndStepsUpWhole(t *testing.T) {
for path, want := range map[string][]string{
"a": {"a"},
"a.b": {"a", "b"},
"misc.territory[SE].name": {"misc", "territory", "[SE]", "name"},
"x[a.b]": {"x", "[a.b]"},
"city[Oslo]..country": {"city", "[Oslo]", "..", "country"},
"a..b": {"a", "..", "b"},
} {
segs, err := SplitPath(path)
if err != nil || !reflect.DeepEqual(segs, want) {
t.Errorf("SplitPath(%q) = %q, %v, want %q", path, segs, err, want)
continue
}
if got := JoinSegments(segs); got != path {
t.Errorf("JoinSegments(%q) = %q, want %q", segs, got, path)
}
}
}
func TestSplitPathRefusesAStraySelectorBracket(t *testing.T) {
for _, path := range []string{"[a]", "a.[b]", "a[b", "a]", "a[]", "a[b]c", "a[b[c]]"} {
if _, err := SplitPath(path); err == nil {
t.Errorf("SplitPath(%q) = nil error, want one", path)
}
}
}
func TestNameSegmentsDropSelectorsAndStepsUp(t *testing.T) {
if got := NameSegments([]string{"a", "[b]", "..", "c"}); !reflect.DeepEqual(got, []string{"a", "c"}) {
t.Errorf("NameSegments = %q, want [a c]", got)
}
}