join_test.go 1.9 KB

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