-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
64 lines (52 loc) · 1.68 KB
/
parser_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
package years_test
import (
"fmt"
"github.com/amberpixels/years"
"github.com/expectto/be/be_time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"time"
)
var _ = Describe("Parser", func() {
Context("Default parser", func() {
AfterEach(func() {
years.SetParserDefaults(
years.AcceptUnixSeconds(),
years.AcceptAliases(),
)
})
It("should parse Unix timestamp", func() {
var timestamp int64 = 1709682885
parsedTime, err := years.DefaultParser().JustParse(fmt.Sprintf("%d", 1709682885))
Expect(err).Should(Succeed())
Expect(parsedTime).To(be_time.Unix(timestamp))
})
It("should parse DateOnly date", func() {
timeStr := "2024-03-06"
expectedTime, _ := time.Parse(time.DateOnly, timeStr)
years.SetParserDefaults(years.WithLayouts(time.DateOnly))
parsedTime, err := years.DefaultParser().JustParse(timeStr)
Expect(err).Should(Succeed())
Expect(parsedTime).To(Equal(expectedTime))
})
It("should parse today/yesterday/tomorrow alias", func() {
mockClock := &StaticClock{
now: time.Date(2024, time.March, 01, 14, 30, 59, 0, time.UTC),
}
parser := years.NewParser(
years.WithCustomClock(mockClock),
years.AcceptAliases(),
years.AcceptUnixSeconds(),
)
today, err := parser.JustParse("today")
Expect(err).Should(Succeed())
Expect(today.String()).To(Equal(`2024-03-01 00:00:00 +0000 UTC`))
yesterday, err := parser.JustParse("yesterday")
Expect(err).Should(Succeed())
Expect(yesterday.String()).To(Equal(`2024-02-29 00:00:00 +0000 UTC`))
tomorrow, err := parser.JustParse("tomorrow")
Expect(err).Should(Succeed())
Expect(tomorrow.String()).To(Equal(`2024-03-02 00:00:00 +0000 UTC`))
})
})
})