pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/knf/validators/validators_test.go (about) 1 package validators 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 [formating] 23 test1: 1 24 test2:2 25 26 test3: 3 27 28 [string] 29 test1: test 30 test2: true 31 test3: 4500 32 test4: !$%^& 33 test5: long long long long text for test 34 test6: 35 36 [boolean] 37 test1: true 38 test2: false 39 test3: 0 40 test4: 1 41 test5: 42 test6: example for test 43 test7: no 44 45 [integer] 46 test1: 1 47 test2: -5 48 test3: 10000000 49 test4: A 50 test5: 0xFF 51 test6: 123.4 52 test7: 123.456789 53 test8: 0xZZYY 54 test9: ABCD 55 56 [file-mode] 57 test1: 644 58 test2: 0644 59 test3: 0 60 test4: ABC 61 test5: true 62 63 [comment] 64 test1: 100 65 # test2: 100 66 67 [macro] 68 test1: 100 69 test2: {macro:test1}.50 70 test3: Value is {macro:test2} 71 test4: "{macro:test3}" 72 test5: {ABC} 73 test6: {} 74 75 [k] 76 t: 1 77 ` 78 79 // ////////////////////////////////////////////////////////////////////////////////// // 80 81 type ValidatorSuite struct{} 82 83 // ////////////////////////////////////////////////////////////////////////////////// // 84 85 var _ = check.Suite(&ValidatorSuite{}) 86 87 // ////////////////////////////////////////////////////////////////////////////////// // 88 89 func Test(t *testing.T) { 90 check.TestingT(t) 91 } 92 93 // ////////////////////////////////////////////////////////////////////////////////// // 94 95 func (s *ValidatorSuite) TestBasicValidators(c *check.C) { 96 var err error 97 98 configFile := createConfig(c, _CONFIG_DATA) 99 100 err = knf.Global(configFile) 101 c.Assert(err, check.IsNil) 102 103 var errs []error 104 105 errs = knf.Validate([]*knf.Validator{ 106 {"integer:test1", Empty, nil}, 107 {"integer:test1", Less, 0}, 108 {"integer:test1", Less, 0.5}, 109 {"integer:test1", Greater, 10}, 110 {"integer:test1", Greater, 10.1}, 111 {"integer:test1", Equals, 10}, 112 {"integer:test1", Equals, 10.1}, 113 {"integer:test1", Equals, "123"}, 114 {"string:test3", NotLen, 4}, 115 {"string:test3", NotPrefix, "45"}, 116 {"string:test3", NotSuffix, "00"}, 117 }) 118 119 c.Assert(errs, check.HasLen, 0) 120 121 errs = knf.Validate([]*knf.Validator{ 122 {"boolean:test5", Empty, nil}, 123 {"integer:test1", Less, 10}, 124 {"integer:test1", Greater, 0}, 125 {"integer:test1", Equals, 1}, 126 {"integer:test1", Greater, "12345"}, 127 {"integer:test1", NotContains, []string{"A", "B", "C"}}, 128 {"string:test3", NotLen, 8}, 129 {"string:test3", NotPrefix, "AB"}, 130 {"string:test3", NotSuffix, "CD"}, 131 }) 132 133 c.Assert(errs, check.HasLen, 9) 134 135 c.Assert(errs[0].Error(), check.Equals, "Property boolean:test5 can't be empty") 136 c.Assert(errs[1].Error(), check.Equals, "Property integer:test1 can't be less than 10") 137 c.Assert(errs[2].Error(), check.Equals, "Property integer:test1 can't be greater than 0") 138 c.Assert(errs[3].Error(), check.Equals, "Property integer:test1 can't be equal 1") 139 c.Assert(errs[4].Error(), check.Equals, "Wrong validator for property integer:test1") 140 c.Assert(errs[5].Error(), check.Equals, "Property integer:test1 doesn't contains any valid value") 141 c.Assert(errs[6].Error(), check.Equals, "Property string:test3 must be 8 symbols long") 142 c.Assert(errs[7].Error(), check.Equals, "Property string:test3 must have prefix \"AB\"") 143 c.Assert(errs[8].Error(), check.Equals, "Property string:test3 must have suffix \"CD\"") 144 145 fakeConfigFile := createConfig(c, ` 146 [test] 147 empty: 148 string: test 149 integer: 10 150 float: 10.0 151 boolean: false`) 152 153 fakeConfig, err := knf.Read(fakeConfigFile) 154 155 c.Assert(err, check.IsNil) 156 c.Assert(fakeConfig, check.NotNil) 157 158 c.Assert(Empty(fakeConfig, "test:empty", nil), check.NotNil) 159 c.Assert(Empty(fakeConfig, "test:string", nil), check.IsNil) 160 161 c.Assert(Less(fakeConfig, "test:integer", 30), check.NotNil) 162 c.Assert(Less(fakeConfig, "test:integer", 5), check.IsNil) 163 c.Assert(Less(fakeConfig, "test:float", 30.0), check.NotNil) 164 c.Assert(Less(fakeConfig, "test:float", 5.0), check.IsNil) 165 c.Assert(Less(fakeConfig, "test:string", "30"), check.NotNil) 166 167 c.Assert(Greater(fakeConfig, "test:integer", 5), check.NotNil) 168 c.Assert(Greater(fakeConfig, "test:integer", 30), check.IsNil) 169 c.Assert(Greater(fakeConfig, "test:float", 5.0), check.NotNil) 170 c.Assert(Greater(fakeConfig, "test:float", 30.0), check.IsNil) 171 c.Assert(Greater(fakeConfig, "test:string", "30"), check.NotNil) 172 173 c.Assert(Equals(fakeConfig, "test:empty", ""), check.NotNil) 174 c.Assert(Equals(fakeConfig, "test:string", "test"), check.NotNil) 175 c.Assert(Equals(fakeConfig, "test:integer", 10), check.NotNil) 176 c.Assert(Equals(fakeConfig, "test:float", 10.0), check.NotNil) 177 c.Assert(Equals(fakeConfig, "test:boolean", false), check.NotNil) 178 179 c.Assert(Equals(fakeConfig, "test:empty", []string{}), check.NotNil) 180 c.Assert(Equals(fakeConfig, "test:empty", "1"), check.IsNil) 181 c.Assert(Equals(fakeConfig, "test:string", "testtest"), check.IsNil) 182 c.Assert(Equals(fakeConfig, "test:integer", 15), check.IsNil) 183 c.Assert(Equals(fakeConfig, "test:float", 130.0), check.IsNil) 184 c.Assert(Equals(fakeConfig, "test:boolean", true), check.IsNil) 185 186 c.Assert(NotContains(fakeConfig, "test:string", []string{"A", "B", "test"}), check.IsNil) 187 c.Assert(NotContains(fakeConfig, "test:string", []string{"A", "B"}), check.NotNil) 188 c.Assert(NotContains(fakeConfig, "test:string", 0), check.NotNil) 189 } 190 191 func (s *ValidatorSuite) TestTypeValidators(c *check.C) { 192 fakeConfigFile := createConfig(c, ` 193 [boolean] 194 test1: 195 test2: 0 196 test3: 1 197 test4: True 198 test5: false 199 test6: Yes 200 test7: no 201 test8: disabled 202 203 [num] 204 test1: 205 test2: 0 206 test3: -100 207 test4: 657 208 test5: ABCD 209 210 [float] 211 test1: 212 test2: 0 213 test3: 0.6 214 test4: -0.45 215 test5: ABCD`) 216 217 fakeConfig, err := knf.Read(fakeConfigFile) 218 219 c.Assert(err, check.IsNil) 220 c.Assert(fakeConfig, check.NotNil) 221 222 c.Assert(TypeBool(fakeConfig, "boolean:test1", nil), check.IsNil) 223 c.Assert(TypeBool(fakeConfig, "boolean:test2", nil), check.IsNil) 224 c.Assert(TypeBool(fakeConfig, "boolean:test3", nil), check.IsNil) 225 c.Assert(TypeBool(fakeConfig, "boolean:test4", nil), check.IsNil) 226 c.Assert(TypeBool(fakeConfig, "boolean:test5", nil), check.IsNil) 227 c.Assert(TypeBool(fakeConfig, "boolean:test6", nil), check.IsNil) 228 c.Assert(TypeBool(fakeConfig, "boolean:test7", nil), check.IsNil) 229 c.Assert(TypeBool(fakeConfig, "boolean:test8", nil), check.NotNil) 230 231 c.Assert(TypeNum(fakeConfig, "num:test1", nil), check.IsNil) 232 c.Assert(TypeNum(fakeConfig, "num:test2", nil), check.IsNil) 233 c.Assert(TypeNum(fakeConfig, "num:test3", nil), check.IsNil) 234 c.Assert(TypeNum(fakeConfig, "num:test4", nil), check.IsNil) 235 c.Assert(TypeNum(fakeConfig, "num:test5", nil), check.NotNil) 236 c.Assert(TypeNum(fakeConfig, "float:test3", nil), check.NotNil) 237 238 c.Assert(TypeFloat(fakeConfig, "float:test1", nil), check.IsNil) 239 c.Assert(TypeFloat(fakeConfig, "float:test2", nil), check.IsNil) 240 c.Assert(TypeFloat(fakeConfig, "float:test3", nil), check.IsNil) 241 c.Assert(TypeFloat(fakeConfig, "float:test4", nil), check.IsNil) 242 c.Assert(TypeFloat(fakeConfig, "float:test5", nil), check.NotNil) 243 } 244 245 // ////////////////////////////////////////////////////////////////////////////////// // 246 247 func createConfig(c *check.C, data string) string { 248 configPath := c.MkDir() + "/config.knf" 249 250 err := ioutil.WriteFile(configPath, []byte(data), 0644) 251 252 if err != nil { 253 c.Fatal(err.Error()) 254 } 255 256 return configPath 257 }