-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaker_test.go
80 lines (61 loc) · 1.46 KB
/
maker_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
// Copyright (c) 2023-2024 Onur Cinar.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
// https://github.com/cinar/checker
package v2_test
import (
"fmt"
"reflect"
"testing"
"github.com/cinar/checker/v2/locales"
v2 "github.com/cinar/checker/v2"
)
func TestMakeCheckersUnknown(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")
type Person struct {
Name string `checkers:"unknown"`
}
person := &Person{
Name: "Onur",
}
v2.CheckStruct(person)
}
func ExampleRegisterMaker() {
locales.EnUSMessages["NOT_FRUIT"] = "Not a fruit name."
v2.RegisterMaker("is-fruit", func(params string) v2.CheckFunc[reflect.Value] {
return func(value reflect.Value) (reflect.Value, error) {
stringValue := value.Interface().(string)
if stringValue == "apple" || stringValue == "banana" {
return value, nil
}
return value, v2.NewCheckError("NOT_FRUIT")
}
})
type Item struct {
Name string `checkers:"is-fruit"`
}
person := &Item{
Name: "banana",
}
err, ok := v2.CheckStruct(person)
if !ok {
fmt.Println(err)
}
}
func TestRegisterMaker(t *testing.T) {
v2.RegisterMaker("unknown", func(params string) v2.CheckFunc[reflect.Value] {
return func(value reflect.Value) (reflect.Value, error) {
return value, nil
}
})
type Person struct {
Name string `checkers:"unknown"`
}
person := &Person{
Name: "Onur",
}
_, ok := v2.CheckStruct(person)
if !ok {
t.Fatal("expected valid")
}
}