-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtime_test.go
84 lines (64 loc) · 1.55 KB
/
time_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
// 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 (
"errors"
"testing"
v2 "github.com/cinar/checker/v2"
)
func ExampleIsTime() {
value := "2024-12-31"
_, err := v2.IsTime("DateOnly", value)
if err != nil {
panic(err)
}
}
func ExampleIsTime_custom() {
rfc3339Layout := "2006-01-02T15:04:05Z07:00"
value := "2024-12-31T10:20:00Z07:00"
_, err := v2.IsTime(rfc3339Layout, value)
if err != nil {
panic(err)
}
}
func TestIsTimeSuccess(t *testing.T) {
value := "2024-12-31"
result, err := v2.IsTime("DateOnly", value)
if result != value {
t.Fatalf("result (%s) is not the original value (%s)", result, value)
}
if err != nil {
t.Fatal(err)
}
}
func TestIsTimeError(t *testing.T) {
value := "2024-12-31"
result, err := v2.IsTime("2006-02-01", value)
if result != value {
t.Fatalf("result (%s) is not the original value (%s)", result, value)
}
if !errors.Is(err, v2.ErrTime) {
t.Fatalf("expected error %s actual %s", v2.ErrTime, err)
}
message := "Not a valid time."
if err.Error() != message {
t.Fatalf("expected %s actual %s", message, err.Error())
}
}
func TestStructTimeError(t *testing.T) {
type Person struct {
Birthday string `checkers:"time:DateOnly"`
}
person := &Person{
Birthday: "2024-31-12",
}
errs, ok := v2.CheckStruct(person)
if ok {
t.Fatalf("expected errors")
}
if !errors.Is(errs["Birthday"], v2.ErrTime) {
t.Fatalf("expected time error")
}
}