where_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package help
  2. import "testing"
  3. func TestWhere(t *testing.T) {
  4. tests := []struct {
  5. input interface{}
  6. predicate func(interface{}) bool
  7. output []interface{}
  8. }{
  9. {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i interface{}) bool {
  10. return i.(int) >= 3
  11. }, []interface{}{3, 4}},
  12. {"sstr", func(i interface{}) bool {
  13. return i.(rune) != 's'
  14. }, []interface{}{'t', 'r'}},
  15. }
  16. for _, test := range tests {
  17. if q := From(test.input).Where(test.predicate); !validateQuery(q, test.output) {
  18. t.Errorf("From(%v).Where()=%v expected %v", test.input, toSlice(q), test.output)
  19. }
  20. }
  21. }
  22. func TestWhereT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
  23. mustPanicWithError(t, "WhereT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
  24. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).WhereT(func(item int) int { return item + 2 })
  25. })
  26. }
  27. func TestWhereIndexed(t *testing.T) {
  28. tests := []struct {
  29. input interface{}
  30. predicate func(int, interface{}) bool
  31. output []interface{}
  32. }{
  33. {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i int, x interface{}) bool {
  34. return x.(int) < 4 && i > 4
  35. }, []interface{}{2, 3, 2}},
  36. {"sstr", func(i int, x interface{}) bool {
  37. return x.(rune) != 's' || i == 1
  38. }, []interface{}{'s', 't', 'r'}},
  39. {"abcde", func(i int, _ interface{}) bool {
  40. return i < 2
  41. }, []interface{}{'a', 'b'}},
  42. }
  43. for _, test := range tests {
  44. if q := From(test.input).WhereIndexed(test.predicate); !validateQuery(q, test.output) {
  45. t.Errorf("From(%v).WhereIndexed()=%v expected %v", test.input, toSlice(q), test.output)
  46. }
  47. }
  48. }
  49. func TestWhereIndexedT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
  50. mustPanicWithError(t, "WhereIndexedT: parameter [predicateFn] has a invalid function signature. Expected: 'func(int,T)bool', actual: 'func(string)'", func() {
  51. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).WhereIndexedT(func(item string) {})
  52. })
  53. }