From 43742e0277216f241ea2ee9ecf9e95b3e0f6105d Mon Sep 17 00:00:00 2001 From: bep Date: Wed, 11 Mar 2015 01:29:18 +0100 Subject: [PATCH] Add some basic tests for doArithmetic We might have to take precision into account for floating point nubers ... at some point. --- tpl/template_test.go | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tpl/template_test.go b/tpl/template_test.go index b0d18f8f4..3ea57b592 100644 --- a/tpl/template_test.go +++ b/tpl/template_test.go @@ -107,6 +107,42 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b inte } } +func TestArethmic(t *testing.T) { + for i, this := range []struct { + a interface{} + b interface{} + op rune + expect interface{} + }{ + {1, 2, '+', int64(3)}, + {1, 2, '-', int64(-1)}, + {2, 2, '*', int64(4)}, + {4, 2, '/', int64(2)}, + {uint8(1), uint8(3), '+', uint64(4)}, + {uint8(3), uint8(2), '-', uint64(1)}, + {uint8(2), uint8(2), '*', uint64(4)}, + {uint16(4), uint8(2), '/', uint64(2)}, + {4, 2, 'ยค', false}, + } { + // TODO(bep): Take precision into account. + result, err := doArithmetic(this.a, this.b, this.op) + + if b, ok := this.expect.(bool); ok && !b { + if err == nil { + t.Errorf("[%d] doArethmic didn't return an expected error", i) + } + } else { + if err != nil { + t.Errorf("[%d] failed: %s", i, err) + continue + } + if !reflect.DeepEqual(result, this.expect) { + t.Errorf("[%d] doArethmic got %v (%T) but expected %v (%T)", i, result, result, this.expect, this.expect) + } + } + } +} + func TestMod(t *testing.T) { for i, this := range []struct { a interface{}