Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder committed Feb 5, 2024
1 parent 97a55c3 commit a1613be
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
43 changes: 42 additions & 1 deletion i18n/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ simple: simple translation
# Comment
detail:
description: detail description
description: detail description
other: detail translation
# Comment
Expand All @@ -150,6 +150,47 @@ everything:
expectMessage(t, bundle, language.AmericanEnglish, "everything", everythingMessage)
}

func TestInvalidYAML(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("yaml", yaml.Unmarshal)
_, err := bundle.ParseMessageFileBytes([]byte(`
# Comment
simple: simple translation
# Comment
detail:
description: detail description
other: detail translation
# Comment
everything:
description: everything description
zero: zero translation
one: one translation
two: two translation
few: few translation
many: many translation
other: other translation
garbage: something
description: translation
`), "en-US.yaml")

expectedErr := &mixedKeysError{
reservedKeys: []string{"description"},
unreservedKeys: []string{"detail", "everything", "simple"},
}
if err == nil {
t.Fatalf("expected error %#v; got nil", expectedErr)
}
if err.Error() != expectedErr.Error() {
t.Fatalf("expected error %q; got %q", expectedErr, err)
}
if c := len(bundle.messageTemplates); c > 0 {
t.Fatalf("expected no message templates in bundle; got %d", c)
}
}

func TestTOML(t *testing.T) {
bundle := NewBundle(language.English)
bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
Expand Down
5 changes: 3 additions & 2 deletions i18n/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package i18n

import (
"fmt"
"slices"
"strings"
)

Expand Down Expand Up @@ -233,7 +234,6 @@ func isMessage(v interface{}) (bool, error) {
unreservedKeys = append(unreservedKeys, k)
}
}
// return false, fmt.Errorf("reserved keys %v mixed with unreserved keys %v", reservedKeys, unreservedKeys)
return false, &mixedKeysError{
reservedKeys: reservedKeys,
unreservedKeys: unreservedKeys,
Expand Down Expand Up @@ -264,7 +264,6 @@ func isMessage(v interface{}) (bool, error) {
unreservedKeys = append(unreservedKeys, k)
}
}
// return false, fmt.Errorf("reserved keys %v mixed with unreserved keys %v", reservedKeys, unreservedKeys)
return false, &mixedKeysError{
reservedKeys: reservedKeys,
unreservedKeys: unreservedKeys,
Expand All @@ -281,5 +280,7 @@ type mixedKeysError struct {
}

func (e *mixedKeysError) Error() string {
slices.Sort(e.reservedKeys)
slices.Sort(e.unreservedKeys)
return fmt.Sprintf("reserved keys %v mixed with unreserved keys %v", e.reservedKeys, e.unreservedKeys)
}

0 comments on commit a1613be

Please sign in to comment.