skip_test.go 2.8 KB

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