-
Notifications
You must be signed in to change notification settings - Fork 3
/
maps_test.go
128 lines (105 loc) · 3.05 KB
/
maps_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package goval_test
import (
"context"
"errors"
"reflect"
"testing"
"github.com/pkg-id/goval"
)
func TestMap(t *testing.T) {
ctx := context.Background()
err := goval.Map[string, string]().Validate(ctx, nil)
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestMapValidator_Required(t *testing.T) {
ctx := context.Background()
err := goval.Map[string, string]().Required().Validate(ctx, map[string]string{})
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.MapRequired) {
t.Errorf("expect the error code: %v; got error code: %v", goval.MapRequired, exp.Code)
}
if exp.Args != nil {
t.Errorf("expect the error args is empty; got error args: %v", exp.Args)
}
err = goval.Map[string, string]().Required().Validate(ctx, map[string]string{"key": "value"})
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestMapValidator_Min(t *testing.T) {
ctx := context.Background()
val := map[string]string{"key": "value"}
err := goval.Map[string, string]().Min(2).Validate(ctx, val)
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.MapMin) {
t.Errorf("expect the error code: %v; got error code: %v", goval.MapMin, exp.Code)
}
args := []any{2}
if !reflect.DeepEqual(exp.Args, args) {
t.Errorf("expect the error args: %v; got error args: %v", args, exp.Args)
}
err = goval.Map[string, string]().Min(1).Validate(ctx, val)
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestMapValidator_Max(t *testing.T) {
ctx := context.Background()
val := map[string]string{
"key1": "value1",
"key2": "value2",
"key3": "value3",
}
err := goval.Map[string, string]().Max(2).Validate(ctx, val)
if err == nil {
t.Errorf("expect error; got no error")
}
var exp *goval.RuleError
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if !exp.Code.Equal(goval.MapMax) {
t.Errorf("expect the error code: %v; got error code: %v", goval.MapMax, exp.Code)
}
args := []any{2}
if !reflect.DeepEqual(exp.Args, args) {
t.Errorf("expect the error args: %v; got error args: %v", args, exp.Args)
}
err = goval.Map[string, string]().Max(3).Validate(ctx, val)
if err != nil {
t.Errorf("expect no error; got error: %v", err)
}
}
func TestMapValidator_Each(t *testing.T) {
ctx := context.Background()
val := map[string]string{
"key1": "",
"key2": "a",
}
sv := goval.String().Required().Min(2)
err := goval.Map[string, string]().Each(sv).Validate(ctx, val)
if err == nil {
t.Errorf("expect error; got no error")
}
var exp goval.Errors
if !errors.As(err, &exp) {
t.Fatalf("expect error type: %T; got error type: %T", exp, err)
}
if len(exp) != 2 {
t.Errorf("expect the error length: %d; got error length: %d", 2, len(exp))
}
}