2026-11-14 10:36:07 +01:00
|
|
|
package labels
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestParseAndSelect(t *testing.T) {
|
|
|
|
|
f, err := Parse([]byte(`{"version":1,"labels":{"sales":["crm","quote"],"finance":["model","crm"]}}`))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
cases := []struct {
|
|
|
|
|
item string
|
|
|
|
|
client []string
|
|
|
|
|
want bool
|
|
|
|
|
}{
|
|
|
|
|
{"unlabeled", nil, true},
|
|
|
|
|
{"unlabeled", []string{}, true},
|
|
|
|
|
{"crm", nil, false},
|
|
|
|
|
{"crm", []string{"sales"}, true},
|
|
|
|
|
{"crm", []string{"finance"}, true},
|
|
|
|
|
{"quote", []string{"finance"}, false},
|
|
|
|
|
{"quote", []string{"finance", "sales"}, true},
|
|
|
|
|
{"model", []string{"other"}, false},
|
|
|
|
|
}
|
|
|
|
|
for _, c := range cases {
|
|
|
|
|
if got := f.Selected(c.item, c.client); got != c.want {
|
|
|
|
|
t.Errorf("Selected(%q, %v) = %v, want %v", c.item, c.client, got, c.want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if got := strings.Join(f.Names(), ","); got != "crm,model,quote" {
|
|
|
|
|
t.Errorf("Names() = %s", got)
|
|
|
|
|
}
|
|
|
|
|
if got := strings.Join(f.LabelsOf("crm"), ","); got != "finance,sales" {
|
|
|
|
|
t.Errorf("LabelsOf(crm) = %s", got)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestNilFileRestrictsNothing(t *testing.T) {
|
|
|
|
|
var f *File
|
|
|
|
|
if !f.Selected("anything", nil) {
|
|
|
|
|
t.Error("a nil file must select every item")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestEmptyLabelsMapIsValid(t *testing.T) {
|
|
|
|
|
f, err := Parse([]byte(`{"version":1,"labels":{}}`))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !f.Selected("x", nil) {
|
|
|
|
|
t.Error("no labels declared means no restrictions")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestParseRejects(t *testing.T) {
|
|
|
|
|
cases := map[string]string{
|
|
|
|
|
"not json": `{`,
|
|
|
|
|
"unknown key": `{"version":1,"labels":{},"extra":true}`,
|
|
|
|
|
"wrong version": `{"version":2,"labels":{}}`,
|
|
|
|
|
"missing version": `{"labels":{}}`,
|
|
|
|
|
"missing labels": `{"version":1}`,
|
|
|
|
|
"upper label": `{"version":1,"labels":{"Sales":["a"]}}`,
|
|
|
|
|
"space label": `{"version":1,"labels":{"sales team":["a"]}}`,
|
|
|
|
|
"leading dash": `{"version":1,"labels":{"-sales":["a"]}}`,
|
|
|
|
|
"empty list": `{"version":1,"labels":{"sales":[]}}`,
|
|
|
|
|
"empty name": `{"version":1,"labels":{"sales":[""]}}`,
|
|
|
|
|
"trailing data": `{"version":1,"labels":{}} {}`,
|
2026-11-14 11:00:41 +01:00
|
|
|
"trailing brace": `{"version":1,"labels":{}}}`,
|
2026-11-14 10:36:07 +01:00
|
|
|
}
|
|
|
|
|
for name, data := range cases {
|
|
|
|
|
if _, err := Parse([]byte(data)); err == nil {
|
|
|
|
|
t.Errorf("%s: expected an error", name)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|