join_test.go 1004 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package qs
  2. import "testing"
  3. func TestReadJoins(t *testing.T) {
  4. type TestCase struct {
  5. Input string
  6. Opt *ReadJoinsOptions
  7. Output Joins
  8. Err error
  9. }
  10. testCases := []TestCase{
  11. {Input: ""},
  12. {
  13. Input: "join=ingredient",
  14. Output: Joins{"ingredient": true},
  15. },
  16. {
  17. Input: "join=author&join=ingredient",
  18. Output: Joins{"author": true, "ingredient": true},
  19. },
  20. }
  21. for n, tc := range testCases {
  22. t.Logf("(%d) Testing %q with options %+v", n, tc.Input, tc.Opt)
  23. joins, err := ReadStringJoins(tc.Input, nil)
  24. if err != tc.Err {
  25. t.Errorf("Expected error %v, got %v", tc.Err, err)
  26. }
  27. if tc.Err != nil {
  28. continue
  29. }
  30. if tc.Output == nil && joins != nil {
  31. t.Error("Expected nil")
  32. continue
  33. }
  34. if len(joins) != len(tc.Output) {
  35. t.Errorf("Expected %d joins, got %d", len(tc.Output), len(joins))
  36. }
  37. for name, join := range tc.Output {
  38. if join != joins[name] {
  39. t.Errorf("Expected %t for join %s, got %t", join, name, joins[name])
  40. }
  41. }
  42. }
  43. }