groupjoin_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package help
  2. import "testing"
  3. func TestGroupJoin(t *testing.T) {
  4. outer := []int{0, 1, 2}
  5. inner := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
  6. want := []interface{}{
  7. KeyValue{0, 4},
  8. KeyValue{1, 5},
  9. KeyValue{2, 0},
  10. }
  11. q := From(outer).GroupJoin(
  12. From(inner),
  13. func(i interface{}) interface{} { return i },
  14. func(i interface{}) interface{} { return i.(int) % 2 },
  15. func(outer interface{}, inners []interface{}) interface{} {
  16. return KeyValue{outer, len(inners)}
  17. })
  18. if !validateQuery(q, want) {
  19. t.Errorf("From().GroupJoin()=%v expected %v", toSlice(q), want)
  20. }
  21. }
  22. func TestGroupJoinT_PanicWhenOuterKeySelectorFnIsInvalid(t *testing.T) {
  23. mustPanicWithError(t, "GroupJoinT: parameter [outerKeySelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
  24. From([]int{0, 1, 2}).GroupJoinT(
  25. From([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}),
  26. func(i, j int) int { return i },
  27. func(i int) int { return i % 2 },
  28. func(outer int, inners []int) KeyValue { return KeyValue{outer, len(inners)} },
  29. )
  30. })
  31. }
  32. func TestGroupJoinT_PanicWhenInnerKeySelectorFnIsInvalid(t *testing.T) {
  33. mustPanicWithError(t, "GroupJoinT: parameter [innerKeySelectorFn] has a invalid function signature. Expected: 'func(T)T', actual: 'func(int,int)int'", func() {
  34. From([]int{0, 1, 2}).GroupJoinT(
  35. From([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}),
  36. func(i int) int { return i },
  37. func(i, j int) int { return i % 2 },
  38. func(outer int, inners []int) KeyValue { return KeyValue{outer, len(inners)} },
  39. )
  40. })
  41. }
  42. func TestGroupJoinT_PanicWhenResultSelectorFnIsInvalid(t *testing.T) {
  43. mustPanicWithError(t, "GroupJoinT: parameter [resultSelectorFn] has a invalid function signature. Expected: 'func(T,T)T', actual: 'func(int,int,[]int)linq.KeyValue'", func() {
  44. From([]int{0, 1, 2}).GroupJoinT(
  45. From([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}),
  46. func(i int) int { return i },
  47. func(i int) int { return i % 2 },
  48. func(outer, j int, inners []int) KeyValue { return KeyValue{outer, len(inners)} },
  49. )
  50. })
  51. }