Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added some parsing failures unit tests (asserting that does not parse…

… some bad code)
  • Loading branch information...
commit dd96fc940fd33d79179ae059de83caaf9edfd11a 1 parent 1a40aaf
@sthilaid authored
Showing with 48 additions and 18 deletions.
  1. +1 −1  dcpu/parser/dcpuLex.go
  2. +47 −17 dcpu/parser/dcpuParser_test.go
View
2  dcpu/parser/dcpuLex.go
@@ -175,5 +175,5 @@ loop:
}
func (DCPULex) Error(s string) {
- println("syntax error!")
+ panic("syntax error!")
}
View
64 dcpu/parser/dcpuParser_test.go
@@ -87,26 +87,56 @@ func TestParsing4(t *testing.T) {
}
}
-
-/*
-func TestParsing1(t *testing.T) {
+//-----------------------------------------------------------------------------
+// Parsing failure checks
+
+func TestParsingFailure0(t *testing.T) {
+ defer func() {
+ if panic := recover(); panic == nil {
+ t.Errorf("Should not parse code with invalid syntax (missing ',')...")
+ }
+ }()
+
lex := new(DCPULex)
- lex.code = "ADD [0xAAAA], 0xFF00 SET PUSH, [0xAAAA]"
+ // missing a "," after 'a'
+ lex.Init("SET PUSH 0x10")
yyParse(lex)
+}
- var expr0 DcpuExpression
- var expr1 DcpuExpression
- var ref0 DcpuReference
+func TestParsingFailure1(t *testing.T) {
+ defer func() {
+ if panic := recover(); panic == nil {
+ t.Errorf("Should not permit addition outside ref...")
+ }
+ }()
+
+ lex := new(DCPULex)
+ // not allowed to have addition outside a reference
+ lex.Init("SET 0x10+B, 0x10")
+ yyParse(lex)
+}
- expr0.inst = DcpuInstruction("ADD")
- ref0 = DcpuLitteral(0xaaaa)
- expr0.a = ref0
- expr0.b = DcpuLitteral(0xff00)
+func TestParsingFailure2(t *testing.T) {
+ defer func() {
+ if panic := recover(); panic == nil {
+ t.Errorf("Should not be allowed to referece special registers...")
+ }
+ }()
+
+ lex := new(DCPULex)
+ // not allowed to have addition outside a reference
+ lex.Init("SET [SP], 0x0")
+ yyParse(lex)
+}
- expr1.inst = DcpuInstruction("SET")
- //...
-
- ast := new(DcpuProgram)
- ast.expressions = []DcpuExpression{expr0, expr1}
+func TestParsingFailure3(t *testing.T) {
+ defer func() {
+ if panic := recover(); panic == nil {
+ t.Errorf("Should not parse invalid instructions...")
+ }
+ }()
+
+ lex := new(DCPULex)
+ lex.Init("SOT A, 0x0")
+ yyParse(lex)
}
-*/
Please sign in to comment.
Something went wrong with that request. Please try again.