chars_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package validate
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func ExampleChars() {
  8. testChars := Chars("0123456789abcdef")
  9. fmt.Println(testChars("invalid input"))
  10. // Output: contains disallowed characters
  11. }
  12. func ExampleExceptChars() {
  13. testExceptChars := Chars("0123456789abcdef")
  14. fmt.Println(testExceptChars("invalid input"))
  15. // Output: contains disallowed characters
  16. }
  17. func TestChars(t *testing.T) {
  18. testCases := map[string]map[string]error{
  19. "0123456789abcdef": {"abcd1234": nil, "abcd 1234": ErrDisallowedChars, "ghijklmno": ErrDisallowedChars},
  20. }
  21. for setup, values := range testCases {
  22. testChars := Chars(setup)
  23. for input, want := range values {
  24. t.Run(input, func(t *testing.T) {
  25. got := testChars(input)
  26. if !errors.Is(got, want) {
  27. t.Error("got", got)
  28. t.Error("want", want)
  29. }
  30. })
  31. }
  32. }
  33. }
  34. func TestExceptChars(t *testing.T) {
  35. testCases := map[string]map[string]error{
  36. "0123456789abcdef": {"abcd1234": ErrDisallowedChars, "abcd 1234": ErrDisallowedChars, "ghijklmno": nil},
  37. }
  38. for setup, values := range testCases {
  39. testExceptChars := ExceptChars(setup)
  40. for input, want := range values {
  41. t.Run(input, func(t *testing.T) {
  42. got := testExceptChars(input)
  43. if !errors.Is(got, want) {
  44. t.Error("got", got)
  45. t.Error("want", want)
  46. }
  47. })
  48. }
  49. }
  50. }