-
Notifications
You must be signed in to change notification settings - Fork 26
/
clock_test.go
65 lines (51 loc) · 1.26 KB
/
clock_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
package chronos_test
import (
"errors"
"testing"
"time"
"github.com/paketo-buildpacks/packit/v2/chronos"
"github.com/sclevine/spec"
. "github.com/onsi/gomega"
)
func testClock(t *testing.T, context spec.G, it spec.S) {
var Expect = NewWithT(t).Expect
context("Now", func() {
it("returns the value from the given Now function", func() {
now := time.Now()
clock := chronos.NewClock(func() time.Time {
return now
})
Expect(clock.Now()).To(Equal(now))
})
})
context("Measure", func() {
var clock chronos.Clock
it.Before(func() {
now := time.Now()
times := []time.Time{now, now.Add(20 * time.Second)}
clock = chronos.NewClock(func() time.Time {
t := time.Now()
if len(times) > 0 {
t = times[0]
times = times[1:]
}
return t
})
})
it("returns the duration taken to perform the operation", func() {
duration, err := clock.Measure(func() error {
return nil
})
Expect(err).NotTo(HaveOccurred())
Expect(duration).To(Equal(20 * time.Second))
})
context("when the operation errors", func() {
it("returns that error", func() {
_, err := clock.Measure(func() error {
return errors.New("operation failed")
})
Expect(err).To(MatchError("operation failed"))
})
})
})
}