lang: unification: Allow err string comparisons in tests
Let's improve our test infra to make it more capable. It's important to catch we failed for the _right_ reason so as to not mask the wrong errors.
This commit is contained in:
@@ -21,6 +21,7 @@ package lang
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/purpleidea/mgmt/lang/funcs"
|
"github.com/purpleidea/mgmt/lang/funcs"
|
||||||
@@ -37,6 +38,7 @@ func TestUnification1(t *testing.T) {
|
|||||||
fail bool
|
fail bool
|
||||||
expect map[interfaces.Expr]*types.Type
|
expect map[interfaces.Expr]*types.Type
|
||||||
experr error // expected error if fail == true (nil ignores it)
|
experr error // expected error if fail == true (nil ignores it)
|
||||||
|
experrstr string // expected error prefix
|
||||||
}
|
}
|
||||||
testCases := []test{}
|
testCases := []test{}
|
||||||
|
|
||||||
@@ -656,7 +658,7 @@ func TestUnification1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
names = append(names, tc.name)
|
names = append(names, tc.name)
|
||||||
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
|
t.Run(fmt.Sprintf("test #%d (%s)", index, tc.name), func(t *testing.T) {
|
||||||
ast, fail, expect, experr := tc.ast, tc.fail, tc.expect, tc.experr
|
ast, fail, expect, experr, experrstr := tc.ast, tc.fail, tc.expect, tc.experr, tc.experrstr
|
||||||
|
|
||||||
//str := strings.NewReader(code)
|
//str := strings.NewReader(code)
|
||||||
//ast, err := LexParse(str)
|
//ast, err := LexParse(str)
|
||||||
@@ -735,6 +737,14 @@ func TestUnification1(t *testing.T) {
|
|||||||
if fail && err != nil {
|
if fail && err != nil {
|
||||||
t.Logf("test #%d: err: %+v", index, err)
|
t.Logf("test #%d: err: %+v", index, err)
|
||||||
}
|
}
|
||||||
|
// test for specific error string!
|
||||||
|
if fail && experrstr != "" && !strings.HasPrefix(err.Error(), experrstr) {
|
||||||
|
t.Errorf("test #%d: FAIL", index)
|
||||||
|
t.Errorf("test #%d: expected fail, got wrong error", index)
|
||||||
|
t.Errorf("test #%d: got error: %s", index, err.Error())
|
||||||
|
t.Errorf("test #%d: exp error: %s", index, experrstr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if expect == nil { // test done early
|
if expect == nil { // test done early
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user