Skip to content

Commit

Permalink
added some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Griesbacher committed Nov 5, 2015
1 parent ffb007c commit d66b3b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ var ParseStringData = []struct {
//{"e[`42`] == true", true, nil },
{`_["k2"] == 10 && _["k2"] == 10`, true, nil},
{`_["k2"] == 10 || _["k2"] == 11`, true, nil},
{`(1==1)`, true, nil},
{`(1==1) && (2==2)`, true, nil},
{`10 == "10"`, false, errors.New("string and int compare")},
{`10 &^ "\y"`, false, errors.New("not a valid regex")},
{`10 &^ "(d"`, false, errors.New("not a valid regex")},
{`1,1 == 1`, false, errors.New("not a valid float")},
{`1 == 1,1`, false, errors.New("not a valid float")},
{`->`, false, errors.New("valid go but not allowed")},
{`"a" < "a"`, false, errors.New("string operator not allowed")},
{`1 &^ 1`, false, errors.New("number operator not allowed")},
{`_["1"] == 1`, false, errors.New("number operator not allowed")},
}

var b = []byte(`{
Expand All @@ -90,6 +96,7 @@ func TestParseString(t *testing.T) {
}
}
}

func TestParseStringChannel(t *testing.T) {
t.Parallel()
_, w, _ := os.Pipe()
Expand Down Expand Up @@ -120,7 +127,6 @@ func TestParseStringChannel(t *testing.T) {
}
}

//TestPrintNode is a dummy test because the PrintNode is just for debugging
func TestPrintNode(t *testing.T) {
t.Parallel()
oldStdout := os.Stdout
Expand All @@ -134,7 +140,6 @@ func TestPrintNode(t *testing.T) {
os.Stdout = oldStdout
}

//TestPrintAst is a dummy test because the PrintNode is just for debugging
func TestPrintAst(t *testing.T) {
t.Parallel()
oldStdout := os.Stdout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (v conditionVisitor) handleNode(node ast.Node) {
op := v.store.popFromStack().(*ast.BinaryExpr).Op.String()
v.store.appendToResult(v.compareBasicLit(nstack, n, op))
case *ast.IndexExpr, *ast.Ident:
panic("should not happen")
v.store.err = errors.New("should not happen")
default:
v.store.appendToStack(node)
}
Expand Down
26 changes: 26 additions & 0 deletions RuleSystem/RuleFileParser/ConditionParser/conditionVisitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ConditionParser

import (
"testing"
"go/ast"
"go/token"
)

func TestHandleNode(t *testing.T) {
t.Parallel()
v := conditionVisitor{store:&dataStore{result:[]bool{}, stack:[]ast.Node{&ast.IndexExpr{}, &ast.Ident{}}}}
if v.handleNode(&ast.BasicLit{}); v.store.err == nil {
t.Error("This should not happen")
}
if v.handleNode(&ast.Comment{}); v.store.err == nil {
t.Error("This should not happen")
}
}

func TestCompareBasicLit(t *testing.T) {
t.Parallel()
v := conditionVisitor{store:&dataStore{}}
if v.compareBasicLit(&ast.BasicLit{Kind:token.COMMENT}, &ast.BasicLit{Kind:token.COMMENT}, "=="); v.store.err == nil {
t.Error("compareBasicLit should not compare these tokens")
}
}

0 comments on commit d66b3b1

Please sign in to comment.