util_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 validation
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. type user struct {
  20. ID int
  21. Tag string `valid:"Maxx(aa)"`
  22. Name string `valid:"Required;"`
  23. Age int `valid:"Required;Range(1, 140)"`
  24. match string `valid:"Required; Match(/^(test)?\\w*@(/test/);com$/);Max(2)"`
  25. }
  26. func TestGetValidFuncs(t *testing.T) {
  27. u := user{Name: "test", Age: 1}
  28. tf := reflect.TypeOf(u)
  29. var vfs []ValidFunc
  30. var err error
  31. f, _ := tf.FieldByName("ID")
  32. if vfs, err = getValidFuncs(f); err != nil {
  33. t.Fatal(err)
  34. }
  35. if len(vfs) != 0 {
  36. t.Fatal("should get none ValidFunc")
  37. }
  38. f, _ = tf.FieldByName("Tag")
  39. if vfs, err = getValidFuncs(f); err.Error() != "doesn't exsits Maxx valid function" {
  40. t.Fatal(err)
  41. }
  42. f, _ = tf.FieldByName("Name")
  43. if vfs, err = getValidFuncs(f); err != nil {
  44. t.Fatal(err)
  45. }
  46. if len(vfs) != 1 {
  47. t.Fatal("should get 1 ValidFunc")
  48. }
  49. if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
  50. t.Error("Required funcs should be got")
  51. }
  52. f, _ = tf.FieldByName("Age")
  53. if vfs, err = getValidFuncs(f); err != nil {
  54. t.Fatal(err)
  55. }
  56. if len(vfs) != 2 {
  57. t.Fatal("should get 2 ValidFunc")
  58. }
  59. if vfs[0].Name != "Required" && len(vfs[0].Params) != 0 {
  60. t.Error("Required funcs should be got")
  61. }
  62. if vfs[1].Name != "Range" && len(vfs[1].Params) != 2 {
  63. t.Error("Range funcs should be got")
  64. }
  65. f, _ = tf.FieldByName("match")
  66. if vfs, err = getValidFuncs(f); err != nil {
  67. t.Fatal(err)
  68. }
  69. if len(vfs) != 3 {
  70. t.Fatal("should get 3 ValidFunc but now is", len(vfs))
  71. }
  72. }
  73. func TestCall(t *testing.T) {
  74. u := user{Name: "test", Age: 180}
  75. tf := reflect.TypeOf(u)
  76. var vfs []ValidFunc
  77. var err error
  78. f, _ := tf.FieldByName("Age")
  79. if vfs, err = getValidFuncs(f); err != nil {
  80. t.Fatal(err)
  81. }
  82. valid := &Validation{}
  83. vfs[1].Params = append([]interface{}{valid, u.Age}, vfs[1].Params...)
  84. if _, err = funcs.Call(vfs[1].Name, vfs[1].Params...); err != nil {
  85. t.Fatal(err)
  86. }
  87. if len(valid.Errors) != 1 {
  88. t.Error("age out of range should be has an error")
  89. }
  90. }