From 9812ed30bd15324d2a337c8d838786a93f9df69f Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 3 Nov 2019 13:33:32 +0100 Subject: Add duration parsing tests --- .../edu/mimuw/cloudatlas/model/ValueDuration.java | 6 +++ .../pl/edu/mimuw/cloudatlas/ValueDurationTest.java | 44 +++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java index bf9d0e0..444ce78 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java @@ -152,6 +152,12 @@ public class ValueDuration extends ValueSimple { return result; } + /** + * @param matcher A regex matcher + * @param group The name of a group matched by matcher that + * should have captured a number. + * @return The parsed number. + */ private static long parseLongGroup(Matcher matcher, String group) { return Long.parseLong(matcher.group(group)); } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java index 6bfc3a0..9db36eb 100644 --- a/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java +++ b/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java @@ -7,6 +7,48 @@ public class ValueDurationTest { @Test public void testGetValue() { ValueDuration v = new ValueDuration(42l); - assertEquals(v.getValue(), new Long(42l)); + assertEquals(new Long(42l), v.getValue()); + } + + @Test + public void testParseDurationMillisecond() { + ValueDuration v = new ValueDuration("+0 00:00:00.001"); + assertEquals(new Long(1l), v.getValue()); + } + + @Test + public void testParseDurationSecond() { + ValueDuration v = new ValueDuration("+0 00:00:01.000"); + assertEquals(new Long(1000l), v.getValue()); + } + + @Test + public void testParseDurationMinute() { + ValueDuration v = new ValueDuration("+0 00:01:00.000"); + assertEquals(new Long(60000l), v.getValue()); + } + + @Test + public void testParseDurationHour() { + ValueDuration v = new ValueDuration("+0 01:00:00.000"); + assertEquals(new Long(3600000l), v.getValue()); + } + + @Test + public void testParseDurationDay() { + ValueDuration v = new ValueDuration("+1 00:00:00.000"); + assertEquals(new Long(86400000l), v.getValue()); + } + + @Test + public void testParseDurationComplex() { + ValueDuration v = new ValueDuration("+0 01:59:40.000"); + assertEquals(new Long(7180000l), v.getValue()); + } + + @Test + public void testParseDurationNegative() { + ValueDuration v = new ValueDuration("-1 01:01:01.001"); + assertEquals(new Long(-90061001l), v.getValue()); } } -- cgit v1.2.3