/src/textmatch/POMsgSourceTest.java

https://github.com/gkovacs/textmatch · Java · 211 lines · 191 code · 20 blank · 0 comment · 0 complexity · 5db50872879c4595784a1eceb2645cf9 MD5 · raw file

  1. package textmatch;
  2. import static textmatch.LCS.*;
  3. import java.util.*;
  4. import org.junit.*;
  5. import static textmatch.GStringUtils.*;
  6. import static textmatch.GCollectionUtils.*;
  7. import static textmatch.POMsgSource.*;
  8. import static org.junit.Assert.assertArrayEquals;
  9. import static org.junit.Assert.assertEquals;
  10. public class POMsgSourceTest {
  11. @Test
  12. public void testBasicPair() throws Exception {
  13. List<String> po = new ArrayList<String>();
  14. po.add("msgid \"gesture|Move left\"");
  15. po.add("msgstr \"Mover a la izquierda\"");
  16. POMsgSource msgsrc = new POMsgSource(po);
  17. List<String> expected = singleElemList("gesture|Move left");
  18. assertEquals(expected, msgsrc.getMsgStrings());
  19. }
  20. @Test
  21. public void testMultiLines() throws Exception {
  22. List<String> po = new ArrayList<String>();
  23. po.add("msgid \"\"");
  24. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  25. po.add("\"performed.\"");
  26. POMsgSource msgsrc = new POMsgSource(po);
  27. List<String> expected = singleElemList("Indicates whether to close the shell when an upgrade or uninstall action is performed.");
  28. assertEquals(expected, msgsrc.getMsgStrings());
  29. }
  30. @Test
  31. public void testIgnoreComments() throws Exception {
  32. List<String> po = new ArrayList<String>();
  33. po.add("msgid \"\"");
  34. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  35. po.add("#comment text2");
  36. po.add("\"performed.\"");
  37. POMsgSource msgsrc = new POMsgSource(po);
  38. List<String> expected = singleElemList("Indicates whether to close the shell when an upgrade or uninstall action is performed.");
  39. assertEquals(expected, msgsrc.getMsgStrings());
  40. }
  41. @Test
  42. public void testTemplateSubstitution() throws Exception {
  43. List<String> po = new ArrayList<String>();
  44. po.add("msgid \"\"");
  45. po.add("\"The shortcut \\\"%s\\\" cannot be used because it will become impossible to type \"");
  46. po.add("\"using this key.\\n\"");
  47. po.add("\"Please try with a key such as Control, Alt or Shift at the same time.\"");
  48. POMsgSource msgsrc = new POMsgSource(po);
  49. List<String> expected = singleElemList("The shortcut \"" + SUBCHAR +"\" cannot be used because it will become impossible to type using this key. Please try with a key such as Control, Alt or Shift at the same time.");
  50. assertEquals(expected, msgsrc.getMsgStrings());
  51. }
  52. @Test
  53. public void testTextFromMsgIdBlock() throws Exception {
  54. List<String> po = new ArrayList<String>();
  55. po.add("msgid \"\"");
  56. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  57. po.add("\"performed.\"");
  58. String expected = "Indicates whether to close the shell when an upgrade or uninstall action is performed.";
  59. assertEquals(expected, textFromMsgIdBlock(join(po, "\n")));
  60. }
  61. @Test
  62. public void testAnnotationFromMsgIdBlock() throws Exception {
  63. List<String> po = new ArrayList<String>();
  64. po.add("#& foo.png(1,2,3,4)~~~分sa分foo");
  65. po.add("msgid \"\"");
  66. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  67. po.add("\"performed.\"");
  68. MsgAnnotation annotation = annotationFromMsgIdBlock(join(po, "\n"));
  69. assertEquals("foo.png", annotation.filename);
  70. assertEquals(1, annotation.x);
  71. assertEquals(2, annotation.y);
  72. assertEquals(3, annotation.w);
  73. assertEquals(4, annotation.h);
  74. String[] expectedTemplateSubstitutions = new String[] {"sa", "foo"};
  75. assertArrayEquals(expectedTemplateSubstitutions, annotation.templateSubstitutions);
  76. }
  77. @Test
  78. public void testAnnotationListFromMsgIdBlock() throws Exception {
  79. List<String> po = new ArrayList<String>();
  80. po.add("#* foo.png(1,2,3,4)~~~分sa分foo");
  81. po.add("msgid \"\"");
  82. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  83. po.add("\"performed.\"");
  84. List<MsgAnnotation> annotationList = annotationListFromMsgIdBlock(join(po, "\n"));
  85. MsgAnnotation annotation = annotationList.get(0);
  86. assertEquals("foo.png", annotation.filename);
  87. assertEquals(1, annotation.x);
  88. assertEquals(2, annotation.y);
  89. assertEquals(3, annotation.w);
  90. assertEquals(4, annotation.h);
  91. String[] expectedTemplateSubstitutions = new String[] {"sa", "foo"};
  92. assertArrayEquals(expectedTemplateSubstitutions, annotation.templateSubstitutions);
  93. }
  94. @Test
  95. public void testSplitIntoMsgIdBlocks() throws Exception {
  96. List<String> po = new ArrayList<String>();
  97. po.add("msgid \"- GNOME Mouse Preferences\"");
  98. po.add("msgid \"\"");
  99. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  100. po.add("#comment text2");
  101. po.add("\"performed.\"");
  102. POMsgSource msgsrc = new POMsgSource(po);
  103. List<String> ex = new ArrayList<String>();
  104. ex.add("msgid \"- GNOME Mouse Preferences\"");
  105. ex.add("msgid \"\"" + '\n' +
  106. "\"Indicates whether to close the shell when an upgrade or uninstall action is \"" + '\n' +
  107. "#comment text2" + '\n' +
  108. "\"performed.\"");
  109. assertEquals(ex, msgsrc.splitIntoMsgIdBlocks());
  110. }
  111. @Test
  112. public void testSplitIntoMsgIdBlocks2() throws Exception {
  113. List<String> po = new ArrayList<String>();
  114. po.add("msgid \"- GNOME Mouse Preferences\"");
  115. po.add("#& some annotation");
  116. po.add("msgid \"\"");
  117. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  118. po.add("\"performed.\"");
  119. POMsgSource msgsrc = new POMsgSource(po);
  120. List<String> ex = new ArrayList<String>();
  121. ex.add("msgid \"- GNOME Mouse Preferences\"");
  122. ex.add("#& some annotation" + '\n' +
  123. "msgid \"\"" + '\n' +
  124. "\"Indicates whether to close the shell when an upgrade or uninstall action is \"" + '\n' +
  125. "\"performed.\"");
  126. assertEquals(ex, msgsrc.splitIntoMsgIdBlocks());
  127. }
  128. @Test
  129. public void testSplitIntoMsgIdBlocks3() throws Exception {
  130. List<String> po = new ArrayList<String>();
  131. po.add("#. comment1");
  132. po.add("msgid \"- GNOME Mouse Preferences\"");
  133. po.add("#. comment2");
  134. po.add("#. comment3");
  135. po.add("msgid \"foobar\"");
  136. POMsgSource msgsrc = new POMsgSource(po);
  137. List<String> ex = new ArrayList<String>();
  138. ex.add("#. comment1" + '\n' +
  139. "msgid \"- GNOME Mouse Preferences\"");
  140. ex.add("#. comment2" + '\n' +
  141. "#. comment3" + '\n' +
  142. "msgid \"foobar\"");
  143. assertEquals(ex, msgsrc.splitIntoMsgIdBlocks());
  144. }
  145. @Test
  146. public void testAnnotateTemplate() throws Exception {
  147. HashMap<String, String> annotations = new HashMap<String, String>();
  148. List<String> po = new ArrayList<String>();
  149. po.add("msgid \"\"");
  150. po.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  151. po.add("\"performed.\"");
  152. annotations.put("Indicates whether to close the shell when an upgrade or uninstall action is performed.", "some annotation");
  153. List<String> ex = new ArrayList<String>();
  154. ex.add("#& some annotation");
  155. ex.add("msgid \"\"");
  156. ex.add("\"Indicates whether to close the shell when an upgrade or uninstall action is \"");
  157. ex.add("\"performed.\"");
  158. assertEquals(join(ex, "\n"), new POMsgSource(po).makeAnnotatedMsgFile(annotations));
  159. }
  160. @Test
  161. public void testAnnotateTemplate2() throws Exception {
  162. HashMap<String, String> annotations = new HashMap<String, String>();
  163. List<String> po = new ArrayList<String>();
  164. po.add("msgid \"foo bar\"");
  165. po.add("msgid \"click clack\"");
  166. annotations.put("click clack", "some annotation");
  167. List<String> ex = new ArrayList<String>();
  168. ex.add("msgid \"foo bar\"");
  169. ex.add("#& some annotation");
  170. ex.add("msgid \"click clack\"");
  171. assertEquals(join(ex, "\n"), new POMsgSource(po).makeAnnotatedMsgFile(annotations));
  172. }
  173. @Test
  174. public void testGetMessageSource() throws Exception {
  175. List<String> po = new ArrayList<String>();
  176. po.add("#: ../capplets/keybindings/gnome-keybinding-properties.c:1207");
  177. po.add("#, c-format");
  178. po.add("msgid \"Error unsetting accelerator in configuration database: %s\"");
  179. String ex = "../capplets/keybindings/gnome-keybinding-properties.c:1207";
  180. assertEquals(ex, msgSourceFromMsgIdBlock(join(po, "\n")));
  181. }
  182. @Test
  183. public void testGetMessageSourceConcise() throws Exception {
  184. List<String> po = new ArrayList<String>();
  185. po.add("#: ../capplets/keybindings/gnome-keybinding-properties.c:1207");
  186. po.add("#, c-format");
  187. po.add("msgid \"Error unsetting accelerator in configuration database: %s\"");
  188. String ex = "gnome-keybinding-properties";
  189. assertEquals(ex, msgSourceConciseFromMsgIdBlock(join(po, "\n")));
  190. }
  191. }