test/java/time/test/java/time/format/TestFractionPrinterParser.java

Print this page

        

*** 57,83 **** * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package test.java.time.format; - import java.time.format.*; - import static java.time.temporal.ChronoField.NANO_OF_SECOND; import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.text.ParsePosition; import java.time.DateTimeException; import java.time.LocalTime; ! import java.time.format.DateTimeBuilder; import java.time.temporal.TemporalField; - import test.java.time.temporal.MockFieldValue; - import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** * Test FractionPrinterParser. */ @Test(groups={"implementation"}) --- 57,81 ---- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package test.java.time.format; import static java.time.temporal.ChronoField.NANO_OF_SECOND; import static java.time.temporal.ChronoField.SECOND_OF_MINUTE; import static org.testng.Assert.assertEquals; import static org.testng.Assert.fail; import java.text.ParsePosition; import java.time.DateTimeException; import java.time.LocalTime; ! import java.time.format.DateTimeFormatter; ! import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; + import test.java.time.temporal.MockFieldValue; /** * Test FractionPrinterParser. */ @Test(groups={"implementation"})
*** 90,105 **** //----------------------------------------------------------------------- // print //----------------------------------------------------------------------- @Test(expectedExceptions=DateTimeException.class) public void test_print_emptyCalendrical() throws Exception { ! getFormatter(NANO_OF_SECOND, 0, 9, true).printTo(EMPTY_DTA, buf); } public void test_print_append() throws Exception { buf.append("EXISTING"); ! getFormatter(NANO_OF_SECOND, 0, 9, true).printTo(LocalTime.of(12, 30, 40, 3), buf); assertEquals(buf.toString(), "EXISTING.000000003"); } //----------------------------------------------------------------------- @DataProvider(name="Nanos") --- 88,103 ---- //----------------------------------------------------------------------- // print //----------------------------------------------------------------------- @Test(expectedExceptions=DateTimeException.class) public void test_print_emptyCalendrical() throws Exception { ! getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(EMPTY_DTA, buf); } public void test_print_append() throws Exception { buf.append("EXISTING"); ! getFormatter(NANO_OF_SECOND, 0, 9, true).formatTo(LocalTime.of(12, 30, 40, 3), buf); assertEquals(buf.toString(), "EXISTING.000000003"); } //----------------------------------------------------------------------- @DataProvider(name="Nanos")
*** 178,197 **** }; } @Test(dataProvider="Nanos") public void test_print_nanos(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).printTo(new MockFieldValue(NANO_OF_SECOND, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), result); } @Test(dataProvider="Nanos") public void test_print_nanos_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(NANO_OF_SECOND, minWidth, maxWidth, false).printTo(new MockFieldValue(NANO_OF_SECOND, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result)); } --- 176,195 ---- }; } @Test(dataProvider="Nanos") public void test_print_nanos(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).formatTo(new MockFieldValue(NANO_OF_SECOND, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), result); } @Test(dataProvider="Nanos") public void test_print_nanos_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(NANO_OF_SECOND, minWidth, maxWidth, false).formatTo(new MockFieldValue(NANO_OF_SECOND, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result)); }
*** 220,239 **** }; } @Test(dataProvider="Seconds") public void test_print_seconds(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).printTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), result); } @Test(dataProvider="Seconds") public void test_print_seconds_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, false).printTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result)); } --- 218,237 ---- }; } @Test(dataProvider="Seconds") public void test_print_seconds(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).formatTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), result); } @Test(dataProvider="Seconds") public void test_print_seconds_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ! getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, false).formatTo(new MockFieldValue(SECOND_OF_MINUTE, value), buf); if (result == null) { fail("Expected exception"); } assertEquals(buf.toString(), (result.startsWith(".") ? result.substring(1) : result)); }
*** 243,273 **** //----------------------------------------------------------------------- @Test(dataProvider="Nanos") public void test_reverseParse(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); int expectedValue = fixParsedValue(maxWidth, value); ! DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(result, pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } @Test(dataProvider="Nanos") public void test_reverseParse_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition((result.startsWith(".") ? 1 : 0)); ! DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, false).parseToBuilder(result, pos); assertEquals(pos.getIndex(), result.length()); int expectedValue = fixParsedValue(maxWidth, value); ! assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } @Test(dataProvider="Nanos") public void test_reverseParse_followedByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); int expectedValue = fixParsedValue(maxWidth, value); ! DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(result + " ", pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } // @Test(dataProvider="Nanos") // public void test_reverseParse_followedByNonDigit_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { // FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, minWidth, maxWidth, false); --- 241,271 ---- //----------------------------------------------------------------------- @Test(dataProvider="Nanos") public void test_reverseParse(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); int expectedValue = fixParsedValue(maxWidth, value); ! TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseUnresolved(result, pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } @Test(dataProvider="Nanos") public void test_reverseParse_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition((result.startsWith(".") ? 1 : 0)); ! TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, false).parseUnresolved(result, pos); assertEquals(pos.getIndex(), result.length()); int expectedValue = fixParsedValue(maxWidth, value); ! assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } @Test(dataProvider="Nanos") public void test_reverseParse_followedByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); int expectedValue = fixParsedValue(maxWidth, value); ! TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseUnresolved(result + " ", pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } // @Test(dataProvider="Nanos") // public void test_reverseParse_followedByNonDigit_noDecimalPoint(int minWidth, int maxWidth, int value, String result) throws Exception { // FractionPrinterParser pp = new FractionPrinterParser(NANO_OF_SECOND, minWidth, maxWidth, false);
*** 279,291 **** @Test(dataProvider="Nanos") public void test_reverseParse_preceededByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(1); int expectedValue = fixParsedValue(maxWidth, value); ! DateTimeBuilder dtb = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseToBuilder(" " + result, pos); assertEquals(pos.getIndex(), result.length() + 1); ! assertParsed(dtb, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } private int fixParsedValue(int maxWidth, int value) { if (maxWidth < 9) { int power = (int) Math.pow(10, (9 - maxWidth)); --- 277,289 ---- @Test(dataProvider="Nanos") public void test_reverseParse_preceededByNonDigit(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(1); int expectedValue = fixParsedValue(maxWidth, value); ! TemporalAccessor parsed = getFormatter(NANO_OF_SECOND, minWidth, maxWidth, true).parseUnresolved(" " + result, pos); assertEquals(pos.getIndex(), result.length() + 1); ! assertParsed(parsed, NANO_OF_SECOND, value == 0 && minWidth == 0 ? null : (long) expectedValue); } private int fixParsedValue(int maxWidth, int value) { if (maxWidth < 9) { int power = (int) Math.pow(10, (9 - maxWidth));
*** 295,314 **** } @Test(dataProvider="Seconds") public void test_reverseParse_seconds(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); ! DateTimeBuilder dtb = getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).parseToBuilder(result, pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(dtb, SECOND_OF_MINUTE, value == 0 && minWidth == 0 ? null : (long) value); } ! private void assertParsed(DateTimeBuilder dtb, TemporalField field, Long value) { if (value == null) { ! assertEquals(dtb.containsFieldValue(field), false); } else { ! assertEquals(dtb.getLong(field), (long)value); } } //----------------------------------------------------------------------- @DataProvider(name="ParseNothing") --- 293,313 ---- } @Test(dataProvider="Seconds") public void test_reverseParse_seconds(int minWidth, int maxWidth, int value, String result) throws Exception { ParsePosition pos = new ParsePosition(0); ! TemporalAccessor parsed = getFormatter(SECOND_OF_MINUTE, minWidth, maxWidth, true).parseUnresolved(result, pos); assertEquals(pos.getIndex(), result.length()); ! assertParsed(parsed, SECOND_OF_MINUTE, value == 0 && minWidth == 0 ? null : (long) value); } ! private void assertParsed(TemporalAccessor parsed, TemporalField field, Long value) { if (value == null) { ! assertEquals(parsed.isSupported(field), false); } else { ! assertEquals(parsed.isSupported(field), true); ! assertEquals(parsed.getLong(field), (long) value); } } //----------------------------------------------------------------------- @DataProvider(name="ParseNothing")
*** 325,336 **** } @Test(dataProvider = "ParseNothing") public void test_parse_nothing(TemporalField field, int min, int max, boolean decimalPoint, String text, int pos, int expected) { ParsePosition ppos = new ParsePosition(pos); ! DateTimeBuilder dtb = getFormatter(field, min, max, decimalPoint).parseToBuilder(text, ppos); assertEquals(ppos.getErrorIndex(), expected); } //----------------------------------------------------------------------- public void test_toString() throws Exception { assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, true).toString(), "Fraction(NanoOfSecond,3,6,DecimalPoint)"); --- 324,336 ---- } @Test(dataProvider = "ParseNothing") public void test_parse_nothing(TemporalField field, int min, int max, boolean decimalPoint, String text, int pos, int expected) { ParsePosition ppos = new ParsePosition(pos); ! TemporalAccessor parsed = getFormatter(field, min, max, decimalPoint).parseUnresolved(text, ppos); assertEquals(ppos.getErrorIndex(), expected); + assertEquals(parsed, null); } //----------------------------------------------------------------------- public void test_toString() throws Exception { assertEquals(getFormatter(NANO_OF_SECOND, 3, 6, true).toString(), "Fraction(NanoOfSecond,3,6,DecimalPoint)");