test/java/time/test/java/time/format/TestNumberParser.java

Print this page

        

*** 57,79 **** * 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.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_WEEK; import static java.time.temporal.ChronoField.DAY_OF_YEAR; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; import java.text.ParsePosition; - import java.time.format.DateTimeBuilder; - import java.time.temporal.TemporalField; import java.time.format.DateTimeFormatter; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /** --- 57,80 ---- * 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.DAY_OF_MONTH; import static java.time.temporal.ChronoField.DAY_OF_WEEK; import static java.time.temporal.ChronoField.DAY_OF_YEAR; import static java.time.temporal.ChronoField.MONTH_OF_YEAR; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertTrue; + import static org.testng.Assert.fail; import java.text.ParsePosition; import java.time.format.DateTimeFormatter; + import java.time.format.SignStyle; + import java.time.temporal.Queries; + import java.time.temporal.TemporalAccessor; + import java.time.temporal.TemporalField; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; /**
*** 92,103 **** } @Test(dataProvider="error") public void test_parse_error(TemporalField field, int min, int max, SignStyle style, String text, int pos, Class<?> expected) { try { ! getFormatter(field, min, max, style).parseToBuilder(text, new ParsePosition(pos)); ! assertTrue(false); } catch (RuntimeException ex) { assertTrue(expected.isInstance(ex)); } } --- 93,104 ---- } @Test(dataProvider="error") public void test_parse_error(TemporalField field, int min, int max, SignStyle style, String text, int pos, Class<?> expected) { try { ! getFormatter(field, min, max, style).parseUnresolved(text, new ParsePosition(pos)); ! fail(); } catch (RuntimeException ex) { assertTrue(expected.isInstance(ex)); } }
*** 168,184 **** DateTimeFormatter dtf = getFormatter(DAY_OF_MONTH, minWidth, maxWidth, signStyle); if (subsequentWidth > 0) { // hacky, to reserve space dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withSymbols(symbols); } ! DateTimeBuilder dtb = dtf.parseToBuilder(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertTrue(subsequentWidth >= 0); assertEquals(ppos.getIndex(), expectedPos + subsequentWidth); ! assertEquals(dtb.getLong(DAY_OF_MONTH), expectedValue); } } @Test(dataProvider="parseData") public void test_parse_textField(int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth, String text, int pos, int expectedPos, long expectedValue) { --- 169,187 ---- DateTimeFormatter dtf = getFormatter(DAY_OF_MONTH, minWidth, maxWidth, signStyle); if (subsequentWidth > 0) { // hacky, to reserve space dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withSymbols(symbols); } ! TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertTrue(subsequentWidth >= 0); assertEquals(ppos.getIndex(), expectedPos + subsequentWidth); ! assertEquals(parsed.getLong(DAY_OF_MONTH), expectedValue); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } @Test(dataProvider="parseData") public void test_parse_textField(int minWidth, int maxWidth, SignStyle signStyle, int subsequentWidth, String text, int pos, int expectedPos, long expectedValue) {
*** 186,202 **** DateTimeFormatter dtf = getFormatter(DAY_OF_WEEK, minWidth, maxWidth, signStyle); if (subsequentWidth > 0) { // hacky, to reserve space dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withSymbols(symbols); } ! DateTimeBuilder dtb = dtf.parseToBuilder(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertTrue(subsequentWidth >= 0); assertEquals(ppos.getIndex(), expectedPos + subsequentWidth); ! assertEquals(dtb.getLong(DAY_OF_WEEK), expectedValue); } } //----------------------------------------------------------------------- @DataProvider(name="parseSignsStrict") --- 189,207 ---- DateTimeFormatter dtf = getFormatter(DAY_OF_WEEK, minWidth, maxWidth, signStyle); if (subsequentWidth > 0) { // hacky, to reserve space dtf = builder.appendValue(DAY_OF_YEAR, subsequentWidth).toFormatter(locale).withSymbols(symbols); } ! TemporalAccessor parsed = dtf.parseUnresolved(text, ppos); if (ppos.getErrorIndex() != -1) { assertEquals(ppos.getErrorIndex(), expectedPos); } else { assertTrue(subsequentWidth >= 0); assertEquals(ppos.getIndex(), expectedPos + subsequentWidth); ! assertEquals(parsed.getLong(DAY_OF_WEEK), expectedValue); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } //----------------------------------------------------------------------- @DataProvider(name="parseSignsStrict")
*** 300,315 **** } @Test(dataProvider="parseSignsStrict") public void test_parseSignsStrict(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { ParsePosition pos = new ParsePosition(0); ! DateTimeBuilder dtb = getFormatter(DAY_OF_MONTH, min, max, style).parseToBuilder(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(dtb.getLong(DAY_OF_MONTH), (long)parseVal); } } //----------------------------------------------------------------------- @DataProvider(name="parseSignsLenient") --- 305,322 ---- } @Test(dataProvider="parseSignsStrict") public void test_parseSignsStrict(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { ParsePosition pos = new ParsePosition(0); ! TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } //----------------------------------------------------------------------- @DataProvider(name="parseSignsLenient")
*** 408,423 **** @Test(dataProvider="parseSignsLenient") public void test_parseSignsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { setStrict(false); ParsePosition pos = new ParsePosition(0); ! DateTimeBuilder dtb = getFormatter(DAY_OF_MONTH, min, max, style).parseToBuilder(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(dtb.getLong(DAY_OF_MONTH), (long)parseVal); } } //----------------------------------------------------------------------- @DataProvider(name="parseDigitsLenient") --- 415,432 ---- @Test(dataProvider="parseSignsLenient") public void test_parseSignsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { setStrict(false); ParsePosition pos = new ParsePosition(0); ! TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } //----------------------------------------------------------------------- @DataProvider(name="parseDigitsLenient")
*** 497,512 **** @Test(dataProvider="parseDigitsLenient") public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { setStrict(false); ParsePosition pos = new ParsePosition(0); ! DateTimeBuilder dtb = getFormatter(DAY_OF_MONTH, min, max, style).parseToBuilder(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(dtb.getLong(DAY_OF_MONTH), (long)parseVal); } } //----------------------------------------------------------------------- @DataProvider(name="parseDigitsAdjacentLenient") --- 506,523 ---- @Test(dataProvider="parseDigitsLenient") public void test_parseDigitsLenient(String input, int min, int max, SignStyle style, int parseLen, Integer parseVal) throws Exception { setStrict(false); ParsePosition pos = new ParsePosition(0); ! TemporalAccessor parsed = getFormatter(DAY_OF_MONTH, min, max, style).parseUnresolved(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(parsed.getLong(DAY_OF_MONTH), (long)parseVal); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } //----------------------------------------------------------------------- @DataProvider(name="parseDigitsAdjacentLenient")
*** 532,547 **** setStrict(false); ParsePosition pos = new ParsePosition(0); DateTimeFormatter f = builder .appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL) .appendValue(DAY_OF_MONTH, 2).toFormatter(locale).withSymbols(symbols); ! DateTimeBuilder dtb = f.parseToBuilder(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(dtb.getLong(MONTH_OF_YEAR), (long) parseMonth); ! assertEquals(dtb.getLong(DAY_OF_MONTH), (long) parsedDay); } } } --- 543,560 ---- setStrict(false); ParsePosition pos = new ParsePosition(0); DateTimeFormatter f = builder .appendValue(MONTH_OF_YEAR, 1, 2, SignStyle.NORMAL) .appendValue(DAY_OF_MONTH, 2).toFormatter(locale).withSymbols(symbols); ! TemporalAccessor parsed = f.parseUnresolved(input, pos); if (pos.getErrorIndex() != -1) { assertEquals(pos.getErrorIndex(), parseLen); } else { assertEquals(pos.getIndex(), parseLen); ! assertEquals(parsed.getLong(MONTH_OF_YEAR), (long) parseMonth); ! assertEquals(parsed.getLong(DAY_OF_MONTH), (long) parsedDay); ! assertEquals(parsed.query(Queries.chronology()), null); ! assertEquals(parsed.query(Queries.zoneId()), null); } } }