|
| 1 | +package underscore_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + u "github.com/rjNemo/underscore" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +var zero = u.Tuple[int, string]{Left: 0, Right: "Zero"} |
| 11 | +var one = u.Tuple[int, string]{Left: 1, Right: "One"} |
| 12 | +var two = u.Tuple[int, string]{Left: 2, Right: "Two"} |
| 13 | +var three = u.Tuple[int, string]{Left: 3, Right: "Three"} |
| 14 | + |
| 15 | +func Test_Join_Can_Join_Two_Slices_Together(t *testing.T) { |
| 16 | + var left = []u.Tuple[int, string]{zero, one, two, three} |
| 17 | + var right = []u.Tuple[int, string]{one, three, two, three, two, three} |
| 18 | + |
| 19 | + selector := func(x u.Tuple[int, string]) int { return x.Left } |
| 20 | + |
| 21 | + var joined = u.Join(left, right, selector, selector) |
| 22 | + var want = []u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]{ |
| 23 | + {Left: zero, Right: nil}, |
| 24 | + {Left: one, Right: []u.Tuple[int, string]{one}}, |
| 25 | + {Left: two, Right: []u.Tuple[int, string]{two, two}}, |
| 26 | + {Left: three, Right: []u.Tuple[int, string]{three, three, three}}, |
| 27 | + } |
| 28 | + |
| 29 | + assert.Equal(t, want, joined) |
| 30 | +} |
| 31 | + |
| 32 | +func Test_Join_Can_Join_and_Project_Two_Slices_Together(t *testing.T) { |
| 33 | + var left = []u.Tuple[int, string]{zero, one, two, three} |
| 34 | + var right = []u.Tuple[int, string]{one, three, two, three, two, three} |
| 35 | + |
| 36 | + selector := func(x u.Tuple[int, string]) int { return x.Left } |
| 37 | + project := func(x u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]) int { |
| 38 | + return len(x.Right) // projecting to a could of how many |
| 39 | + } |
| 40 | + |
| 41 | + var joined = u.JoinProject(left, right, selector, selector, project) |
| 42 | + var want = []int{0, 1, 2, 3} |
| 43 | + |
| 44 | + assert.Equal(t, want, joined) |
| 45 | +} |
0 commit comments