Skip to content

Commit

Permalink
Add three arithmetic functions tests
Browse files Browse the repository at this point in the history
  • Loading branch information
binoue committed Feb 14, 2019
1 parent 0bdfa3b commit 7eef4ae
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 10 deletions.
12 changes: 8 additions & 4 deletions template_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

var (
errNotInt = errors.New("not an integer")
errNotFloat = errors.New("not a float")
errNotInt = errors.New("not an integer")
errNotFloat = errors.New("not a float")
errZeroDivision = errors.New("zero division")
)

func jsonFunc(i interface{}) (string, error) {
Expand Down Expand Up @@ -128,12 +129,15 @@ func mulFunc(a, b interface{}) (interface{}, error) {

func divFunc(a, b interface{}) (interface{}, error) {
ia, ib, err := getAsInts(a, b)
if err == nil {
if err == nil && ib != 0 {
return ia / ib, nil
}
fa, fb, err := getAsFloats(a, b)
if err == nil {
if err == nil && fb != 0 {
return fa / fb, nil
}
if fb == 0 {
return nil, errZeroDivision
}
return nil, err
}
130 changes: 124 additions & 6 deletions template_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (

func testAddFunc(t *testing.T) {
cases := []struct {
name string
a interface{}
b interface{}
expect interface{}
err bool
name string
a interface{}
b interface{}
expect interface{}
wantedErr bool
}{
{"addInt2", int(3), int(-1), int64(2), false},
{"addInt8Int16", int8(3), int16(-1), int64(2), false},
Expand All @@ -33,7 +33,122 @@ func testAddFunc(t *testing.T) {
t.Parallel()
actual, err := addFunc(tt.a, tt.b)
if err != nil {
if !tt.err {
if !tt.wantedErr {
t.Error("unexpected error:", err)
}
return
}

if !cmp.Equal(tt.expect, actual, testCmp) {
t.Error("unexpected result:", cmp.Diff(tt.expect, actual, testCmp))
}
})
}
}

func testSubFunc(t *testing.T) {
cases := []struct {
name string
a interface{}
b interface{}
expect interface{}
wantedErr bool
}{
{"subInt2", int(3), int(-1), int64(4), false},
{"subInt8Int16", int8(3), int16(-1), int64(4), false},
{"subInt32Int64", int32(3), int64(-1), int64(4), false},
{"subUintUint8", uint(3), uint8(1), int64(2), false},
{"subUint16Uint32", uint16(3), uint32(1), int64(2), false},
{"subUint32Uint64", uint32(3), uint64(1), int64(2), false},
{"subIntFloat32", int(3), float32(-1.3), float64(4.3), false},
{"subFloat64Uint", float64(3.3), uint(1), float64(2.3), false},
{"subInt64String", int64(-1), "", 0, true},
{"subFloat64String", float64(1.7), "", 0, true},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
actual, err := subFunc(tt.a, tt.b)
if err != nil {
if !tt.wantedErr {
t.Error("unexpected error:", err)
}
return
}

if !cmp.Equal(tt.expect, actual, testCmp) {
t.Error("unexpected result:", cmp.Diff(tt.expect, actual, testCmp))
}
})
}
}

func testMulFunc(t *testing.T) {
cases := []struct {
name string
a interface{}
b interface{}
expect interface{}
wantedErr bool
}{
{"mulInt2", int(3), int(-1), int64(-3), false},
{"mulInt8Int16", int8(3), int16(-1), int64(-3), false},
{"mulInt32Int64", int32(3), int64(-1), int64(-3), false},
{"mulUintUint8", uint(3), uint8(1), int64(3), false},
{"mulUint16Uint32", uint16(3), uint32(1), int64(3), false},
{"mulUint32Uint64", uint32(3), uint64(1), int64(3), false},
{"mulIntFloat32", int(3), float32(-1.3), float64(-3.9), false},
{"mulFloat64Uint", float64(3.3), uint(1), float64(3.3), false},
{"mulInt64String", int64(-1), "", 0, true},
{"mulFloat64String", float64(1.7), "", 0, true},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
actual, err := mulFunc(tt.a, tt.b)
if err != nil {
if !tt.wantedErr {
t.Error("unexpected error:", err)
}
return
}

if !cmp.Equal(tt.expect, actual, testCmp) {
t.Error("unexpected result:", cmp.Diff(tt.expect, actual, testCmp))
}
})
}
}

func testDivFunc(t *testing.T) {
cases := []struct {
name string
a interface{}
b interface{}
expect interface{}
wantedErr bool
}{
{"divInt2", int(4), int(2), int64(2), false},
{"divInt8Int16", int8(4), int16(2), int64(2), false},
{"divInt32Int64", int32(4), int64(2), int64(2), false},
{"divUintUint8", uint(4), uint8(2), int64(2), false},
{"divUint16Uint32", uint16(4), uint32(2), int64(2), false},
{"divUint32Uint64", uint32(4), uint64(2), int64(2), false},
{"divFloat32Int", float32(3.9), int(3), float64(1.3), false},
{"divFloat64Uint", float64(3.9), uint(3), float64(1.3), false},
{"divIntInt64", int(10), int64(0), 0, true},
{"divInt64String", int64(-1), "", 0, true},
{"divFloat64String", float64(1.7), "", 0, true},
}
for _, tt := range cases {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
actual, err := divFunc(tt.a, tt.b)
if err != nil {
if !tt.wantedErr {
t.Error("unexpected error:", err)
}
return
Expand All @@ -52,4 +167,7 @@ var testCmp = cmp.Comparer(func(a, b float64) bool {

func TestTemplateFuncs(t *testing.T) {
t.Run("add", testAddFunc)
t.Run("sub", testSubFunc)
t.Run("mul", testMulFunc)
t.Run("div", testDivFunc)
}

0 comments on commit 7eef4ae

Please sign in to comment.