From 91041ca9443c695a11244724bc6296b235bba716 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 28 Oct 2018 12:18:32 -0400 Subject: Add compilation support Inflection strings can now be compiled and reused, like regular expressions. --- src/test/java/bjc/inflexion/InflectPair.java | 30 ------ src/test/java/bjc/inflexion/InflectionMLTest.java | 117 +++++++++++++++++++++- 2 files changed, 114 insertions(+), 33 deletions(-) delete mode 100644 src/test/java/bjc/inflexion/InflectPair.java (limited to 'src/test') diff --git a/src/test/java/bjc/inflexion/InflectPair.java b/src/test/java/bjc/inflexion/InflectPair.java deleted file mode 100644 index d2f076a..0000000 --- a/src/test/java/bjc/inflexion/InflectPair.java +++ /dev/null @@ -1,30 +0,0 @@ -/** - * (C) Copyright 2018 Benjamin Culkin. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package bjc.inflexion; - -class InflectPair { - public String exp; - public Object[] pars; - - public InflectPair(String str, Object... pars) { - this.exp = str; - this.pars = pars; - } - - public static InflectPair pair(String str, Object... pars) { - return new InflectPair(str, pars); - } -} \ No newline at end of file diff --git a/src/test/java/bjc/inflexion/InflectionMLTest.java b/src/test/java/bjc/inflexion/InflectionMLTest.java index d94407f..f40070d 100644 --- a/src/test/java/bjc/inflexion/InflectionMLTest.java +++ b/src/test/java/bjc/inflexion/InflectionMLTest.java @@ -3,8 +3,8 @@ package bjc.inflexion; import org.junit.Test; import static org.junit.Assert.*; -import static bjc.inflexion.InflectPair.pair; +import static bjc.inflexion.InflectionMLTest.InflectPair.pair; import static bjc.inflexion.InflectionML.inflect; /** @@ -99,8 +99,97 @@ public class InflectionMLTest { pair("7 brethren found", 7, "brother")); } - private static void assertInflects(String exp, String real) { - assertEquals(exp, inflect(real)); + /** + * Test compiled inflection markup. + */ + @Test + public void testCompiledML() { + assertCInflects("test literal string", pair("test literal string")); + + assertCInflects("test $1 string", pair("test literal string", "literal"), + pair("test variable string", "variable")); + ////////////////////// + // Check # command // + ////////////////////// + + // Check general inflection + assertCInflects("<#:$1> $2 found", pair("0 indexes were found", 0, "were"), + pair("1 index was found", 1, "was"), pair("99 indexes were found", 99, "were")); + + // Check fancier inflection + assertCInflects("<#wnc:$1> $2 found", pair("no indexes were found", 0, "were"), + pair("one index was found", 1, "was"), pair("99 indexes were found", 99, "were")); + + // Check count inflection + assertCInflects("<#w20:$1> were found", pair("six indexes were found", 6), + pair("nineteen indexes were found", 19), pair("20 indexes were found", 20)); + + // Check 'n' option + assertCInflects("<#n:$1> ", pair("no results", 0), pair("7 results", 7)); + + // FIXME + // + // Adjust this to use for were/was when it is implemented + // Check general inflection + // + // For now, compiled inflection strings don't support variable options + assertCInflects("<#n:$1> $2 found", pair("no items were found", 0, "were")); + assertCInflects("<#ns:$1> $2 found", pair("no item was found", 0, "was")); + + // Check article picking + assertCInflects("<#a:$1> ", pair("a result", 1, "results"), pair("3 results", 3, "results"), + pair("an outcome", 1, "outcomes"), pair("7 outcomes", 7, "outcomes")); + + // Check 'w' option + assertCInflects("<#w:$1> ", pair("six results", 6), pair("ten results", 10), + pair("11 results", 11)); + // Check 'o' option + assertCInflects("<#o:$1> ", pair("6th result", 6), pair("11th result", 11), + pair("22nd result", 22)); + assertCInflects("<#ow:$1> ", pair("first result", 1), pair("sixth result", 6), + pair("22nd result", 22)); + assertCInflects("<#o15:$1> ", pair("6th result", 6), pair("11th result", 11), + pair("22 results", 22)); + + // Check 'f' option + assertCInflects("Found <#f:$1> ", pair("Found no matches", 0), pair("Found one match", 1), + pair("Found a couple of matches", 2), pair("Found a few matches", 4), + pair("Found several matches", 8), pair("Found many matches", 11)); + + // FIXME Don't require spaces to mark out directives. + // - Ben Culkin, 10/28/18 + assertCInflects("Searching for ....found <#f1:$1>", + pair("Searching for items ....found none", 0), + pair("Searching for items ....found one", 1), + pair("Searching for items ....found a couple", 2), + pair("Searching for items ....found a few", 4), + pair("Searching for items ....found several", 8), + pair("Searching for items ....found many", 11)); + + assertCInflects("Found <#fns:$1> ", pair("Found no match", 0), + pair("Found several matches", 7)); + assertCInflects("Found <#fa:$1> ", pair("Found a match", 1), + pair("Found several matches", 7)); + + // Check 'e' option + assertCInflects("Found <#e:$1> ", pair("Found no match", 0), pair("Found a match", 1), + pair("Found ten matches", 10), pair("Found 12 matches", 12)); + + // Check 'd' option + assertCInflects("<#d:$1> found", pair(" Match found", 1), pair(" Matches found", 2)); + + //////////////////////// + // Check N command // + /////////////////////// + + // Check 'c' option + assertCInflects("<#:$1> found", pair("7 maximums found", 7, "maximum"), + pair("7 formulas found", 7, "formula"), pair("7 corpuses found", 7, "corpuses"), + pair("7 brothers found", 7, "brothers")); + + assertCInflects("<#:$1> found", pair("7 maxima found", 7, "maximum"), + pair("7 formulae found", 7, "formula"), pair("7 corpora found", 7, "corpus"), + pair("7 brethren found", 7, "brother")); } private static void assertInflects(String real, InflectPair... pairs) { @@ -108,4 +197,26 @@ public class InflectionMLTest { assertEquals(pair.exp, inflect(String.format(real, pair.pars))); } } + + private static void assertCInflects(String real, InflectPair... pairs) { + InflectionString strang = new InflectionString(real); + + for (InflectPair pair : pairs) { + assertEquals(pair.exp, strang.inflect(pair.pars)); + } + } + + static class InflectPair { + public String exp; + public Object[] pars; + + public InflectPair(String str, Object... pars) { + this.exp = str; + this.pars = pars; + } + + public static InflectPair pair(String str, Object... pars) { + return new InflectPair(str, pars); + } + } } -- cgit v1.2.3