github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/validator/enum/enum_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 enum
     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/Enum")
    18  }
    19  
    20  var _ = Describe("ValidateSlice", func() {
    21  	It("Should match all good", func() {
    22  		ok, err := ValidateSlice([]string{"1", "2", "3"}, []string{"1", "2", "3"})
    23  		Expect(err).ToNot(HaveOccurred())
    24  		Expect(ok).To(BeTrue())
    25  	})
    26  
    27  	It("Should catch errors", func() {
    28  		ok, err := ValidateSlice([]string{"1", "2", "3"}, []string{"1", "2"})
    29  		Expect(err).To(MatchError("'3' is not in the allowed list: 1, 2"))
    30  		Expect(ok).To(BeFalse())
    31  	})
    32  })
    33  
    34  var _ = Describe("ValidateString", func() {
    35  	It("Should validate string fields", func() {
    36  		ok, err := ValidateString("two", []string{"one", "two"})
    37  		Expect(err).ToNot(HaveOccurred())
    38  		Expect(ok).To(BeTrue())
    39  
    40  		ok, err = ValidateString("two", []string{"one", "three"})
    41  		Expect(err).To(MatchError("'two' is not in the allowed list: one, three"))
    42  		Expect(ok).To(BeFalse())
    43  	})
    44  })
    45  
    46  var _ = Describe("ValidateStructField", func() {
    47  	type st struct {
    48  		Things  []string `validate:"enum=one,two"`
    49  		Thing   string   `validate:"enum=one,two"`
    50  		Invalid int      `validate:"enum=one,two"`
    51  	}
    52  
    53  	It("Should validate the enum field", func() {
    54  		things := st{[]string{"one", "two", "three"}, "three", 1}
    55  
    56  		val := reflect.ValueOf(things)
    57  		valueField := val.FieldByName("Things")
    58  		typeField, _ := val.Type().FieldByName("Things")
    59  
    60  		ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate"))
    61  		Expect(err).To(MatchError("'three' is not in the allowed list: one, two"))
    62  		Expect(ok).To(BeFalse())
    63  
    64  		things = st{[]string{"one", "two"}, "three", 2}
    65  		val = reflect.ValueOf(things)
    66  		valueField = val.FieldByName("Things")
    67  
    68  		ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate"))
    69  		Expect(err).ToNot(HaveOccurred())
    70  		Expect(ok).To(BeTrue())
    71  
    72  		valueField = val.FieldByName("Thing")
    73  
    74  		ok, err = ValidateStructField(valueField, typeField.Tag.Get("validate"))
    75  		Expect(err).To(MatchError("'three' is not in the allowed list: one, two"))
    76  		Expect(ok).To(BeFalse())
    77  
    78  	})
    79  
    80  	It("Should validate only supported types", func() {
    81  		things := st{[]string{"one", "two", "three"}, "three", 1}
    82  
    83  		val := reflect.ValueOf(things)
    84  		valueField := val.FieldByName("Invalid")
    85  		typeField, _ := val.Type().FieldByName("Invalid")
    86  
    87  		ok, err := ValidateStructField(valueField, typeField.Tag.Get("validate"))
    88  		Expect(err).To(MatchError("cannot valid data of type int for enums"))
    89  		Expect(ok).To(BeFalse())
    90  	})
    91  })