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 92 93 94 95 96 97 98 99 100 101 102
|
package types_test
import (
"encoding/json"
"time"
"github.com/mimuret/golang-iij-dpf/pkg/types"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("types.time", func() {
Context("ParseTime", func() {
var (
err error
t types.Time
)
When("failed to parse time.Parse", func() {
BeforeEach(func() {
_, err = types.ParseTime(" ", "//")
})
It("returns err", func() {
Expect(err).To(HaveOccurred())
})
})
When("parse successfull", func() {
BeforeEach(func() {
t, err = types.ParseTime(time.RFC3339Nano, "2021-07-17T17:27:05.999999999+09:00")
Expect(err).To(Succeed())
})
It("returns count value", func() {
Expect(t.Year()).To(Equal(2021))
Expect(t.Month()).To(Equal(time.July))
Expect(t.Day()).To(Equal(17))
Expect(t.Hour()).To(Equal(17))
Expect(t.Minute()).To(Equal(27))
})
})
})
Context("Time", func() {
var (
bs []byte
err error
t types.Time
)
Context("MarshalJSON", func() {
When("valid value", func() {
BeforeEach(func() {
t, err = types.ParseTime(time.RFC3339Nano, "2021-07-17T17:27:05.01+09:00")
Expect(err).To(Succeed())
bs, err = json.Marshal(t)
Expect(err).To(Succeed())
})
It("return string", func() {
Expect(string(bs)).To(Equal(`"2021-07-17T17:27:05.01+09:00"`))
})
})
})
Context("UnmarshalJSON", func() {
When("invalid format", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"p[rke[prkew[rkwe[rkw["`), &t)
})
It("returns count value", func() {
Expect(err).To(HaveOccurred())
})
})
When("empty value", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`""`), &t)
})
It("returns count value", func() {
Expect(err).To(Succeed())
Expect(t).To(Equal(types.Time{}))
})
})
When("valid format", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"2021-07-17T17:27:05.0+09:00"`), &t)
Expect(err).To(Succeed())
})
It("returns count value", func() {
te, err := types.ParseTime(time.RFC3339Nano, "2021-07-17T17:27:05.0+09:00")
Expect(err).To(Succeed())
Expect(t).To(Equal(te))
})
})
})
Context("DeepCopyInto", func() {
BeforeEach(func() {
err = json.Unmarshal([]byte(`"2021-07-17T17:27:05.0+09:00"`), &t)
Expect(err).To(Succeed())
})
It("returns copy object", func() {
copy := types.Time{}
copy.DeepCopyInto(&t)
Expect(copy).To(Equal(t))
Expect(©).ShouldNot(BeIdenticalTo(&t))
})
})
})
})
|