take_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. package help
  2. import "testing"
  3. func TestTake(t *testing.T) {
  4. tests := []struct {
  5. input interface{}
  6. output []interface{}
  7. }{
  8. {[]int{1, 2, 2, 3, 1}, []interface{}{1, 2, 2}},
  9. {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, []interface{}{1, 1, 1}},
  10. {"sstr", []interface{}{'s', 's', 't'}},
  11. }
  12. for _, test := range tests {
  13. if q := From(test.input).Take(3); !validateQuery(q, test.output) {
  14. t.Errorf("From(%v).Take(3)=%v expected %v", test.input, toSlice(q), test.output)
  15. }
  16. }
  17. }
  18. func TestTakeWhile(t *testing.T) {
  19. tests := []struct {
  20. input interface{}
  21. predicate func(interface{}) bool
  22. output []interface{}
  23. }{
  24. {[]int{1, 1, 1, 2, 1, 2}, func(i interface{}) bool {
  25. return i.(int) < 3
  26. }, []interface{}{1, 1, 1, 2, 1, 2}},
  27. {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i interface{}) bool {
  28. return i.(int) < 3
  29. }, []interface{}{1, 1, 1, 2, 1, 2}},
  30. {"sstr", func(i interface{}) bool {
  31. return i.(rune) == 's'
  32. }, []interface{}{'s', 's'}},
  33. }
  34. for _, test := range tests {
  35. if q := From(test.input).TakeWhile(test.predicate); !validateQuery(q, test.output) {
  36. t.Errorf("From(%v).TakeWhile()=%v expected %v", test.input, toSlice(q), test.output)
  37. }
  38. }
  39. }
  40. func TestTakeWhileT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
  41. mustPanicWithError(t, "TakeWhileT: parameter [predicateFn] has a invalid function signature. Expected: 'func(T)bool', actual: 'func(int)int'", func() {
  42. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).TakeWhileT(func(item int) int { return item + 2 })
  43. })
  44. }
  45. func TestTakeWhileIndexed(t *testing.T) {
  46. tests := []struct {
  47. input interface{}
  48. predicate func(int, interface{}) bool
  49. output []interface{}
  50. }{
  51. {[]int{1, 1, 1, 2}, func(i int, x interface{}) bool {
  52. return x.(int) < 2 || i < 5
  53. }, []interface{}{1, 1, 1, 2}},
  54. {[9]int{1, 1, 1, 2, 1, 2, 3, 4, 2}, func(i int, x interface{}) bool {
  55. return x.(int) < 2 || i < 5
  56. }, []interface{}{1, 1, 1, 2, 1}},
  57. {"sstr", func(i int, x interface{}) bool {
  58. return x.(rune) == 's' && i < 1
  59. }, []interface{}{'s'}},
  60. }
  61. for _, test := range tests {
  62. if q := From(test.input).TakeWhileIndexed(test.predicate); !validateQuery(q, test.output) {
  63. t.Errorf("From(%v).TakeWhileIndexed()=%v expected %v", test.input, toSlice(q), test.output)
  64. }
  65. }
  66. }
  67. func TestTakeWhileIndexedT_PanicWhenPredicateFnIsInvalid(t *testing.T) {
  68. mustPanicWithError(t, "TakeWhileIndexedT: parameter [predicateFn] has a invalid function signature. Expected: 'func(int,T)bool', actual: 'func(int)int'", func() {
  69. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).TakeWhileIndexedT(func(item int) int { return item + 2 })
  70. })
  71. }