catalog_test.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package message
  5. import (
  6. "strings"
  7. "testing"
  8. "golang.org/x/text/language"
  9. "golang.org/x/text/message/catalog"
  10. )
  11. func TestMatchLanguage(t *testing.T) {
  12. c := catalog.NewBuilder(catalog.Fallback(language.English))
  13. c.SetString(language.Bengali, "", "")
  14. c.SetString(language.English, "", "")
  15. c.SetString(language.German, "", "")
  16. saved := DefaultCatalog
  17. defer func() { DefaultCatalog = saved }()
  18. DefaultCatalog = c
  19. testCases := []struct {
  20. args string // '|'-separated list
  21. want string
  22. }{{
  23. args: "de-CH",
  24. want: "de-u-rg-chzzzz",
  25. }, {
  26. args: "bn-u-nu-latn|en-US,en;q=0.9,de;q=0.8,nl;q=0.7",
  27. want: "bn-u-nu-latn",
  28. }, {
  29. args: "gr",
  30. want: "en",
  31. }}
  32. for _, tc := range testCases {
  33. t.Run(tc.args, func(t *testing.T) {
  34. got := MatchLanguage(strings.Split(tc.args, "|")...)
  35. if got != language.Make(tc.want) {
  36. t.Errorf("got %q; want %q", got, tc.want)
  37. }
  38. })
  39. }
  40. }