-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmin_len_test.go
91 lines (67 loc) · 1.58 KB
/
min_len_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
// 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 (
"testing"
v2 "github.com/cinar/checker/v2"
)
func TestMinLenSuccess(t *testing.T) {
value := "test"
check := v2.MinLen[string](4)
result, err := check(value)
if result != value {
t.Fatalf("result (%s) is not the original value (%s)", result, value)
}
if err != nil {
t.Fatal(err)
}
}
func TestMinLenError(t *testing.T) {
value := "test"
check := v2.MinLen[string](5)
result, err := check(value)
if result != value {
t.Fatalf("result (%s) is not the original value (%s)", result, value)
}
message := "Value cannot be less than 5."
if err.Error() != message {
t.Fatalf("expected %s actual %s", message, err.Error())
}
}
func TestReflectMinLenError(t *testing.T) {
type Person struct {
Name string `checkers:"trim min-len:8"`
}
person := &Person{
Name: " Onur ",
}
errs, ok := v2.CheckStruct(person)
if ok {
t.Fatalf("expected errors")
}
if errs["Name"] == nil {
t.Fatalf("expected minimum length error")
}
}
func TestReflectMinLenInvalidMinLen(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")
type Person struct {
Name string `checkers:"min-len:abcd"`
}
person := &Person{
Name: "Onur",
}
v2.CheckStruct(person)
}
func TestReflectMinLenInvalidType(t *testing.T) {
defer FailIfNoPanic(t, "expected panic")
type Person struct {
Name int `checkers:"min-len:8"`
}
person := &Person{
Name: 1,
}
v2.CheckStruct(person)
}