in_test.go 594 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package validate
  2. import (
  3. "errors"
  4. "fmt"
  5. "testing"
  6. )
  7. func ExampleIn() {
  8. testIn := In("abc", "def", "xyz")
  9. fmt.Println(testIn("123"))
  10. // Output: not allowed
  11. }
  12. func TestIn(t *testing.T) {
  13. testIn := In("abc", "def", "xyz")
  14. testCases := map[string]error{
  15. "abc": nil,
  16. "def": nil,
  17. "xyz": nil,
  18. "abcd": ErrValueNotAllowed,
  19. "123": ErrValueNotAllowed,
  20. "": ErrValueNotAllowed,
  21. }
  22. for input, want := range testCases {
  23. t.Run(input, func(t *testing.T) {
  24. got := testIn(input)
  25. if !errors.Is(got, want) {
  26. t.Error("got", got)
  27. t.Error("want", want)
  28. }
  29. })
  30. }
  31. }