Skip to content

Commit

Permalink
Improve negative tests in CronTabTest and LabelExpressionTest (je…
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Oct 26, 2022
1 parent e7df5eb commit a3d1e0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 7 additions & 3 deletions core/src/test/java/hudson/scheduler/CronTabTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ public int next(int n) {
compare(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 2), new CronTab("H(0-3)/4 * * * *", Hash.from("junk")).ceil(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 0)));
compare(new GregorianCalendar(2013, Calendar.MARCH, 21, 1, 2), new CronTab("H(0-3)/4 * * * *", Hash.from("junk")).ceil(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 5)));

assertThrows(ANTLRException.class, () -> compare(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 0), new CronTab("H(0-3)/15 * * * *", Hash.from("junk")).ceil(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 0))));
ANTLRException e = assertThrows(ANTLRException.class, () ->
compare(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 0), new CronTab("H(0-3)/15 * * * *", Hash.from("junk")).ceil(new GregorianCalendar(2013, Calendar.MARCH, 21, 0, 0))));
assertEquals("line 1:8: 15 is an invalid value. Must be within 1 and 4", e.toString());
}

@Test public void repeatedHash() throws Exception {
Expand All @@ -285,11 +287,13 @@ public int next(int n) {
}

@Test public void rangeBoundsCheckFailHour() {
assertThrows(ANTLRException.class, () -> new CronTab("H H(12-24) * * *"));
ANTLRException e = assertThrows(ANTLRException.class, () -> new CronTab("H H(12-24) * * *"));
assertEquals("line 1:10: 24 is an invalid value. Must be within 0 and 23", e.toString());
}

@Test public void rangeBoundsCheckFailMinute() {
assertThrows(ANTLRException.class, () -> new CronTab("H(33-66) * * * *"));
ANTLRException e = assertThrows(ANTLRException.class, () -> new CronTab("H(33-66) * * * *"));
assertEquals("line 1:8: 66 is an invalid value. Must be within 0 and 59", e.toString());
}

@Issue("JENKINS-9283")
Expand Down
23 changes: 12 additions & 11 deletions test/src/test/java/hudson/model/labels/LabelExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ private void parseAndVerify(String expected, String expr) throws ANTLRException

@Test
public void parserError() {
parseShouldFail("foo bar");
parseShouldFail("foo (bar)");
parseShouldFail("foo(bar)");
parseShouldFail("a <- b");
parseShouldFail("a -< b");
parseShouldFail("a - b");
parseShouldFail("->");
parseShouldFail("-<");
parseShouldFail("-!");
parseShouldFail("foo bar", "line 1:5: unexpected token: bar");
parseShouldFail("foo (bar)", "line 1:5: unexpected token: (");
parseShouldFail("foo(bar)", "line 1:4: unexpected token: (");
parseShouldFail("a <- b", "line 1:5: expecting '>', found ' '");
parseShouldFail("a -< b", "line 1:3: unexpected token: -");
parseShouldFail("a - b", "line 1:3: unexpected token: -");
parseShouldFail("->", "line 1:1: unexpected token: ->");
parseShouldFail("-<", "line 1:3: expecting '-', found '<EOF>'");
parseShouldFail("-!", "line 1:2: unexpected token: !");
}

@Test
Expand Down Expand Up @@ -344,11 +344,12 @@ public void expression_and_withoutSpaces() throws Exception {
assertThat(label, instanceOf(LabelExpression.And.class));
}

private void parseShouldFail(String expr) {
assertThrows(
private void parseShouldFail(String expr, String message) {
ANTLRException e = assertThrows(
expr + " should fail to parse",
ANTLRException.class,
() -> Label.parseExpression(expr));
assertEquals(message, e.toString());
}

@Test
Expand Down

0 comments on commit a3d1e0c

Please sign in to comment.