Skip to content

Commit 83f0814

Browse files
committed
add Reduce tests
1 parent c1954f2 commit 83f0814

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

reducer.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dscope
22

33
import (
4+
"errors"
45
"fmt"
56
"reflect"
67
"strings"
@@ -12,9 +13,11 @@ type Reducer interface {
1213

1314
var reducerType = reflect.TypeOf((*Reducer)(nil)).Elem()
1415

16+
var ErrNoValues = errors.New("no values")
17+
1518
func Reduce(vs []reflect.Value) reflect.Value {
1619
if len(vs) == 0 {
17-
panic("no values")
20+
panic(ErrNoValues)
1821
}
1922

2023
t := vs[0].Type()
@@ -68,7 +71,7 @@ func Reduce(vs []reflect.Value) reflect.Value {
6871
ret = reflect.New(t).Elem()
6972
ret.SetInt(i)
7073

71-
default:
74+
default: // NOCOVER
7275
panic(fmt.Errorf("don't know how to reduce %v", t))
7376
}
7477

reducer_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package dscope
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestReduceNil(t *testing.T) {
9+
func() {
10+
defer func() {
11+
p := recover()
12+
if p == nil {
13+
t.Fatal("should panic")
14+
}
15+
err, ok := p.(error)
16+
if !ok {
17+
t.Fatal()
18+
}
19+
if !is(err, ErrNoValues) {
20+
t.Fatal()
21+
}
22+
}()
23+
Reduce(nil)
24+
}()
25+
}
26+
27+
func TestReduceMap(t *testing.T) {
28+
m := Reduce([]reflect.Value{
29+
reflect.ValueOf(map[int]int{
30+
1: 2,
31+
}),
32+
reflect.ValueOf(map[int]int{
33+
3: 4,
34+
}),
35+
}).Interface().(map[int]int)
36+
if len(m) != 2 {
37+
t.Fatal()
38+
}
39+
if m[1] != 2 {
40+
t.Fatal()
41+
}
42+
if m[3] != 4 {
43+
t.Fatal()
44+
}
45+
}

0 commit comments

Comments
 (0)