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.

235 lines
5.3KB

  1. /*
  2. * Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
  3. * Licensed under the MIT License. See LICENSE file in the project root for full license information.
  4. */
  5. package gotext
  6. import (
  7. "os"
  8. "path"
  9. "testing"
  10. )
  11. func TestMo_Get(t *testing.T) {
  12. // Create po object
  13. mo := new(Mo)
  14. // Try to parse a directory
  15. mo.ParseFile(path.Clean(os.TempDir()))
  16. // Parse file
  17. mo.ParseFile("fixtures/en_US/default.mo")
  18. // Test translations
  19. tr := mo.Get("My text")
  20. if tr != "Translated text" {
  21. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  22. }
  23. // Test translations
  24. tr = mo.Get("language")
  25. if tr != "en_US" {
  26. t.Errorf("Expected 'en_US' but got '%s'", tr)
  27. }
  28. }
  29. func TestMo(t *testing.T) {
  30. // Create po object
  31. mo := new(Mo)
  32. // Try to parse a directory
  33. mo.ParseFile(path.Clean(os.TempDir()))
  34. // Parse file
  35. mo.ParseFile("fixtures/en_US/default.mo")
  36. // Test translations
  37. tr := mo.Get("My text")
  38. if tr != "Translated text" {
  39. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  40. }
  41. v := "Variable"
  42. tr = mo.Get("One with var: %s", v)
  43. if tr != "This one is the singular: Variable" {
  44. t.Errorf("Expected 'This one is the singular: Variable' but got '%s'", tr)
  45. }
  46. // Test multi-line id
  47. tr = mo.Get("multilineid")
  48. if tr != "id with multiline content" {
  49. t.Errorf("Expected 'id with multiline content' but got '%s'", tr)
  50. }
  51. // Test multi-line plural id
  52. tr = mo.Get("multilinepluralid")
  53. if tr != "plural id with multiline content" {
  54. t.Errorf("Expected 'plural id with multiline content' but got '%s'", tr)
  55. }
  56. // Test multi-line
  57. tr = mo.Get("Multi-line")
  58. if tr != "Multi line" {
  59. t.Errorf("Expected 'Multi line' but got '%s'", tr)
  60. }
  61. // Test plural
  62. tr = mo.GetN("One with var: %s", "Several with vars: %s", 2, v)
  63. if tr != "This one is the plural: Variable" {
  64. t.Errorf("Expected 'This one is the plural: Variable' but got '%s'", tr)
  65. }
  66. // Test not existent translations
  67. tr = mo.Get("This is a test")
  68. if tr != "This is a test" {
  69. t.Errorf("Expected 'This is a test' but got '%s'", tr)
  70. }
  71. tr = mo.GetN("This is a test", "This are tests", 100)
  72. if tr != "This are tests" {
  73. t.Errorf("Expected 'This are tests' but got '%s'", tr)
  74. }
  75. // Test context translations
  76. v = "Test"
  77. tr = mo.GetC("One with var: %s", "Ctx", v)
  78. if tr != "This one is the singular in a Ctx context: Test" {
  79. t.Errorf("Expected 'This one is the singular in a Ctx context: Test' but got '%s'", tr)
  80. }
  81. // Test plural
  82. tr = mo.GetNC("One with var: %s", "Several with vars: %s", 17, "Ctx", v)
  83. if tr != "This one is the plural in a Ctx context: Test" {
  84. t.Errorf("Expected 'This one is the plural in a Ctx context: Test' but got '%s'", tr)
  85. }
  86. // Test default plural vs singular return responses
  87. tr = mo.GetN("Original", "Original plural", 4)
  88. if tr != "Original plural" {
  89. t.Errorf("Expected 'Original plural' but got '%s'", tr)
  90. }
  91. tr = mo.GetN("Original", "Original plural", 1)
  92. if tr != "Original" {
  93. t.Errorf("Expected 'Original' but got '%s'", tr)
  94. }
  95. // Test empty Translation strings
  96. tr = mo.Get("Empty Translation")
  97. if tr != "Empty Translation" {
  98. t.Errorf("Expected 'Empty Translation' but got '%s'", tr)
  99. }
  100. tr = mo.Get("Empty plural form singular")
  101. if tr != "Singular translated" {
  102. t.Errorf("Expected 'Singular translated' but got '%s'", tr)
  103. }
  104. tr = mo.GetN("Empty plural form singular", "Empty plural form", 1)
  105. if tr != "Singular translated" {
  106. t.Errorf("Expected 'Singular translated' but got '%s'", tr)
  107. }
  108. tr = mo.GetN("Empty plural form singular", "Empty plural form", 2)
  109. if tr != "Empty plural form" {
  110. t.Errorf("Expected 'Empty plural form' but got '%s'", tr)
  111. }
  112. // Test last Translation
  113. tr = mo.Get("More")
  114. if tr != "More translation" {
  115. t.Errorf("Expected 'More translation' but got '%s'", tr)
  116. }
  117. }
  118. func TestMoRace(t *testing.T) {
  119. // Create Po object
  120. mo := new(Mo)
  121. // Create sync channels
  122. pc := make(chan bool)
  123. rc := make(chan bool)
  124. // Parse po content in a goroutine
  125. go func(mo *Mo, done chan bool) {
  126. // Parse file
  127. mo.ParseFile("fixtures/en_US/default.mo")
  128. done <- true
  129. }(mo, pc)
  130. // Read some Translation on a goroutine
  131. go func(mo *Mo, done chan bool) {
  132. mo.Get("My text")
  133. done <- true
  134. }(mo, rc)
  135. // Read something at top level
  136. mo.Get("My text")
  137. // Wait for goroutines to finish
  138. <-pc
  139. <-rc
  140. }
  141. func TestNewMoTranslatorRace(t *testing.T) {
  142. // Create Po object
  143. mo := NewMoTranslator()
  144. // Create sync channels
  145. pc := make(chan bool)
  146. rc := make(chan bool)
  147. // Parse po content in a goroutine
  148. go func(mo Translator, done chan bool) {
  149. // Parse file
  150. mo.ParseFile("fixtures/en_US/default.mo")
  151. done <- true
  152. }(mo, pc)
  153. // Read some Translation on a goroutine
  154. go func(mo Translator, done chan bool) {
  155. mo.Get("My text")
  156. done <- true
  157. }(mo, rc)
  158. // Read something at top level
  159. mo.Get("My text")
  160. // Wait for goroutines to finish
  161. <-pc
  162. <-rc
  163. }
  164. func TestMoBinaryEncoding(t *testing.T) {
  165. // Create mo objects
  166. mo := new(Mo)
  167. mo2 := new(Mo)
  168. // Parse file
  169. mo.ParseFile("fixtures/en_US/default.mo")
  170. buff, err := mo.MarshalBinary()
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. err = mo2.UnmarshalBinary(buff)
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. // Test translations
  179. tr := mo2.Get("My text")
  180. if tr != "Translated text" {
  181. t.Errorf("Expected 'Translated text' but got '%s'", tr)
  182. }
  183. // Test translations
  184. tr = mo2.Get("language")
  185. if tr != "en_US" {
  186. t.Errorf("Expected 'en_US' but got '%s'", tr)
  187. }
  188. }