|
|
@@ -13,6 +13,7 @@ import java.util.Map;
|
|
|
import net.ranides.assira.trace.LoggerUtils;
|
|
|
|
|
|
import org.junit.Test;
|
|
|
+import static org.junit.Assert.*;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
@@ -20,22 +21,32 @@ import org.junit.Test;
|
|
|
*/
|
|
|
public class PrintFormatterTest {
|
|
|
|
|
|
- private static final class F implements PrintFunction {
|
|
|
- @Override
|
|
|
- public void apply(Appendable target, PrintParams params, Object[] values) throws IOException {
|
|
|
- System.out.printf(
|
|
|
- "F(%s / %s / %s / %s) = %s\n",
|
|
|
- params.flags(), params.padder(), params.width(), params.precision(), values[params.index()]
|
|
|
- );
|
|
|
- target.append("F");
|
|
|
- }
|
|
|
- @Override
|
|
|
- public int estimate(PrintParams params, Object[] values) {
|
|
|
- return Math.max(4, params.width());
|
|
|
- }
|
|
|
+ private final PrintFormatter formatter;
|
|
|
+
|
|
|
+ public PrintFormatterTest() {
|
|
|
+ LoggerUtils.resetLogger4j(true);
|
|
|
+
|
|
|
+ Map<Character, PrintFunction> map = new HashMap<Character, PrintFunction>();
|
|
|
+ map.put('s', PrintFunction.PrintString.INSTANCE);
|
|
|
+ map.put('d', PrintFunction.PrintNumberDec.INSTANCE);
|
|
|
+ map.put('f', PrintFunction.PrintNumberFP.INSTANCE);
|
|
|
+ map.put('t', new PrintText());
|
|
|
+
|
|
|
+ formatter = new PrintFormatter(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testCompile() {
|
|
|
+
|
|
|
+ PrintPattern pattern = formatter.compile("Hello %-'.8s :> %% ! %3$07.2f :) %f %1$d");
|
|
|
+
|
|
|
+ assertEquals("Hello world... :> %% ! 0019.65 :) 3.141593 14", pattern.format("world", 14, 3.1415926, 19.6451));
|
|
|
+
|
|
|
+ assertEquals("ŹREBIĘ zjada ćmę", formatter.format("%ut zjada %lt", "ŹrEbIę", "ĆmĘ"));
|
|
|
+
|
|
|
}
|
|
|
|
|
|
- private static final class S implements PrintFunction {
|
|
|
+ private static final class PrintText implements PrintFunction {
|
|
|
|
|
|
@Override
|
|
|
public void apply(Appendable target, PrintParams params, Object[] values) throws IOException {
|
|
|
@@ -57,28 +68,4 @@ public class PrintFormatterTest {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
-
|
|
|
- private final PrintFormatter formatter;
|
|
|
-
|
|
|
- public PrintFormatterTest() {
|
|
|
- LoggerUtils.resetLogger4j(true);
|
|
|
-
|
|
|
- Map<Character, PrintFunction> map = new HashMap<Character, PrintFunction>();
|
|
|
- map.put('s', PrintFunction.PrintString.INSTANCE);
|
|
|
- map.put('d', PrintFunction.PrintNumberDec.INSTANCE);
|
|
|
- map.put('f', PrintFunction.PrintNumberFP.INSTANCE);
|
|
|
- map.put('t', new S());
|
|
|
-
|
|
|
- formatter = new PrintFormatter(map);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void testCompile() {
|
|
|
-
|
|
|
- PrintPattern pattern = formatter.compile("Hello %-'.8s :> %% ! %3$07.2f :) %f %1$d");
|
|
|
-
|
|
|
- System.out.println( pattern.format("world", 14, 3.1415926, 19.6451) );
|
|
|
-
|
|
|
- System.out.println( formatter.format("%ut zjada %lt", "ŹrEbIę", "ĆmĘ") );
|
|
|
- }
|
|
|
}
|