You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

193 lines
5.5 KiB

  1. // Copyright 2015 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. "bytes"
  7. "fmt"
  8. "io"
  9. "testing"
  10. "golang.org/x/text/internal"
  11. "golang.org/x/text/internal/format"
  12. "golang.org/x/text/language"
  13. "golang.org/x/text/message/catalog"
  14. )
  15. type formatFunc func(s fmt.State, v rune)
  16. func (f formatFunc) Format(s fmt.State, v rune) { f(s, v) }
  17. func TestBinding(t *testing.T) {
  18. testCases := []struct {
  19. tag string
  20. value interface{}
  21. want string
  22. }{
  23. {"en", 1, "1"},
  24. {"en", "2", "2"},
  25. { // Language is passed.
  26. "en",
  27. formatFunc(func(fs fmt.State, v rune) {
  28. s := fs.(format.State)
  29. io.WriteString(s, s.Language().String())
  30. }),
  31. "en",
  32. },
  33. }
  34. for i, tc := range testCases {
  35. p := NewPrinter(language.MustParse(tc.tag))
  36. if got := p.Sprint(tc.value); got != tc.want {
  37. t.Errorf("%d:%s:Sprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
  38. }
  39. var buf bytes.Buffer
  40. p.Fprint(&buf, tc.value)
  41. if got := buf.String(); got != tc.want {
  42. t.Errorf("%d:%s:Fprint(%v) = %q; want %q", i, tc.tag, tc.value, got, tc.want)
  43. }
  44. }
  45. }
  46. func TestLocalization(t *testing.T) {
  47. type test struct {
  48. tag string
  49. key Reference
  50. args []interface{}
  51. want string
  52. }
  53. args := func(x ...interface{}) []interface{} { return x }
  54. empty := []interface{}{}
  55. joe := []interface{}{"Joe"}
  56. joeAndMary := []interface{}{"Joe", "Mary"}
  57. testCases := []struct {
  58. desc string
  59. cat []entry
  60. test []test
  61. }{{
  62. desc: "empty",
  63. test: []test{
  64. {"en", "key", empty, "key"},
  65. {"en", "", empty, ""},
  66. {"nl", "", empty, ""},
  67. },
  68. }, {
  69. desc: "hierarchical languages",
  70. cat: []entry{
  71. {"en", "hello %s", "Hello %s!"},
  72. {"en-GB", "hello %s", "Hellø %s!"},
  73. {"en-US", "hello %s", "Howdy %s!"},
  74. {"en", "greetings %s and %s", "Greetings %s and %s!"},
  75. },
  76. test: []test{
  77. {"und", "hello %s", joe, "hello Joe"},
  78. {"nl", "hello %s", joe, "hello Joe"},
  79. {"en", "hello %s", joe, "Hello Joe!"},
  80. {"en-US", "hello %s", joe, "Howdy Joe!"},
  81. {"en-GB", "hello %s", joe, "Hellø Joe!"},
  82. {"en-oxendict", "hello %s", joe, "Hello Joe!"},
  83. {"en-US-oxendict-u-ms-metric", "hello %s", joe, "Howdy Joe!"},
  84. {"und", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
  85. {"nl", "greetings %s and %s", joeAndMary, "greetings Joe and Mary"},
  86. {"en", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
  87. {"en-US", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
  88. {"en-GB", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
  89. {"en-oxendict", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
  90. {"en-US-oxendict-u-ms-metric", "greetings %s and %s", joeAndMary, "Greetings Joe and Mary!"},
  91. },
  92. }, {
  93. desc: "references",
  94. cat: []entry{
  95. {"en", "hello", "Hello!"},
  96. },
  97. test: []test{
  98. {"en", "hello", empty, "Hello!"},
  99. {"en", Key("hello", "fallback"), empty, "Hello!"},
  100. {"en", Key("xxx", "fallback"), empty, "fallback"},
  101. {"und", Key("hello", "fallback"), empty, "fallback"},
  102. },
  103. }, {
  104. desc: "zero substitution", // work around limitation of fmt
  105. cat: []entry{
  106. {"en", "hello %s", "Hello!"},
  107. {"en", "hi %s and %s", "Hello %[2]s!"},
  108. },
  109. test: []test{
  110. {"en", "hello %s", joe, "Hello!"},
  111. {"en", "hello %s", joeAndMary, "Hello!"},
  112. {"en", "hi %s and %s", joeAndMary, "Hello Mary!"},
  113. // The following tests resolve to the fallback string.
  114. {"und", "hello", joeAndMary, "hello"},
  115. {"und", "hello %%%%", joeAndMary, "hello %%"},
  116. {"und", "hello %#%%4.2% ", joeAndMary, "hello %% "},
  117. {"und", "hello %s", joeAndMary, "hello Joe%!(EXTRA string=Mary)"},
  118. {"und", "hello %+%%s", joeAndMary, "hello %Joe%!(EXTRA string=Mary)"},
  119. {"und", "hello %-42%%s ", joeAndMary, "hello %Joe %!(EXTRA string=Mary)"},
  120. },
  121. }, {
  122. desc: "number formatting",
  123. cat: []entry{
  124. {"und", "files", "%d files left"},
  125. {"und", "meters", "%.2f meters"},
  126. {"de", "files", "%d Dateien übrig"},
  127. },
  128. test: []test{
  129. {"en", "meters", args(3000.2), "3,000.20 meters"},
  130. {"en-u-nu-gujr", "files", args(123456), "૧૨૩,૪૫૬ files left"},
  131. {"de", "files", args(1234), "1.234 Dateien übrig"},
  132. {"de-CH", "files", args(1234), "1’234 Dateien übrig"},
  133. {"de-CH-u-nu-mong", "files", args(1234), "᠑’᠒᠓᠔ Dateien übrig"},
  134. },
  135. }, {
  136. desc: "substitute translation",
  137. cat: []entry{
  138. {"en", "google", "Google"},
  139. {"en", "sub", "%s"},
  140. {"en", "visit", "Lookup: %m."},
  141. },
  142. test: []test{
  143. {"en", "visit", args("google"), "Lookup: Google."},
  144. {"en", "visit", args("sub"), "Lookup: %s."},
  145. },
  146. }}
  147. for _, tc := range testCases {
  148. cat, _ := initCat(tc.cat)
  149. for i, pt := range tc.test {
  150. t.Run(fmt.Sprintf("%s:%d", tc.desc, i), func(t *testing.T) {
  151. p := NewPrinter(language.MustParse(pt.tag), Catalog(cat))
  152. if got := p.Sprintf(pt.key, pt.args...); got != pt.want {
  153. t.Errorf("Sprintf(%q, %v) = %s; want %s",
  154. pt.key, pt.args, got, pt.want)
  155. return // Next error will likely be the same.
  156. }
  157. w := &bytes.Buffer{}
  158. p.Fprintf(w, pt.key, pt.args...)
  159. if got := w.String(); got != pt.want {
  160. t.Errorf("Fprintf(%q, %v) = %s; want %s",
  161. pt.key, pt.args, got, pt.want)
  162. }
  163. })
  164. }
  165. }
  166. }
  167. type entry struct{ tag, key, msg string }
  168. func initCat(entries []entry) (*catalog.Builder, []language.Tag) {
  169. tags := []language.Tag{}
  170. cat := catalog.NewBuilder()
  171. for _, e := range entries {
  172. tag := language.MustParse(e.tag)
  173. tags = append(tags, tag)
  174. cat.SetString(tag, e.key, e.msg)
  175. }
  176. return cat, internal.UniqueTags(tags)
  177. }