convert_test.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2015 go-swagger maintainers
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package swag
  15. import (
  16. "math"
  17. "strconv"
  18. "testing"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. // These are really dumb tests
  22. func TestConvertBool(t *testing.T) {
  23. for k := range evaluatesAsTrue {
  24. r, err := ConvertBool(k)
  25. if assert.NoError(t, err) {
  26. assert.True(t, r)
  27. }
  28. }
  29. for _, k := range []string{"a", "", "0", "false", "unchecked"} {
  30. r, err := ConvertBool(k)
  31. if assert.NoError(t, err) {
  32. assert.False(t, r)
  33. }
  34. }
  35. }
  36. func TestConvertFloat32(t *testing.T) {
  37. validFloats := []float32{1.0, -1, math.MaxFloat32, math.SmallestNonzeroFloat32, 0, 5.494430303}
  38. invalidFloats := []string{"a", strconv.FormatFloat(math.MaxFloat64, 'f', -1, 64), "true"}
  39. for _, f := range validFloats {
  40. c, err := ConvertFloat32(FormatFloat32(f))
  41. if assert.NoError(t, err) {
  42. assert.EqualValues(t, f, c)
  43. }
  44. }
  45. for _, f := range invalidFloats {
  46. _, err := ConvertFloat32(f)
  47. assert.Error(t, err, "expected '"+f+"' to generate an error")
  48. }
  49. }
  50. func TestConvertFloat64(t *testing.T) {
  51. validFloats := []float64{1.0, -1, float64(math.MaxFloat32), float64(math.SmallestNonzeroFloat32), math.MaxFloat64, math.SmallestNonzeroFloat64, 0, 5.494430303}
  52. invalidFloats := []string{"a", "true"}
  53. for _, f := range validFloats {
  54. c, err := ConvertFloat64(FormatFloat64(f))
  55. if assert.NoError(t, err) {
  56. assert.EqualValues(t, f, c)
  57. }
  58. }
  59. for _, f := range invalidFloats {
  60. _, err := ConvertFloat64(f)
  61. assert.Error(t, err, "expected '"+f+"' to generate an error")
  62. }
  63. }
  64. func TestConvertInt8(t *testing.T) {
  65. validInts := []int8{0, 1, -1, math.MaxInt8, math.MinInt8}
  66. invalidInts := []string{"1.233", "a", "false", strconv.Itoa(int(math.MaxInt64))}
  67. for _, f := range validInts {
  68. c, err := ConvertInt8(FormatInt8(f))
  69. if assert.NoError(t, err) {
  70. assert.EqualValues(t, f, c)
  71. }
  72. }
  73. for _, f := range invalidInts {
  74. _, err := ConvertInt8(f)
  75. assert.Error(t, err, "expected '"+f+"' to generate an error")
  76. }
  77. }
  78. func TestConvertInt16(t *testing.T) {
  79. validInts := []int16{0, 1, -1, math.MaxInt8, math.MinInt8, math.MaxInt16, math.MinInt16}
  80. invalidInts := []string{"1.233", "a", "false", strconv.Itoa(int(math.MaxInt64))}
  81. for _, f := range validInts {
  82. c, err := ConvertInt16(FormatInt16(f))
  83. if assert.NoError(t, err) {
  84. assert.EqualValues(t, f, c)
  85. }
  86. }
  87. for _, f := range invalidInts {
  88. _, err := ConvertInt16(f)
  89. assert.Error(t, err, "expected '"+f+"' to generate an error")
  90. }
  91. }
  92. func TestConvertInt32(t *testing.T) {
  93. validInts := []int32{0, 1, -1, math.MaxInt8, math.MinInt8, math.MaxInt16, math.MinInt16, math.MinInt32, math.MaxInt32}
  94. invalidInts := []string{"1.233", "a", "false", strconv.Itoa(int(math.MaxInt64))}
  95. for _, f := range validInts {
  96. c, err := ConvertInt32(FormatInt32(f))
  97. if assert.NoError(t, err) {
  98. assert.EqualValues(t, f, c)
  99. }
  100. }
  101. for _, f := range invalidInts {
  102. _, err := ConvertInt32(f)
  103. assert.Error(t, err, "expected '"+f+"' to generate an error")
  104. }
  105. }
  106. func TestConvertInt64(t *testing.T) {
  107. validInts := []int64{0, 1, -1, math.MaxInt8, math.MinInt8, math.MaxInt16, math.MinInt16, math.MinInt32, math.MaxInt32, math.MaxInt64, math.MinInt64}
  108. invalidInts := []string{"1.233", "a", "false"}
  109. for _, f := range validInts {
  110. c, err := ConvertInt64(FormatInt64(f))
  111. if assert.NoError(t, err) {
  112. assert.EqualValues(t, f, c)
  113. }
  114. }
  115. for _, f := range invalidInts {
  116. _, err := ConvertInt64(f)
  117. assert.Error(t, err, "expected '"+f+"' to generate an error")
  118. }
  119. }
  120. func TestConvertUint8(t *testing.T) {
  121. validInts := []uint8{0, 1, math.MaxUint8}
  122. invalidInts := []string{"1.233", "a", "false", strconv.FormatUint(math.MaxUint64, 10)}
  123. for _, f := range validInts {
  124. c, err := ConvertUint8(FormatUint8(f))
  125. if assert.NoError(t, err) {
  126. assert.EqualValues(t, f, c)
  127. }
  128. }
  129. for _, f := range invalidInts {
  130. _, err := ConvertUint8(f)
  131. assert.Error(t, err, "expected '"+f+"' to generate an error")
  132. }
  133. }
  134. func TestConvertUint16(t *testing.T) {
  135. validUints := []uint16{0, 1, math.MaxUint8, math.MaxUint16}
  136. invalidUints := []string{"1.233", "a", "false", strconv.FormatUint(math.MaxUint64, 10)}
  137. for _, f := range validUints {
  138. c, err := ConvertUint16(FormatUint16(f))
  139. if assert.NoError(t, err) {
  140. assert.EqualValues(t, f, c)
  141. }
  142. }
  143. for _, f := range invalidUints {
  144. _, err := ConvertUint16(f)
  145. assert.Error(t, err, "expected '"+f+"' to generate an error")
  146. }
  147. }
  148. func TestConvertUint32(t *testing.T) {
  149. validUints := []uint32{0, 1, math.MaxUint8, math.MaxUint16, math.MaxUint32}
  150. invalidUints := []string{"1.233", "a", "false", strconv.FormatUint(math.MaxUint64, 10)}
  151. for _, f := range validUints {
  152. c, err := ConvertUint32(FormatUint32(f))
  153. if assert.NoError(t, err) {
  154. assert.EqualValues(t, f, c)
  155. }
  156. }
  157. for _, f := range invalidUints {
  158. _, err := ConvertUint32(f)
  159. assert.Error(t, err, "expected '"+f+"' to generate an error")
  160. }
  161. }
  162. func TestConvertUint64(t *testing.T) {
  163. validUints := []uint64{0, 1, math.MaxUint8, math.MaxUint16, math.MaxUint32, math.MaxUint64}
  164. invalidUints := []string{"1.233", "a", "false"}
  165. for _, f := range validUints {
  166. c, err := ConvertUint64(FormatUint64(f))
  167. if assert.NoError(t, err) {
  168. assert.EqualValues(t, f, c)
  169. }
  170. }
  171. for _, f := range invalidUints {
  172. _, err := ConvertUint64(f)
  173. assert.Error(t, err, "expected '"+f+"' to generate an error")
  174. }
  175. }
  176. func TestIsFloat64AJSONInteger(t *testing.T) {
  177. assert.False(t, IsFloat64AJSONInteger(math.Inf(1)))
  178. assert.False(t, IsFloat64AJSONInteger(maxJSONFloat+1))
  179. assert.False(t, IsFloat64AJSONInteger(minJSONFloat-1))
  180. assert.True(t, IsFloat64AJSONInteger(1.0))
  181. assert.True(t, IsFloat64AJSONInteger(maxJSONFloat))
  182. assert.True(t, IsFloat64AJSONInteger(minJSONFloat))
  183. assert.True(t, IsFloat64AJSONInteger(1/0.01*67.15000001))
  184. assert.False(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64))
  185. assert.False(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/2))
  186. assert.True(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/3))
  187. assert.True(t, IsFloat64AJSONInteger(math.SmallestNonzeroFloat64/4))
  188. }
  189. func TestFormatBool(t *testing.T) {
  190. assert.Equal(t, "true", FormatBool(true))
  191. assert.Equal(t, "false", FormatBool(false))
  192. }