validate_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import (
  6. "bytes"
  7. "testing"
  8. "time"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. type testInterface interface {
  12. String() string
  13. }
  14. type substructNoValidation struct {
  15. IString string
  16. IInt int
  17. }
  18. type mapNoValidationSub map[string]substructNoValidation
  19. type structNoValidationValues struct {
  20. substructNoValidation
  21. Boolean bool
  22. Uinteger uint
  23. Integer int
  24. Integer8 int8
  25. Integer16 int16
  26. Integer32 int32
  27. Integer64 int64
  28. Uinteger8 uint8
  29. Uinteger16 uint16
  30. Uinteger32 uint32
  31. Uinteger64 uint64
  32. Float32 float32
  33. Float64 float64
  34. String string
  35. Date time.Time
  36. Struct substructNoValidation
  37. InlinedStruct struct {
  38. String []string
  39. Integer int
  40. }
  41. IntSlice []int
  42. IntPointerSlice []*int
  43. StructPointerSlice []*substructNoValidation
  44. StructSlice []substructNoValidation
  45. InterfaceSlice []testInterface
  46. UniversalInterface interface{}
  47. CustomInterface testInterface
  48. FloatMap map[string]float32
  49. StructMap mapNoValidationSub
  50. }
  51. func createNoValidationValues() structNoValidationValues {
  52. integer := 1
  53. s := structNoValidationValues{
  54. Boolean: true,
  55. Uinteger: 1 << 29,
  56. Integer: -10000,
  57. Integer8: 120,
  58. Integer16: -20000,
  59. Integer32: 1 << 29,
  60. Integer64: 1 << 61,
  61. Uinteger8: 250,
  62. Uinteger16: 50000,
  63. Uinteger32: 1 << 31,
  64. Uinteger64: 1 << 62,
  65. Float32: 123.456,
  66. Float64: 123.456789,
  67. String: "text",
  68. Date: time.Time{},
  69. CustomInterface: &bytes.Buffer{},
  70. Struct: substructNoValidation{},
  71. IntSlice: []int{-3, -2, 1, 0, 1, 2, 3},
  72. IntPointerSlice: []*int{&integer},
  73. StructSlice: []substructNoValidation{},
  74. UniversalInterface: 1.2,
  75. FloatMap: map[string]float32{
  76. "foo": 1.23,
  77. "bar": 232.323,
  78. },
  79. StructMap: mapNoValidationSub{
  80. "foo": substructNoValidation{},
  81. "bar": substructNoValidation{},
  82. },
  83. // StructPointerSlice []noValidationSub
  84. // InterfaceSlice []testInterface
  85. }
  86. s.InlinedStruct.Integer = 1000
  87. s.InlinedStruct.String = []string{"first", "second"}
  88. s.IString = "substring"
  89. s.IInt = 987654
  90. return s
  91. }
  92. func TestValidateNoValidationValues(t *testing.T) {
  93. origin := createNoValidationValues()
  94. test := createNoValidationValues()
  95. empty := structNoValidationValues{}
  96. assert.Nil(t, validate(test))
  97. assert.Nil(t, validate(&test))
  98. assert.Nil(t, validate(empty))
  99. assert.Nil(t, validate(&empty))
  100. assert.Equal(t, origin, test)
  101. }
  102. type structNoValidationPointer struct {
  103. substructNoValidation
  104. Boolean bool
  105. Uinteger *uint
  106. Integer *int
  107. Integer8 *int8
  108. Integer16 *int16
  109. Integer32 *int32
  110. Integer64 *int64
  111. Uinteger8 *uint8
  112. Uinteger16 *uint16
  113. Uinteger32 *uint32
  114. Uinteger64 *uint64
  115. Float32 *float32
  116. Float64 *float64
  117. String *string
  118. Date *time.Time
  119. Struct *substructNoValidation
  120. IntSlice *[]int
  121. IntPointerSlice *[]*int
  122. StructPointerSlice *[]*substructNoValidation
  123. StructSlice *[]substructNoValidation
  124. InterfaceSlice *[]testInterface
  125. FloatMap *map[string]float32
  126. StructMap *mapNoValidationSub
  127. }
  128. func TestValidateNoValidationPointers(t *testing.T) {
  129. //origin := createNoValidation_values()
  130. //test := createNoValidation_values()
  131. empty := structNoValidationPointer{}
  132. //assert.Nil(t, validate(test))
  133. //assert.Nil(t, validate(&test))
  134. assert.Nil(t, validate(empty))
  135. assert.Nil(t, validate(&empty))
  136. //assert.Equal(t, origin, test)
  137. }
  138. type Object map[string]interface{}
  139. func TestValidatePrimitives(t *testing.T) {
  140. obj := Object{"foo": "bar", "bar": 1}
  141. assert.NoError(t, validate(obj))
  142. assert.NoError(t, validate(&obj))
  143. assert.Equal(t, obj, Object{"foo": "bar", "bar": 1})
  144. obj2 := []Object{{"foo": "bar", "bar": 1}, {"foo": "bar", "bar": 1}}
  145. assert.NoError(t, validate(obj2))
  146. assert.NoError(t, validate(&obj2))
  147. nu := 10
  148. assert.NoError(t, validate(nu))
  149. assert.NoError(t, validate(&nu))
  150. assert.Equal(t, nu, 10)
  151. str := "value"
  152. assert.NoError(t, validate(str))
  153. assert.NoError(t, validate(&str))
  154. assert.Equal(t, str, "value")
  155. }