X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Ftest%2FStringUtilsTest.java;h=452b9061185f9838b2145fb74c7e533bb99d0c11;hb=6a493e0518e4b88cc68f6d4aae1faea08aa3afce;hp=5c939575059b4e2a87805f718467bc8eee56d396;hpb=451f434bd60a354ae5928f5da36b76f24b7b423b;p=nikiroo-utils.git diff --git a/src/be/nikiroo/utils/test/StringUtilsTest.java b/src/be/nikiroo/utils/test/StringUtilsTest.java index 5c93957..452b906 100644 --- a/src/be/nikiroo/utils/test/StringUtilsTest.java +++ b/src/be/nikiroo/utils/test/StringUtilsTest.java @@ -2,6 +2,7 @@ package be.nikiroo.utils.test; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -76,7 +77,7 @@ class StringUtilsTest extends TestLauncher { true, data.trim().equals(result.trim())); result = StringUtils.padString(data, size, false, - Alignment.End); + Alignment.RIGHT); if (size > data.length()) { assertEquals( "Padding a String to the end should work as expected", @@ -84,7 +85,25 @@ class StringUtilsTest extends TestLauncher { } result = StringUtils.padString(data, size, false, - Alignment.Center); + Alignment.JUSTIFY); + if (size > data.length()) { + String unspacedData = data.trim(); + String unspacedResult = result.trim(); + for (int i = 0; i < size; i++) { + unspacedData = unspacedData.replace(" ", " "); + unspacedResult = unspacedResult.replace(" ", + " "); + } + + assertEquals( + "Justified text trimmed with all spaces collapsed " + + "sould be identical to original text " + + "trimmed with all spaces collapsed", + unspacedData, unspacedResult); + } + + result = StringUtils.padString(data, size, false, + Alignment.CENTER); if (size > data.length()) { int before = 0; for (int i = 0; i < result.length() @@ -120,6 +139,38 @@ class StringUtilsTest extends TestLauncher { } }); + addTest(new TestCase("Justifying") { + @Override + public void test() throws Exception { + for (String data : new String[] { "test", + "let's test some words", "" }) { + int total = 0; + for (String word : data.split((" "))) { + total += word.replace("-", "").replace(" ", "") + .length(); + } + List result = StringUtils.justifyText(data, 5, + StringUtils.Alignment.LEFT); + + System.out.println("["+data+"] -> ["); + + int totalResult = 0; + for (String resultLine : result) { + System.out.println(resultLine); + for (String word : resultLine.split((" "))) { + totalResult += word.replace("-", "") + .replace(" ", "").length(); + } + } + System.out.println("]"); + + assertEquals( + "The number of letters ('-' not included) should be identical before and after", + total, totalResult); + } + } + }); + addTest(new TestCase("unhtml") { @Override public void test() throws Exception {