diff options
| author | Dibyendu Majumdar <dibyendumajumdar@users.noreply.github.com> | 2023-09-30 01:02:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-29 17:02:07 -0700 |
| commit | 3f2e9e84e137462272ae1bb592f47cecc362bdff (patch) | |
| tree | 2962b6a9e07cd135729f91e96ac4084dc03de272 /chapter02/src/test/java | |
| parent | 29def92f2ddbfca64ba827f116172fe98b2df491 (diff) | |
| download | Simple-3f2e9e84e137462272ae1bb592f47cecc362bdff.tar.gz Simple-3f2e9e84e137462272ae1bb592f47cecc362bdff.zip | |
Apply changes from chapter 2 to chapter 1 (#5)
Fix tests that check for exceptions
Diffstat (limited to 'chapter02/src/test/java')
| -rw-r--r-- | chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java b/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java index ba9ae448..846aff58 100644 --- a/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java +++ b/chapter02/src/test/java/com/seaofnodes/simple/Chapter02Test.java @@ -5,12 +5,18 @@ import com.seaofnodes.simple.node.Node; import com.seaofnodes.simple.node.ReturnNode; import com.seaofnodes.simple.node.StartNode; import com.seaofnodes.simple.type.TypeInteger; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; public class Chapter02Test { + @Rule + public ExpectedException exceptionRule = ExpectedException.none(); + @Test public void testChapter2ParseGrammar() { Node._disablePeephole = true; // disable peephole so we can observe full graph @@ -94,32 +100,34 @@ public class Chapter02Test { @Test public void testBad1() { - try { new Parser("ret").parse(); } - catch( RuntimeException e ) { assertEquals("Syntax error, expected return: ret",e.getMessage()); } + exceptionRule.expect(RuntimeException.class); + exceptionRule.expectMessage("Syntax error, expected return: ret"); + new Parser("ret").parse(); } @Test public void testBad2() { - try { new Parser("return 0123;").parse(); } - catch( RuntimeException e ) { assertEquals("Syntax error: integer values cannot start with '0'",e.getMessage()); } + exceptionRule.expect(RuntimeException.class); + exceptionRule.expectMessage("Syntax error: integer values cannot start with '0'"); + new Parser("return 0123;").parse(); } @Test - public void testBad3() { - try { new Parser("return --12;").parse(); } - catch( RuntimeException e ) { assertEquals("Syntax error, expected integer literal: -",e.getMessage()); } + public void testNotBad3() { + // this test used to fail in chapter 1 + assertEquals("return 12;", new Parser("return --12;").parse().print()); } @Test public void testBad4() { - try { new Parser("return 100").parse(); } - catch( RuntimeException e ) { assertTrue(e.getMessage().contains("Syntax error, expected ;:")); } + exceptionRule.expect(RuntimeException.class); + exceptionRule.expectMessage("Syntax error, expected ;:"); + new Parser("return 100").parse(); } - // Negative numbers require unary operator support that is not in scope @Test - public void testBad5() { - try { new Parser("return -100;").parse(); } - catch( RuntimeException e ) { assertEquals("Syntax error, expected integer literal: -", e.getMessage()); } + public void testNotBad5() { + // this test used to fail in chapter 1 + assertEquals("return -100;", new Parser("return -100;").parse().print()); } } |
