package abono import ( "reflect" "strings" "testing" ) func TestDecodeCatalog(t *testing.T) { got, err := decodeCatalog([]byte("# a comment\n\nabono.a = \"One \\\"two\\\" {x}\" # trailing\nabono.b = 'lit\\eral'\r\nabono.c-d_e = \"é\"\n")) if err != nil { t.Fatal(err) } want := map[string]string{"abono.a": `One "two" {x}`, "abono.b": `lit\eral`, "abono.c-d_e": "é"} if !reflect.DeepEqual(map[string]string(got), want) { t.Errorf("got %v, want %v", got, want) } } func TestDecodeCatalogRefuses(t *testing.T) { for name, in := range map[string]string{ "table": "[abono]\n", "no equals": "abono.a\n", "bare value": "abono.a = 3\n", "unterminated": "abono.a = \"x\n", "trailing": "abono.a = \"x\" y\n", "duplicate": "abono.a = \"x\"\nabono.a = \"y\"\n", "empty key": " = \"x\"\n", "bad escape": "abono.a = \"\\q\"\n", } { if _, err := decodeCatalog([]byte(in)); err == nil { t.Errorf("%s: accepted %q", name, in) } } } // The shipped file decodes, and Catalog hands out a copy. func TestCatalogIsACopyOfTheShippedFile(t *testing.T) { c := Catalog() for k := range c { if !strings.HasPrefix(k, "abono.") { t.Errorf("key %q is not in abono's namespace", k) } } c["abono.test.sentinel"] = "x" if _, leaked := Catalog()["abono.test.sentinel"]; leaked { t.Error("Catalog shares its table with the caller") } }