@@ -375,3 +375,63 @@ func BenchmarkIsValidEmail(b *testing.B) {
375
375
_ , _ = IsValidEmail (testEmail , false )
376
376
}
377
377
}
378
+
379
+ //TestIsValidEnum testing the enum value in an accepted list of values
380
+ func TestIsValidEnum (t * testing.T ) {
381
+
382
+ var ok bool
383
+ var err error
384
+
385
+ //Invalid value
386
+ testEnumValue := "1"
387
+ testAcceptedValues := []string {"123" }
388
+ if ok , err = IsValidEnum (testEnumValue , & testAcceptedValues , false ); ok {
389
+ t .Fatal ("This should have failed - value is not found" , testEnumValue , testAcceptedValues , err )
390
+ } else if err .Error () != "value " + testEnumValue + " is not allowed" {
391
+ t .Fatal ("error message was not as expected" , err .Error ())
392
+ }
393
+
394
+ //Valid value
395
+ testEnumValue = "123"
396
+ if ok , err = IsValidEnum (testEnumValue , & testAcceptedValues , false ); ! ok {
397
+ t .Fatal ("This should have passed - value is valid" , testEnumValue , testAcceptedValues , err )
398
+ }
399
+
400
+ //Empty valid not allowed
401
+ testEnumValue = ""
402
+ if ok , err = IsValidEnum (testEnumValue , & testAcceptedValues , false ); ok {
403
+ t .Fatal ("This should have failed - can be empty flag" , testEnumValue , testAcceptedValues , err )
404
+ }
405
+
406
+ //Empty value allowed
407
+ testEnumValue = ""
408
+ if ok , err = IsValidEnum (testEnumValue , & testAcceptedValues , true ); ! ok {
409
+ t .Fatal ("This should have passed - can be empty flag" , testEnumValue , testAcceptedValues , err )
410
+ }
411
+
412
+ }
413
+
414
+ //ExampleIsValidEnum_invalid example of an invalid enum
415
+ func ExampleIsValidEnum_invalid () {
416
+ testAcceptedValues := []string {"123" }
417
+ ok , err := IsValidEnum ("1" , & testAcceptedValues , false ) //Invalid
418
+ fmt .Println (ok , err )
419
+ // Output: false value 1 is not allowed
420
+ }
421
+
422
+ //ExampleIsValidEnum_valid example of an valid enum
423
+ func ExampleIsValidEnum_valid () {
424
+ testAcceptedValues := []string {"123" }
425
+ ok , err := IsValidEnum ("123" , & testAcceptedValues , false ) //Valid
426
+ fmt .Println (ok , err )
427
+ // Output: true <nil>
428
+ }
429
+
430
+ //BenchmarkIsValidEnum benchmarks the IsValidEnum (valid value)
431
+ func BenchmarkIsValidEnum (b * testing.B ) {
432
+ testValue := "1"
433
+ testAcceptedValues := []string {"123" }
434
+ for i := 0 ; i < b .N ; i ++ {
435
+ _ , _ = IsValidEnum (testValue , & testAcceptedValues , false )
436
+ }
437
+ }
0 commit comments