time_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package unversioned
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "time"
  19. "github.com/ghodss/yaml"
  20. )
  21. type TimeHolder struct {
  22. T Time `json:"t"`
  23. }
  24. func TestTimeMarshalYAML(t *testing.T) {
  25. cases := []struct {
  26. input Time
  27. result string
  28. }{
  29. {Time{}, "t: null\n"},
  30. {Date(1998, time.May, 5, 1, 5, 5, 50, time.FixedZone("test", -4*60*60)), "t: 1998-05-05T05:05:05Z\n"},
  31. {Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "t: 1998-05-05T05:05:05Z\n"},
  32. }
  33. for _, c := range cases {
  34. input := TimeHolder{c.input}
  35. result, err := yaml.Marshal(&input)
  36. if err != nil {
  37. t.Errorf("Failed to marshal input: '%v': %v", input, err)
  38. }
  39. if string(result) != c.result {
  40. t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
  41. }
  42. }
  43. }
  44. func TestTimeUnmarshalYAML(t *testing.T) {
  45. cases := []struct {
  46. input string
  47. result Time
  48. }{
  49. {"t: null\n", Time{}},
  50. {"t: 1998-05-05T05:05:05Z\n", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
  51. }
  52. for _, c := range cases {
  53. var result TimeHolder
  54. if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
  55. t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
  56. }
  57. if result.T != c.result {
  58. t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
  59. }
  60. }
  61. }
  62. func TestTimeMarshalJSON(t *testing.T) {
  63. cases := []struct {
  64. input Time
  65. result string
  66. }{
  67. {Time{}, "{\"t\":null}"},
  68. {Date(1998, time.May, 5, 5, 5, 5, 50, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
  69. {Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC), "{\"t\":\"1998-05-05T05:05:05Z\"}"},
  70. }
  71. for _, c := range cases {
  72. input := TimeHolder{c.input}
  73. result, err := json.Marshal(&input)
  74. if err != nil {
  75. t.Errorf("Failed to marshal input: '%v': %v", input, err)
  76. }
  77. if string(result) != c.result {
  78. t.Errorf("Failed to marshal input: '%v': expected %+v, got %q", input, c.result, string(result))
  79. }
  80. }
  81. }
  82. func TestTimeUnmarshalJSON(t *testing.T) {
  83. cases := []struct {
  84. input string
  85. result Time
  86. }{
  87. {"{\"t\":null}", Time{}},
  88. {"{\"t\":\"1998-05-05T05:05:05Z\"}", Time{Date(1998, time.May, 5, 5, 5, 5, 0, time.UTC).Local()}},
  89. }
  90. for _, c := range cases {
  91. var result TimeHolder
  92. if err := json.Unmarshal([]byte(c.input), &result); err != nil {
  93. t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
  94. }
  95. if result.T != c.result {
  96. t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
  97. }
  98. }
  99. }
  100. func TestTimeMarshalJSONUnmarshalYAML(t *testing.T) {
  101. cases := []struct {
  102. input Time
  103. }{
  104. {Time{}},
  105. {Date(1998, time.May, 5, 5, 5, 5, 50, time.Local).Rfc3339Copy()},
  106. {Date(1998, time.May, 5, 5, 5, 5, 0, time.Local).Rfc3339Copy()},
  107. }
  108. for i, c := range cases {
  109. input := TimeHolder{c.input}
  110. jsonMarshalled, err := json.Marshal(&input)
  111. if err != nil {
  112. t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
  113. }
  114. var result TimeHolder
  115. err = yaml.Unmarshal(jsonMarshalled, &result)
  116. if err != nil {
  117. t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
  118. }
  119. iN, iO := input.T.Zone()
  120. oN, oO := result.T.Zone()
  121. if iN != oN || iO != oO {
  122. t.Errorf("%d-3: Time zones differ before and after serialization %s:%d %s:%d", i, iN, iO, oN, oO)
  123. }
  124. if input.T.UnixNano() != result.T.UnixNano() {
  125. t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
  126. }
  127. }
  128. }
  129. func TestTimeProto(t *testing.T) {
  130. cases := []struct {
  131. input Time
  132. }{
  133. {Time{}},
  134. {Date(1998, time.May, 5, 1, 5, 5, 50, time.Local)},
  135. {Date(1998, time.May, 5, 5, 5, 5, 0, time.Local)},
  136. }
  137. for _, c := range cases {
  138. input := c.input
  139. data, err := input.Marshal()
  140. if err != nil {
  141. t.Fatalf("Failed to marshal input: '%v': %v", input, err)
  142. }
  143. time := Time{}
  144. if err := time.Unmarshal(data); err != nil {
  145. t.Fatalf("Failed to unmarshal output: '%v': %v", input, err)
  146. }
  147. if !reflect.DeepEqual(input, time) {
  148. t.Errorf("Marshal->Unmarshal is not idempotent: '%v' vs '%v'", input, time)
  149. }
  150. }
  151. }