Debian Codegraph @

debian/go/src/github.com/prashantv/gostub/examples_time_func_test.go

raw
1 package gostub_test
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/prashantv/gostub"
8 )
9
10 // Production code
11 var timeNow = time.Now
12
13 func GetDay() int {
14 return timeNow().Day()
15 }
16
17 // Test code
18 func Example_stubTimeWithFunction() {
19 var day = 2
20 stubs := gostub.Stub(&timeNow, func() time.Time {
21 return time.Date(2015, 07, day, 0, 0, 0, 0, time.UTC)
22 })
23 defer stubs.Reset()
24
25 firstDay := GetDay()
26
27 day = 3
28 secondDay := GetDay()
29
30 fmt.Printf("First day: %v, second day: %v\n", firstDay, secondDay)
31 // Output:
32 // First day: 2, second day: 3
33 }
34
35 // Test code
36 func Example_stubTimeWithConstant() {
37 stubs := gostub.StubFunc(&timeNow, time.Date(2015, 07, 2, 0, 0, 0, 0, time.UTC))
38 defer stubs.Reset()
39
40 fmt.Println("Day:", GetDay())
41 // Output:
42 // Day: 2
43 }