shithub: hugo

Download patch

ref: 43742e0277216f241ea2ee9ecf9e95b3e0f6105d
parent: b9b70fb6b085e5141078bee80f342d2a9fdc2f5e
author: bep <[email protected]>
date: Tue Mar 10 21:29:18 EDT 2015

Add some basic tests for doArithmetic

We might have to take precision into account for floating point nubers ... at some point.

--- a/tpl/template_test.go
+++ b/tpl/template_test.go
@@ -107,6 +107,42 @@
 	}
 }
 
+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{}