chars_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package validate
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. func TestChars(t *testing.T) {
  7. type TestCase struct {
  8. Input string
  9. C string
  10. Err error
  11. }
  12. hexRange := "0123456789abcdef"
  13. testCases := []TestCase{
  14. {Input: "abcd1234", C: hexRange},
  15. {Input: "abcd 1234", C: hexRange, Err: ErrDisallowedChars},
  16. {Input: "ghijklmno", C: hexRange, Err: ErrDisallowedChars},
  17. }
  18. for n, tc := range testCases {
  19. t.Logf("(%d) Testing %q against %q", n, tc.Input, tc.C)
  20. f := Chars(tc.C)
  21. err := f(tc.Input)
  22. if !errors.Is(err, tc.Err) {
  23. t.Errorf("Expected error %v, got %v", tc.Err, err)
  24. }
  25. }
  26. }
  27. func TestExceptChars(t *testing.T) {
  28. type TestCase struct {
  29. Input string
  30. C string
  31. Err error
  32. }
  33. hexRange := "0123456789abcdef"
  34. testCases := []TestCase{
  35. {Input: "abcd1234", C: hexRange, Err: ErrDisallowedChars},
  36. {Input: "abcd 1234", C: hexRange, Err: ErrDisallowedChars},
  37. {Input: "ghijklmno", C: hexRange},
  38. }
  39. for n, tc := range testCases {
  40. t.Logf("(%d) Testing %q against %q", n, tc.Input, tc.C)
  41. f := ExceptChars(tc.C)
  42. err := f(tc.Input)
  43. if !errors.Is(err, tc.Err) {
  44. t.Errorf("Expected error %v, got %v", tc.Err, err)
  45. }
  46. }
  47. }