summaryrefslogtreecommitdiff
path: root/src/test/java/com
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter01Test.java4
-rw-r--r--src/test/java/com/seaofnodes/simple/Chapter03Test.java68
2 files changed, 70 insertions, 2 deletions
diff --git a/src/test/java/com/seaofnodes/simple/Chapter01Test.java b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
index 49edfb5b..311ca23b 100644
--- a/src/test/java/com/seaofnodes/simple/Chapter01Test.java
+++ b/src/test/java/com/seaofnodes/simple/Chapter01Test.java
@@ -40,7 +40,7 @@ public class Chapter01Test {
new Parser("ret").parse();
fail();
} catch( RuntimeException e ) {
- assertEquals("Syntax error, expected a statement: ret",e.getMessage());
+ assertEquals("Syntax error, expected =: ",e.getMessage());
}
}
@@ -82,7 +82,7 @@ public class Chapter01Test {
new Parser("return100").parse();
fail();
} catch( RuntimeException e ) {
- assertEquals("Syntax error, expected a statement: return100",e.getMessage());
+ assertEquals("Syntax error, expected =: ",e.getMessage());
}
}
diff --git a/src/test/java/com/seaofnodes/simple/Chapter03Test.java b/src/test/java/com/seaofnodes/simple/Chapter03Test.java
new file mode 100644
index 00000000..9a7dda5d
--- /dev/null
+++ b/src/test/java/com/seaofnodes/simple/Chapter03Test.java
@@ -0,0 +1,68 @@
+package com.seaofnodes.simple;
+
+import com.seaofnodes.simple.node.*;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class Chapter03Test {
+
+ @Test
+ public void testVarDecl() {
+ Parser parser = new Parser("int a=1; return a;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 1;", ret.print());
+ }
+
+ @Test
+ public void testVarAdd() {
+ Parser parser = new Parser("int a=1; int b=2; return a+b;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 3;", ret.print());
+ }
+
+ @Test
+ public void testVarScope() {
+ Parser parser = new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b; } return c;");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 4;", ret.print());
+ }
+
+ @Test
+ public void testVarDist() {
+ Parser parser = new Parser("int x0=1; int y0=2; int x1=3; int y1=4; return (x0-x1)*(x0-x1) + (y0-y1)*(y0-y1);");
+ ReturnNode ret = parser.parse();
+ assertEquals("return 8;", ret.print());
+ }
+
+ @Test
+ public void testSelfAssign() {
+ try {
+ new Parser("int a=a; return a;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Undefined name 'a'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testRedeclareVar() {
+ try {
+ new Parser("int a=1; int b=2; int c=0; int b=3; c=a+b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Redefining name 'b'",e.getMessage());
+ }
+ }
+
+ @Test
+ public void testBad1() {
+ try {
+ new Parser("int a=1; int b=2; int c=0; { int b=3; c=a+b;").parse();
+ fail();
+ } catch( RuntimeException e ) {
+ assertEquals("Syntax error, expected }: ",e.getMessage());
+ }
+ }
+
+}