File tree 2 files changed +50
-2
lines changed
2 files changed +50
-2
lines changed Original file line number Diff line number Diff line change 1
1
package dscope
2
2
3
3
import (
4
+ "errors"
4
5
"fmt"
5
6
"reflect"
6
7
"strings"
@@ -12,9 +13,11 @@ type Reducer interface {
12
13
13
14
var reducerType = reflect .TypeOf ((* Reducer )(nil )).Elem ()
14
15
16
+ var ErrNoValues = errors .New ("no values" )
17
+
15
18
func Reduce (vs []reflect.Value ) reflect.Value {
16
19
if len (vs ) == 0 {
17
- panic ("no values" )
20
+ panic (ErrNoValues )
18
21
}
19
22
20
23
t := vs [0 ].Type ()
@@ -68,7 +71,7 @@ func Reduce(vs []reflect.Value) reflect.Value {
68
71
ret = reflect .New (t ).Elem ()
69
72
ret .SetInt (i )
70
73
71
- default :
74
+ default : // NOCOVER
72
75
panic (fmt .Errorf ("don't know how to reduce %v" , t ))
73
76
}
74
77
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments