selectmany_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package help
  2. import (
  3. "strconv"
  4. "testing"
  5. )
  6. func TestSelectMany(t *testing.T) {
  7. tests := []struct {
  8. input interface{}
  9. selector func(interface{}) Query
  10. output []interface{}
  11. }{
  12. {[][]int{{1, 2, 3}, {4, 5, 6, 7}}, func(i interface{}) Query {
  13. return From(i)
  14. }, []interface{}{1, 2, 3, 4, 5, 6, 7}},
  15. {[]string{"str", "ing"}, func(i interface{}) Query {
  16. return FromString(i.(string))
  17. }, []interface{}{'s', 't', 'r', 'i', 'n', 'g'}},
  18. }
  19. for _, test := range tests {
  20. if q := From(test.input).SelectMany(test.selector); !validateQuery(q, test.output) {
  21. t.Errorf("From(%v).SelectMany()=%v expected %v", test.input, toSlice(q), test.output)
  22. }
  23. }
  24. }
  25. func TestSelectManyT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  26. mustPanicWithError(t, "SelectManyT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)linq.Query', actual: 'func(int)int'", func() {
  27. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SelectManyT(func(item int) int { return item + 2 })
  28. })
  29. }
  30. func TestSelectManyIndexed(t *testing.T) {
  31. tests := []struct {
  32. input interface{}
  33. selector func(int, interface{}) Query
  34. output []interface{}
  35. }{
  36. {[][]int{{1, 2, 3}, {4, 5, 6, 7}}, func(i int, x interface{}) Query {
  37. if i > 0 {
  38. return From(x.([]int)[1:])
  39. }
  40. return From(x)
  41. }, []interface{}{1, 2, 3, 5, 6, 7}},
  42. {[]string{"str", "ing"}, func(i int, x interface{}) Query {
  43. return FromString(x.(string) + strconv.Itoa(i))
  44. }, []interface{}{'s', 't', 'r', '0', 'i', 'n', 'g', '1'}},
  45. }
  46. for _, test := range tests {
  47. if q := From(test.input).SelectManyIndexed(test.selector); !validateQuery(q, test.output) {
  48. t.Errorf("From(%v).SelectManyIndexed()=%v expected %v", test.input, toSlice(q), test.output)
  49. }
  50. }
  51. }
  52. func TestSelectManyIndexedT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  53. mustPanicWithError(t, "SelectManyIndexedT: parameter [selectorFn] has a invalid function signature. Expected: 'func(int,T)linq.Query', actual: 'func(int)int'", func() {
  54. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SelectManyIndexedT(func(item int) int { return item + 2 })
  55. })
  56. }
  57. func TestSelectManyBy(t *testing.T) {
  58. tests := []struct {
  59. input interface{}
  60. selector func(interface{}) Query
  61. resultSelector func(interface{}, interface{}) interface{}
  62. output []interface{}
  63. }{
  64. {[][]int{{1, 2, 3}, {4, 5, 6, 7}}, func(i interface{}) Query {
  65. return From(i)
  66. }, func(x interface{}, y interface{}) interface{} {
  67. return x.(int) + 1
  68. }, []interface{}{2, 3, 4, 5, 6, 7, 8}},
  69. {[]string{"str", "ing"}, func(i interface{}) Query {
  70. return FromString(i.(string))
  71. }, func(x interface{}, y interface{}) interface{} {
  72. return string(x.(rune)) + "_"
  73. }, []interface{}{"s_", "t_", "r_", "i_", "n_", "g_"}},
  74. }
  75. for _, test := range tests {
  76. if q := From(test.input).SelectManyBy(test.selector, test.resultSelector); !validateQuery(q, test.output) {
  77. t.Errorf("From(%v).SelectManyBy()=%v expected %v", test.input, toSlice(q), test.output)
  78. }
  79. }
  80. }
  81. func TestSelectManyByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  82. mustPanicWithError(t, "SelectManyByT: parameter [selectorFn] has a invalid function signature. Expected: 'func(T)linq.Query', actual: 'func(int)interface {}'", func() {
  83. From([]int{1, 1, 1, 2, 1, 2, 3, 4, 2}).SelectManyByT(func(item int) interface{} { return item + 2 }, 2)
  84. })
  85. }
  86. func TestSelectManyByT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
  87. mustPanicWithError(t, "SelectManyByT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func()'", func() {
  88. From([][]int{{1, 1, 1, 2}, {1, 2, 3, 4, 2}}).SelectManyByT(
  89. func(item interface{}) Query { return From(item) },
  90. func() {},
  91. )
  92. })
  93. }
  94. func TestSelectManyIndexedBy(t *testing.T) {
  95. tests := []struct {
  96. input interface{}
  97. selector func(int, interface{}) Query
  98. resultSelector func(interface{}, interface{}) interface{}
  99. output []interface{}
  100. }{
  101. {[][]int{{1, 2, 3}, {4, 5, 6, 7}}, func(i int, x interface{}) Query {
  102. if i == 0 {
  103. return From([]int{10, 20, 30})
  104. }
  105. return From(x)
  106. }, func(x interface{}, y interface{}) interface{} {
  107. return x.(int) + 1
  108. }, []interface{}{11, 21, 31, 5, 6, 7, 8}},
  109. {[]string{"st", "ng"}, func(i int, x interface{}) Query {
  110. if i == 0 {
  111. return FromString(x.(string) + "r")
  112. }
  113. return FromString("i" + x.(string))
  114. }, func(x interface{}, y interface{}) interface{} {
  115. return string(x.(rune)) + "_"
  116. }, []interface{}{"s_", "t_", "r_", "i_", "n_", "g_"}},
  117. }
  118. for _, test := range tests {
  119. if q := From(test.input).SelectManyByIndexed(test.selector, test.resultSelector); !validateQuery(q, test.output) {
  120. t.Errorf("From(%v).SelectManyIndexedBy()=%v expected %v", test.input, toSlice(q), test.output)
  121. }
  122. }
  123. }
  124. func TestSelectManyIndexedByT_PanicWhenSelectorFnIsInvalid(t *testing.T) {
  125. mustPanicWithError(t, "SelectManyByIndexedT: parameter [selectorFn] has a invalid function signature. Expected: 'func(int,T)linq.Query', actual: 'func(int)interface {}'", func() {
  126. From([][]int{{1, 1, 1, 2}, {1, 2, 3, 4, 2}}).SelectManyByIndexedT(
  127. func(item int) interface{} { return item + 2 },
  128. 2,
  129. )
  130. })
  131. }
  132. func TestSelectManyIndexedByT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
  133. mustPanicWithError(t, "SelectManyByIndexedT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func()'", func() {
  134. From([][]int{{1, 1, 1, 2}, {1, 2, 3, 4, 2}}).SelectManyByIndexedT(
  135. func(index int, item interface{}) Query { return From(item) },
  136. func() {},
  137. )
  138. })
  139. }