summaryrefslogtreecommitdiffstats
path: root/tpl/template_test.go
diff options
context:
space:
mode:
authorbep <bjorn.erik.pedersen@gmail.com>2015-01-20 16:38:08 +0100
committerbep <bjorn.erik.pedersen@gmail.com>2015-01-20 16:38:23 +0100
commitf77f2a9b402a1d4650d6afada3fdbb7ac619d42a (patch)
treeb2388d8e6127184e2d1d1dffef583b86eafa94d1 /tpl/template_test.go
parent1348caa6de1c41865cbe9f37cb0048395cf21279 (diff)
Refactor TestGt to also include Lt, Ge, Le, Eq and Ne
Diffstat (limited to 'tpl/template_test.go')
-rw-r--r--tpl/template_test.go99
1 files changed, 78 insertions, 21 deletions
diff --git a/tpl/template_test.go b/tpl/template_test.go
index a96337094..2dcf0b12d 100644
--- a/tpl/template_test.go
+++ b/tpl/template_test.go
@@ -6,39 +6,96 @@ import (
"fmt"
"github.com/stretchr/testify/assert"
"html/template"
+ "path"
"reflect"
+ "runtime"
"testing"
)
type tstNoStringer struct {
}
-func TestGt(t *testing.T) {
+type tstCompareType int
+
+const (
+ tstEq tstCompareType = iota
+ tstNe
+ tstGt
+ tstGe
+ tstLt
+ tstLe
+)
+
+func tstIsEq(tp tstCompareType) bool {
+ return tp == tstEq || tp == tstGe || tp == tstLe
+}
+
+func tstIsGt(tp tstCompareType) bool {
+ return tp == tstGt || tp == tstGe
+}
+
+func tstIsLt(tp tstCompareType) bool {
+ return tp == tstLt || tp == tstLe
+}
+
+func TestCompare(t *testing.T) {
+ for _, this := range []struct {
+ tstCompareType
+ funcUnderTest func(a, b interface{}) bool
+ }{
+ {tstGt, Gt},
+ {tstLt, Lt},
+ {tstGe, Ge},
+ {tstLe, Le},
+ {tstEq, Eq},
+ {tstNe, Ne},
+ } {
+ doTestCompare(t, this.tstCompareType, this.funcUnderTest)
+ }
+
+}
+
+func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b interface{}) bool) {
for i, this := range []struct {
- left interface{}
- right interface{}
- leftShouldWin bool
+ left interface{}
+ right interface{}
+ expectIndicator int
}{
- {5, 8, false},
- {8, 5, true},
- {5, 5, false},
- {-2, 1, false},
- {2, -5, true},
- {0.0, 1.23, false},
- {1.23, 0.0, true},
- {"8", "5", true},
- {"5", "0001", true},
- {[]int{100, 99}, []int{1, 2, 3, 4}, false},
+ {5, 8, -1},
+ {8, 5, 1},
+ {5, 5, 0},
+ {-2, 1, -1},
+ {2, -5, 1},
+ {0.0, 1.23, -1},
+ {1.23, 0.0, 1},
+ {"5", "5", 0},
+ {"8", "5", 1},
+ {"5", "0001", 1},
+ {[]int{100, 99}, []int{1, 2, 3, 4}, -1},
} {
- leftIsBigger := Gt(this.left, this.right)
- if leftIsBigger != this.leftShouldWin {
- var which string
- if leftIsBigger {
- which = "expected right to be bigger, but left was"
+ result := funcUnderTest(this.left, this.right)
+ success := false
+
+ if this.expectIndicator == 0 {
+ if tstIsEq(tp) {
+ success = result
} else {
- which = "expected left to be bigger, but right was"
+ success = !result
}
- t.Errorf("[%d] %v compared to %v: %s", i, this.left, this.right, which)
+ }
+
+ if this.expectIndicator < 0 {
+ success = result && (tstIsLt(tp) || tp == tstNe)
+ success = success || (!result && !tstIsLt(tp))
+ }
+
+ if this.expectIndicator > 0 {
+ success = result && (tstIsGt(tp) || tp == tstNe)
+ success = success || (!result && (!tstIsGt(tp) || tp != tstNe))
+ }
+
+ if !success {
+ t.Errorf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), this.left, this.right, result)
}
}
}