github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/validator/maxlength/maxlength_test.go (about) 1 // Copyright (c) 2018-2021, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package maxlength 6 7 import ( 8 "reflect" 9 "testing" 10 11 . "github.com/onsi/ginkgo/v2" 12 . "github.com/onsi/gomega" 13 ) 14 15 func TestFileContent(t *testing.T) { 16 RegisterFailHandler(Fail) 17 RunSpecs(t, "Validator/Maxlength") 18 } 19 20 var _ = Describe("ValidateString", func() { 21 It("Should allow short strings", func() { 22 ok, err := ValidateString("foo", 4) 23 Expect(err).ToNot(HaveOccurred()) 24 Expect(ok).To(BeTrue()) 25 }) 26 27 It("Should fail on long strings", func() { 28 ok, err := ValidateString("foo", 1) 29 Expect(err).To(MatchError("3 characters, max allowed 1")) 30 Expect(ok).To(BeFalse()) 31 }) 32 }) 33 34 var _ = Describe("ValidateStructField", func() { 35 type t struct { 36 Broken string `validate:"maxlength"` 37 String string `validate:"maxlength=3"` 38 Slice []string `validate:"maxlength=3"` 39 Untestable bool `validate:"maxlength=3"` 40 } 41 42 var ( 43 st t 44 ) 45 46 BeforeEach(func() { 47 st = t{} 48 }) 49 50 It("Should fail for invalid tags", func() { 51 val := reflect.ValueOf(st) 52 valueField := val.Field(0) 53 typeField := val.Type().Field(0) 54 55 ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) 56 Expect(err).To(MatchError("invalid tag 'maxlength', must be maxlength=n")) 57 Expect(ok).To(BeFalse()) 58 }) 59 60 It("Should validate strings", func() { 61 st.String = "foo" 62 63 val := reflect.ValueOf(st) 64 valueField := val.FieldByName("String") 65 typeField, _ := val.Type().FieldByName("String") 66 67 ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) 68 Expect(err).ToNot(HaveOccurred()) 69 Expect(ok).To(BeTrue()) 70 71 st.String = "foo foo foo" 72 val = reflect.ValueOf(st) 73 valueField = val.Field(1) 74 75 ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) 76 Expect(err).To(MatchError("11 characters, max allowed 3")) 77 Expect(ok).To(BeFalse()) 78 }) 79 80 It("Should validate slices", func() { 81 st.Slice = []string{"one", "two", "three", "four"} 82 83 val := reflect.ValueOf(st) 84 valueField := val.FieldByName("Slice") 85 typeField, _ := val.Type().FieldByName("Slice") 86 87 ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) 88 Expect(err).To(MatchError("4 values, max allowed 3")) 89 Expect(ok).To(BeFalse()) 90 91 st.Slice = []string{"one", "two", "three"} 92 93 val = reflect.ValueOf(st) 94 valueField = val.FieldByName("Slice") 95 96 ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate")) 97 Expect(err).ToNot(HaveOccurred()) 98 Expect(ok).To(BeTrue()) 99 100 }) 101 102 It("Should fail for invalid data", func() { 103 val := reflect.ValueOf(st) 104 valueField := val.FieldByName("Untestable") 105 typeField, _ := val.Type().FieldByName("Untestable") 106 107 ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate")) 108 Expect(err).To(MatchError("cannot check length of bool type")) 109 Expect(ok).To(BeFalse()) 110 }) 111 })