summaryrefslogtreecommitdiff
path: root/chapter01/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'chapter01/src/test/java/com')
-rw-r--r--chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java45
1 files changed, 36 insertions, 9 deletions
diff --git a/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
index 9bc2fc1c..586c53de 100644
--- a/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
+++ b/chapter01/src/test/java/com/seaofnodes/simple/ParserTest.java
@@ -1,8 +1,8 @@
package com.seaofnodes.simple;
import com.seaofnodes.simple.node.*;
-import org.junit.Assert;
import org.junit.Test;
+import static org.junit.Assert.*;
public class ParserTest {
@@ -10,23 +10,50 @@ public class ParserTest {
public void testSimpleProgram() {
Parser parser = new Parser();
StartNode startNode = parser.parse("return 1;");
- Assert.assertNotNull(startNode);
- Assert.assertEquals(2, startNode.nOuts());
+ assertNotNull(startNode);
+ assertEquals(2, startNode.nOuts());
for (int i = 0; i < startNode.nOuts(); i++) {
switch( startNode.out(i) ) {
case ReturnNode ret:
- Assert.assertEquals(2, ret.nIns());
- Assert.assertEquals(startNode, ret.in(0));
- Assert.assertTrue(ret.in(1) instanceof ConstantNode);
+ assertEquals(2, ret.nIns());
+ assertEquals(startNode, ret.in(0));
+ assertTrue(ret.in(1) instanceof ConstantNode);
break;
case ConstantNode con:
- Assert.assertEquals(1, con._value);
- Assert.assertEquals(1, con.nIns());
- Assert.assertEquals(startNode, con.in(0));
+ assertEquals(1, con._value);
+ assertEquals(1, con.nIns());
+ assertEquals(startNode, con.in(0));
break;
default:
throw new IllegalStateException();
}
}
}
+
+ @Test
+ public void testNegative() {
+ Parser parser = new Parser();
+ StartNode start = parser.parse("return -123;");
+ for( Node use : start._outputs )
+ if( use instanceof ConstantNode con )
+ assertEquals(-123,con._value);
+ }
+
+ @Test
+ public void testBad1() {
+ try { new Parser().parse("ret"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected return: ret",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad2() {
+ try { new Parser().parse("return 0123;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected nested expr or integer literal: 0123",e.getMessage()); }
+ }
+
+ @Test
+ public void testBad3() {
+ try { new Parser().parse("return --12;"); }
+ catch( RuntimeException e ) { assertEquals("syntax error, expected ( got --: --",e.getMessage()); }
+ }
}