StringUtlTest.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. XOWA: the XOWA Offline Wiki Application
  3. Copyright (C) 2012-2021 gnosygnu@gmail.com
  4. XOWA is licensed under the terms of the General Public License (GPL) Version 3,
  5. or alternatively under the terms of the Apache License Version 2.0.
  6. You may use XOWA according to either of these licenses as is most appropriate
  7. for your project on a case-by-case basis.
  8. The terms of each license can be found in the source code repository:
  9. GPLv3 License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-GPLv3.txt
  10. Apache License: https://github.com/gnosygnu/xowa/blob/master/LICENSE-APACHE2.txt
  11. */
  12. package gplx.objects.strings;
  13. import gplx.types.basics.utls.ObjectUtl;
  14. import gplx.frameworks.tests.GfoTstr;
  15. import gplx.types.basics.utls.StringUtl;
  16. import org.junit.Test;
  17. public class StringUtlTest {
  18. private final StringUtlTstr fxt = new StringUtlTstr();
  19. @Test public void Len() {
  20. fxt.TestLen("" , 0);
  21. fxt.TestLen("abc", 3);
  22. }
  23. @Test public void X() {
  24. GfoTstr.Write("k".compareTo("a"));
  25. }
  26. @Test public void Format() {
  27. fxt.TestFormat("empty fmt" , "" , "");
  28. fxt.TestFormat("empty fmt w/ args" , "" , "", "a");
  29. fxt.TestFormat("no args" , "a" , "a");
  30. fxt.TestFormat("args = 1" , "a" , "{0}", "a");
  31. fxt.TestFormat("args = n" , "a + b" , "{0} + {1}", "a", "b");
  32. fxt.TestFormat("escape {" , "{" , "{{", 0);
  33. fxt.TestFormat("escape }" , "}" , "}}", 0);
  34. fxt.TestFormat("nested" , "{a0c}" , "{a{0}c}", 0);
  35. fxt.TestFormat("nested; invalid" , "{a{b}c}" , "{a{b}c}", 0);
  36. fxt.TestFormat("out of bounds" , "{1}" , "{1}", "a");
  37. fxt.TestFormat("invalid arg" , "{a} {b}" , "{a} {b}", 0);
  38. fxt.TestFormat("invalid and valid args" , "{a}0{b}1", "{a}{0}{b}{1}", 0, 1);
  39. fxt.TestFormat("dangling" , "{0" , "{0", 0);
  40. }
  41. @Test public void LimitToFirst() {
  42. TestLimitToFirst("abc", 0, "");
  43. TestLimitToFirst("abc", 1, "a");
  44. TestLimitToFirst("abc", 2, "ab");
  45. TestLimitToFirst("abc", 3, "abc");
  46. TestLimitToFirst("abc", 4, "abc");
  47. TestLimitToFirst("abc", -1);
  48. }
  49. void TestLimitToFirst(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.LimitToFirst(s, v));}
  50. void TestLimitToFirst(String s, int v) {try {StringUtl.LimitToFirst(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
  51. @Test public void DelBgn() {
  52. TestDelBgn("abc", 0, "abc");
  53. TestDelBgn("abc", 1, "bc");
  54. TestDelBgn("abc", 2, "c");
  55. TestDelBgn("abc", 3, "");
  56. TestDelBgn(null, 0);
  57. TestDelBgn("abc", 4);
  58. }
  59. void TestDelBgn(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.DelBgn(s, v));}
  60. void TestDelBgn(String s, int v) {try {StringUtl.DelBgn(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
  61. @Test public void DelEnd() {
  62. TestDelEnd("abc", 0, "abc");
  63. TestDelEnd("abc", 1, "ab");
  64. TestDelEnd("abc", 2, "a");
  65. TestDelEnd("abc", 3, "");
  66. TestDelEnd(null, 0);
  67. TestDelEnd("abc", 4);
  68. }
  69. void TestDelEnd(String s, int v, String expd) {GfoTstr.Eq(expd, StringUtl.DelEnd(s, v));}
  70. void TestDelEnd(String s, int v) {try {StringUtl.DelEnd(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
  71. @Test public void DelEndIf() {
  72. TestDelEndIf("abc", "", "abc");
  73. TestDelEndIf("abc", "c", "ab");
  74. TestDelEndIf("abc", "bc", "a");
  75. TestDelEndIf("abc", "abc", "");
  76. TestDelEndIf("abc", "abcd", "abc");
  77. TestDelEndIf("abc", "ab", "abc");
  78. TestDelEndIf(null, "");
  79. TestDelEndIf("", null);
  80. }
  81. void TestDelEndIf(String s, String v, String expd) {GfoTstr.Eq(expd, StringUtl.DelEndIf(s, v));}
  82. void TestDelEndIf(String s, String v) {try {StringUtl.DelEndIf(s, v);} catch (Exception exc) {GfoTstr.EqErr(exc, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
  83. @Test public void MidByPos() {
  84. TestMidByPos("abc", 0, 0, "");
  85. TestMidByPos("abc", 0, 1, "a");
  86. TestMidByPos("abc", 0, 2, "ab");
  87. TestMidByPos("abc", 0, 3, "abc");
  88. TestMidByPos("abc", 2, 3, "c");
  89. TestMidByPos("abc", 1, 5);
  90. // TestMidByPos("abc", 0, 4);
  91. }
  92. void TestMidByPos(String s, int bgn, int end, String expd) {GfoTstr.Eq(expd, StringUtl.Mid(s, bgn, end));}
  93. void TestMidByPos(String s, int bgn, int end) {try {StringUtl.Mid(s, bgn, end);} catch (Exception e) {GfoTstr.EqErr(e, gplx.types.errs.Err.class); return;} GfoTstr.FailBcExpdError();}
  94. @Test public void Count() {
  95. String text = "0 0 0";
  96. GfoTstr.Eq(3, StringUtl.Count(text, "0"));
  97. }
  98. @Test public void Has() {
  99. String text = "find word";
  100. GfoTstr.EqBoolY(StringUtl.Has(text, "word"));
  101. GfoTstr.EqBoolN(StringUtl.Has(text, "nothing"));
  102. }
  103. @Test public void Repeat() {
  104. GfoTstr.Eq("333", StringUtl.Repeat("3", 3));
  105. }
  106. @Test public void Split() {
  107. TestSplit("ab", " ", "ab"); // no match -> return array with original input
  108. TestSplit("ab cd", " ", "ab", "cd"); // separator.length = 1
  109. TestSplit("ab+!cd", "+!", "ab", "cd"); // separator.length = 2
  110. TestSplit("ab+!cd+!ef", "+!", "ab", "cd", "ef"); // terms = 3
  111. TestSplit("ab+!cd+!", "+!", "ab", "cd", ""); // closing separator
  112. TestSplit("+!ab", "+!", "", "ab"); // opening separator
  113. TestSplit("ab+cd+!ef", "+!", "ab+cd", "ef"); // ignore partial matches
  114. TestSplit("ab+!cd+", "+!", "ab", "cd+"); // ignore partial matches; end of String
  115. // boundary
  116. TestSplit("ab", "", "ab"); // separator.length = 0 -> return array with input as only member
  117. TestSplit("", " ", ""); // empty input -> return array with empty input
  118. // acceptance
  119. TestSplit("this\r\nis\na\rtest\r\n.", "\r\n", "this", "is\na\rtest", ".");
  120. } void TestSplit(String text, String separator, String... expd) {GfoTstr.EqLines(expd, StringUtl.Split(text, separator));}
  121. @Test public void Concat_with_obj() {
  122. TestConcatWith_any("a|b", "|", "a", "b"); // do not append final delimiter
  123. TestConcatWith_any("a||c", "|", "a", null, "c"); // null
  124. TestConcatWith_any("a|b", "|", ObjectUtl.Ary("a", "b")); // pass array as arg
  125. } void TestConcatWith_any(String expd, String delimiter, Object... array) {GfoTstr.Eq(expd, StringUtl.ConcatWithObj(delimiter, array));}
  126. @Test public void FindBwd() { // WORKAROUND.CS:String.LastIndexOf returns -1 for multi-chars;
  127. TestFindRev("abc", "a", 0, 0);
  128. TestFindRev("abc", "ab", 0, 0); // 2 chars
  129. TestFindRev("abc", "abc", 0, 0); // 3 chars
  130. TestFindRev("ab", "abc", 0, -1); // out of index error
  131. TestFindRev("ababab", "ab", 2, 2); // make sure cs implementation doesn't pick up next
  132. } void TestFindRev(String s, String find, int pos, int expd) {GfoTstr.Eq(expd, StringUtl.FindBwd(s, find, pos));}
  133. @Test public void Extract_after_bwd() {
  134. Extract_after_bwd_tst("a/b", "/", "b");
  135. Extract_after_bwd_tst("a/", "/", "");
  136. Extract_after_bwd_tst("a", "/", "");
  137. } void Extract_after_bwd_tst(String src, String dlm, String expd) {GfoTstr.Eq(expd, StringUtl.ExtractAfterBwd(src, dlm));}
  138. }
  139. class StringUtlTstr {
  140. public void TestLen(String v, int expd) {
  141. GfoTstr.Eq(expd, StringUtl.Len(v));
  142. }
  143. public void TestFormat(String note, String expd, String fmt, Object... ary) {
  144. GfoTstr.Eq(expd, StringUtl.Format(fmt, ary), note);
  145. }
  146. }