diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-22 19:02:42 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-04-22 19:38:31 -0300 |
| commit | df25a7e566e4a91eafbc30566cd19418ac977d02 (patch) | |
| tree | efa599433dd60412b9647e5e9ba1434a7122f7b8 /clformat/src | |
| parent | 321f2faa7fb0e3ff5be9e2cd08aa2f33124c4697 (diff) | |
Fixed comb. of `@` mod and 4-param ~A dir.
Fixed the combination of the `@` mod and the 4-parameter form of the ~A
directive. Previously, this would add an excessive amount of padding to
the output.
Also added some test cases for this, and fixed another similar padding
bug.
Diffstat (limited to 'clformat/src')
| -rw-r--r-- | clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java | 37 | ||||
| -rw-r--r-- | clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java | 12 |
2 files changed, 38 insertions, 11 deletions
diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java index 1767acd..513b7aa 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java @@ -15,28 +15,43 @@ public class AestheticDirective implements Directive { @Override public void format(FormatParameters dirParams) throws IOException { + // Check that we have an item CLFormatter.checkItem(dirParams.item, 'A'); - int mincol = 0, colinc = 1, minpad = 0; + // Parameter values + int mincol = 0; + int colinc = 1; + int minpad = 0; + char padchar = ' '; CLParameters params = dirParams.arrParams; - if (params.length() == 0) { + + // We take 0, 1 or 4 parameters + switch (params.length()) { + case 0: // Zero parameters, use all defaults - } else if (params.length() == 1) { + break; + case 1: params.mapIndices("mincol"); mincol = params.getInt("mincol", "minimum column count", "A", 0); - } else if (params.length() < 4) { - throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive"); - } else { - params.mapIndices("colinc", "minpad", "padchar"); + break; + case 4: + params.mapIndices("mincol", "colinc", "minpad", "padchar"); + + mincol = params.getInt("mincol", "minimum column count", "A", 0); + colinc = params.getInt("colinc", "padding increment", "A", 1); + minpad = params.getInt("minpad", "minimum amount of padding", "A", 0); - colinc = params.getInt( "colinc", "padding increment", "A", 1); - minpad = params.getInt( "minpad", "minimum amount of padding", "A", 0); padchar = params.getChar("padchar", "padding character", "A", ' '); + break; + default: + throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive"); } + String tmp = dirParams.item.toString(); + StringBuilder work = new StringBuilder(); if (dirParams.mods.atMod) { @@ -44,14 +59,14 @@ public class AestheticDirective implements Directive { work.append(padchar); } - for (int i = work.length(); i < mincol; i++) { + for (int i = work.length() + tmp.length(); i < mincol; i++) { for (int k = 0; k < colinc; k++) { work.append(padchar); } } } - work.append(dirParams.item.toString()); + work.append(tmp); if (!dirParams.mods.atMod) { for (int i = 0; i < minpad; i++) { diff --git a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java index b14a6b3..d3d7075 100644 --- a/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java +++ b/clformat/src/test/java/bjc/utils/test/ioutils/CLFormatterTest.java @@ -110,11 +110,23 @@ public class CLFormatterTest { } @Test + public void testADirective() { + assertFormat("foo", "~A", "foo"); + assertFormat("foobar ", "~7A", "foobar"); + assertFormat(" foobar", "~7@A", "foobar"); + assertFormat(" foobar", "~#mincol;8,#colinc;2,#minpad;1,#padchar;' @A", "foobar"); + } + + @Test public void testRandomCases() { // Random test cases assertEquals("3 dogs are here", format("~D dog~:[s are~; is~] here", 3, 3 == 1)); } + private void assertFormat(String res, String fmt, Object... params) { + assertEquals(res, format(fmt, params)); + } + private String format(String str, Object... params) { try { return fmt.formatString(str, params); |
