pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/regexp/validators_test.go (about) 1 package regexp 2 3 // ////////////////////////////////////////////////////////////////////////////////// // 4 // // 5 // Copyright (c) 2022 ESSENTIAL KAOS // 6 // Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // 7 // // 8 // ////////////////////////////////////////////////////////////////////////////////// // 9 10 import ( 11 "io/ioutil" 12 "testing" 13 14 "pkg.re/essentialkaos/ek.v12/knf" 15 16 check "pkg.re/essentialkaos/check.v1" 17 ) 18 19 // ////////////////////////////////////////////////////////////////////////////////// // 20 21 const _CONFIG_DATA = ` 22 [regexp] 23 test1: TEST1 24 test2: test 25 test3: 26 ` 27 28 // ////////////////////////////////////////////////////////////////////////////////// // 29 30 type ValidatorSuite struct{} 31 32 // ////////////////////////////////////////////////////////////////////////////////// // 33 34 var _ = check.Suite(&ValidatorSuite{}) 35 36 // ////////////////////////////////////////////////////////////////////////////////// // 37 38 func Test(t *testing.T) { 39 check.TestingT(t) 40 } 41 42 // ////////////////////////////////////////////////////////////////////////////////// // 43 44 func (s *ValidatorSuite) TestRegexpValidator(c *check.C) { 45 var err error 46 47 configFile := createConfig(c, _CONFIG_DATA) 48 49 err = knf.Global(configFile) 50 c.Assert(err, check.IsNil) 51 52 errs := knf.Validate([]*knf.Validator{ 53 {"regexp:test1", Regexp, `^[A-Z0-9]{4,5}$`}, 54 {"regexp:test2", Regexp, ``}, 55 {"regexp:test3", Regexp, `^[A-Z0-9]{4,5}$`}, 56 }) 57 58 c.Assert(errs, check.HasLen, 0) 59 60 errs = knf.Validate([]*knf.Validator{ 61 {"regexp:test2", Regexp, `^[A-Z0-9]{4}$`}, 62 {"regexp:test2", Regexp, `\`}, 63 }) 64 65 c.Assert(errs, check.HasLen, 2) 66 } 67 68 // ////////////////////////////////////////////////////////////////////////////////// // 69 70 func createConfig(c *check.C, data string) string { 71 configPath := c.MkDir() + "/config.knf" 72 73 err := ioutil.WriteFile(configPath, []byte(data), 0644) 74 75 if err != nil { 76 c.Fatal(err.Error()) 77 } 78 79 return configPath 80 }