aggregate_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package help
  2. import "testing"
  3. import "strings"
  4. func TestAggregate(t *testing.T) {
  5. tests := []struct {
  6. input interface{}
  7. want interface{}
  8. }{
  9. {[]string{"apple", "mango", "orange", "passionfruit", "grape"}, "passionfruit"},
  10. {[]string{}, nil},
  11. }
  12. for _, test := range tests {
  13. r := From(test.input).Aggregate(func(r interface{}, i interface{}) interface{} {
  14. if len(r.(string)) > len(i.(string)) {
  15. return r
  16. }
  17. return i
  18. })
  19. if r != test.want {
  20. t.Errorf("From(%v).Aggregate()=%v expected %v", test.input, r, test.want)
  21. }
  22. }
  23. }
  24. func TestAggregateT_PanicWhenFunctionIsInvalid(t *testing.T) {
  25. mustPanicWithError(t, "AggregateT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
  26. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateT(func(x int, r string, i string) string {
  27. if len(r) > len(i) {
  28. return r
  29. }
  30. return i
  31. })
  32. })
  33. }
  34. func TestAggregateWithSeed(t *testing.T) {
  35. input := []string{"apple", "mango", "orange", "banana", "grape"}
  36. want := "passionfruit"
  37. r := From(input).AggregateWithSeed(want,
  38. func(r interface{}, i interface{}) interface{} {
  39. if len(r.(string)) > len(i.(string)) {
  40. return r
  41. }
  42. return i
  43. })
  44. if r != want {
  45. t.Errorf("From(%v).AggregateWithSeed()=%v expected %v", input, r, want)
  46. }
  47. }
  48. func TestAggregateWithSeedT_PanicWhenFunctionIsInvalid(t *testing.T) {
  49. mustPanicWithError(t, "AggregateWithSeed: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
  50. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedT(3, func(x int, r string, i string) string {
  51. if len(r) > len(i) {
  52. return r
  53. }
  54. return i
  55. })
  56. })
  57. }
  58. func TestAggregateWithSeedBy(t *testing.T) {
  59. input := []string{"apple", "mango", "orange", "passionfruit", "grape"}
  60. want := "PASSIONFRUIT"
  61. r := From(input).AggregateWithSeedBy("banana",
  62. func(r interface{}, i interface{}) interface{} {
  63. if len(r.(string)) > len(i.(string)) {
  64. return r
  65. }
  66. return i
  67. },
  68. func(r interface{}) interface{} {
  69. return strings.ToUpper(r.(string))
  70. },
  71. )
  72. if r != want {
  73. t.Errorf("From(%v).AggregateWithSeed()=%v expected %v", input, r, want)
  74. }
  75. }
  76. func TestAggregateWithSeedByT_PanicWhenFunctionIsInvalid(t *testing.T) {
  77. mustPanicWithError(t, "AggregateWithSeedByT: parameter [f] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,string,string)string'", func() {
  78. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
  79. func(x int, r string, i string) string {
  80. if len(r) > len(i) {
  81. return r
  82. }
  83. return i
  84. },
  85. func(r string) string {
  86. return r
  87. },
  88. )
  89. })
  90. }
  91. func TestAggregateWithSeedByT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
  92. mustPanicWithError(t, "AggregateWithSeedByT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(string,int)string'", func() {
  93. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).AggregateWithSeedByT(3,
  94. func(x int, r int) int {
  95. if x > r {
  96. return x
  97. }
  98. return r
  99. },
  100. func(r string, t int) string {
  101. return r
  102. },
  103. )
  104. })
  105. }