intstr_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 intstr
  14. import (
  15. "encoding/json"
  16. "reflect"
  17. "testing"
  18. "github.com/ghodss/yaml"
  19. )
  20. func TestFromInt(t *testing.T) {
  21. i := FromInt(93)
  22. if i.Type != Int || i.IntVal != 93 {
  23. t.Errorf("Expected IntVal=93, got %+v", i)
  24. }
  25. }
  26. func TestFromString(t *testing.T) {
  27. i := FromString("76")
  28. if i.Type != String || i.StrVal != "76" {
  29. t.Errorf("Expected StrVal=\"76\", got %+v", i)
  30. }
  31. }
  32. type IntOrStringHolder struct {
  33. IOrS IntOrString `json:"val"`
  34. }
  35. func TestIntOrStringUnmarshalJSON(t *testing.T) {
  36. cases := []struct {
  37. input string
  38. result IntOrString
  39. }{
  40. {"{\"val\": 123}", FromInt(123)},
  41. {"{\"val\": \"123\"}", FromString("123")},
  42. }
  43. for _, c := range cases {
  44. var result IntOrStringHolder
  45. if err := json.Unmarshal([]byte(c.input), &result); err != nil {
  46. t.Errorf("Failed to unmarshal input '%v': %v", c.input, err)
  47. }
  48. if result.IOrS != c.result {
  49. t.Errorf("Failed to unmarshal input '%v': expected %+v, got %+v", c.input, c.result, result)
  50. }
  51. }
  52. }
  53. func TestIntOrStringMarshalJSON(t *testing.T) {
  54. cases := []struct {
  55. input IntOrString
  56. result string
  57. }{
  58. {FromInt(123), "{\"val\":123}"},
  59. {FromString("123"), "{\"val\":\"123\"}"},
  60. }
  61. for _, c := range cases {
  62. input := IntOrStringHolder{c.input}
  63. result, err := json.Marshal(&input)
  64. if err != nil {
  65. t.Errorf("Failed to marshal input '%v': %v", input, err)
  66. }
  67. if string(result) != c.result {
  68. t.Errorf("Failed to marshal input '%v': expected: %+v, got %q", input, c.result, string(result))
  69. }
  70. }
  71. }
  72. func TestIntOrStringMarshalJSONUnmarshalYAML(t *testing.T) {
  73. cases := []struct {
  74. input IntOrString
  75. }{
  76. {FromInt(123)},
  77. {FromString("123")},
  78. }
  79. for _, c := range cases {
  80. input := IntOrStringHolder{c.input}
  81. jsonMarshalled, err := json.Marshal(&input)
  82. if err != nil {
  83. t.Errorf("1: Failed to marshal input: '%v': %v", input, err)
  84. }
  85. var result IntOrStringHolder
  86. err = yaml.Unmarshal(jsonMarshalled, &result)
  87. if err != nil {
  88. t.Errorf("2: Failed to unmarshal '%+v': %v", string(jsonMarshalled), err)
  89. }
  90. if !reflect.DeepEqual(input, result) {
  91. t.Errorf("3: Failed to marshal input '%+v': got %+v", input, result)
  92. }
  93. }
  94. }
  95. func TestGetValueFromIntOrPercent(t *testing.T) {
  96. tests := []struct {
  97. input IntOrString
  98. total int
  99. roundUp bool
  100. expectErr bool
  101. expectVal int
  102. }{
  103. {
  104. input: FromInt(123),
  105. expectErr: false,
  106. expectVal: 123,
  107. },
  108. {
  109. input: FromString("90%"),
  110. total: 100,
  111. roundUp: true,
  112. expectErr: false,
  113. expectVal: 90,
  114. },
  115. {
  116. input: FromString("90%"),
  117. total: 95,
  118. roundUp: true,
  119. expectErr: false,
  120. expectVal: 86,
  121. },
  122. {
  123. input: FromString("90%"),
  124. total: 95,
  125. roundUp: false,
  126. expectErr: false,
  127. expectVal: 85,
  128. },
  129. {
  130. input: FromString("%"),
  131. expectErr: true,
  132. },
  133. {
  134. input: FromString("90#"),
  135. expectErr: true,
  136. },
  137. {
  138. input: FromString("#%"),
  139. expectErr: true,
  140. },
  141. }
  142. for i, test := range tests {
  143. t.Logf("test case %d", i)
  144. value, err := GetValueFromIntOrPercent(&test.input, test.total, test.roundUp)
  145. if test.expectErr && err == nil {
  146. t.Errorf("expected error, but got none")
  147. continue
  148. }
  149. if !test.expectErr && err != nil {
  150. t.Errorf("unexpected err: %v", err)
  151. continue
  152. }
  153. if test.expectVal != value {
  154. t.Errorf("expected %v, but got %v", test.expectVal, value)
  155. }
  156. }
  157. }