duration_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. "testing"
  17. "time"
  18. "github.com/ghodss/yaml"
  19. )
  20. type DurationHolder struct {
  21. D Duration `json:"d"`
  22. }
  23. func TestDurationMarshalYAML(t *testing.T) {
  24. cases := []struct {
  25. input Duration
  26. result string
  27. }{
  28. {Duration{5 * time.Second}, "d: 5s\n"},
  29. {Duration{2 * time.Minute}, "d: 2m0s\n"},
  30. {Duration{time.Hour + 3*time.Millisecond}, "d: 1h0m0.003s\n"},
  31. }
  32. for _, c := range cases {
  33. input := DurationHolder{c.input}
  34. result, err := yaml.Marshal(&input)
  35. if err != nil {
  36. t.Errorf("Failed to marshal input: %q: %v", input, err)
  37. }
  38. if string(result) != c.result {
  39. t.Errorf("Failed to marshal input: %q: expected %q, got %q", input, c.result, string(result))
  40. }
  41. }
  42. }
  43. func TestDurationUnmarshalYAML(t *testing.T) {
  44. cases := []struct {
  45. input string
  46. result Duration
  47. }{
  48. {"d: 0s\n", Duration{}},
  49. {"d: 5s\n", Duration{5 * time.Second}},
  50. {"d: 2m0s\n", Duration{2 * time.Minute}},
  51. {"d: 1h0m0.003s\n", Duration{time.Hour + 3*time.Millisecond}},
  52. // Units with zero values can optionally be dropped
  53. {"d: 2m\n", Duration{2 * time.Minute}},
  54. {"d: 1h0.003s\n", Duration{time.Hour + 3*time.Millisecond}},
  55. }
  56. for _, c := range cases {
  57. var result DurationHolder
  58. if err := yaml.Unmarshal([]byte(c.input), &result); err != nil {
  59. t.Errorf("Failed to unmarshal input %q: %v", c.input, err)
  60. }
  61. if result.D != c.result {
  62. t.Errorf("Failed to unmarshal input %q: expected %q, got %q", c.input, c.result, result)
  63. }
  64. }
  65. }
  66. func TestDurationMarshalJSON(t *testing.T) {
  67. cases := []struct {
  68. input Duration
  69. result string
  70. }{
  71. {Duration{5 * time.Second}, `{"d":"5s"}`},
  72. {Duration{2 * time.Minute}, `{"d":"2m0s"}`},
  73. {Duration{time.Hour + 3*time.Millisecond}, `{"d":"1h0m0.003s"}`},
  74. }
  75. for _, c := range cases {
  76. input := DurationHolder{c.input}
  77. result, err := json.Marshal(&input)
  78. if err != nil {
  79. t.Errorf("Failed to marshal input: %q: %v", input, err)
  80. }
  81. if string(result) != c.result {
  82. t.Errorf("Failed to marshal input: %q: expected %q, got %q", input, c.result, string(result))
  83. }
  84. }
  85. }
  86. func TestDurationUnmarshalJSON(t *testing.T) {
  87. cases := []struct {
  88. input string
  89. result Duration
  90. }{
  91. {`{"d":"0s"}`, Duration{}},
  92. {`{"d":"5s"}`, Duration{5 * time.Second}},
  93. {`{"d":"2m0s"}`, Duration{2 * time.Minute}},
  94. {`{"d":"1h0m0.003s"}`, Duration{time.Hour + 3*time.Millisecond}},
  95. // Units with zero values can optionally be dropped
  96. {`{"d":"2m"}`, Duration{2 * time.Minute}},
  97. {`{"d":"1h0.003s"}`, Duration{time.Hour + 3*time.Millisecond}},
  98. }
  99. for _, c := range cases {
  100. var result DurationHolder
  101. if err := json.Unmarshal([]byte(c.input), &result); err != nil {
  102. t.Errorf("Failed to unmarshal input %q: %v", c.input, err)
  103. }
  104. if result.D != c.result {
  105. t.Errorf("Failed to unmarshal input %q: expected %q, got %q", c.input, c.result, result)
  106. }
  107. }
  108. }
  109. func TestDurationMarshalJSONUnmarshalYAML(t *testing.T) {
  110. cases := []struct {
  111. input Duration
  112. }{
  113. {Duration{}},
  114. {Duration{5 * time.Second}},
  115. {Duration{2 * time.Minute}},
  116. {Duration{time.Hour + 3*time.Millisecond}},
  117. }
  118. for i, c := range cases {
  119. input := DurationHolder{c.input}
  120. jsonMarshalled, err := json.Marshal(&input)
  121. if err != nil {
  122. t.Errorf("%d-1: Failed to marshal input: '%v': %v", i, input, err)
  123. }
  124. var result DurationHolder
  125. if err := yaml.Unmarshal(jsonMarshalled, &result); err != nil {
  126. t.Errorf("%d-2: Failed to unmarshal '%+v': %v", i, string(jsonMarshalled), err)
  127. }
  128. if input.D != result.D {
  129. t.Errorf("%d-4: Failed to marshal input '%#v': got %#v", i, input, result)
  130. }
  131. }
  132. }