mirror of
https://github.com/rjNemo/underscore
synced 2026-06-06 02:26:42 +00:00
* Adding some new funky functions which I find useful Created a Tuple struct as some of the new functions require you to return a new slice with two fields which is the result of the new functions Created the Join, JoinProjection, Range, SumMap, Zip functions, ecah fuction is documented with how it works and had a unit test or maybe more * Added in an OrderBy function * Documentation comment for OrderBy which I missed out * Adding a Unit test for JoinProject function Updated the comments on the Join & OrderBy functions so they make a little more sense. Covered an extra test case with the Join test, where the left set has more data than the right and so the Right handside array of the join is empty
45 lines
1.5 KiB
Go
45 lines
1.5 KiB
Go
package underscore_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
u "github.com/rjNemo/underscore"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
var zero = u.Tuple[int, string]{Left: 0, Right: "Zero"}
|
|
var one = u.Tuple[int, string]{Left: 1, Right: "One"}
|
|
var two = u.Tuple[int, string]{Left: 2, Right: "Two"}
|
|
var three = u.Tuple[int, string]{Left: 3, Right: "Three"}
|
|
|
|
func Test_Join_Can_Join_Two_Slices_Together(t *testing.T) {
|
|
var left = []u.Tuple[int, string]{zero, one, two, three}
|
|
var right = []u.Tuple[int, string]{one, three, two, three, two, three}
|
|
|
|
selector := func(x u.Tuple[int, string]) int { return x.Left }
|
|
|
|
var joined = u.Join(left, right, selector, selector)
|
|
var want = []u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]{
|
|
{Left: zero, Right: nil},
|
|
{Left: one, Right: []u.Tuple[int, string]{one}},
|
|
{Left: two, Right: []u.Tuple[int, string]{two, two}},
|
|
{Left: three, Right: []u.Tuple[int, string]{three, three, three}},
|
|
}
|
|
|
|
assert.Equal(t, want, joined)
|
|
}
|
|
|
|
func Test_Join_Can_Join_and_Project_Two_Slices_Together(t *testing.T) {
|
|
var left = []u.Tuple[int, string]{zero, one, two, three}
|
|
var right = []u.Tuple[int, string]{one, three, two, three, two, three}
|
|
|
|
selector := func(x u.Tuple[int, string]) int { return x.Left }
|
|
project := func(x u.Tuple[u.Tuple[int, string], []u.Tuple[int, string]]) int {
|
|
return len(x.Right) // projecting to a could of how many
|
|
}
|
|
|
|
var joined = u.JoinProject(left, right, selector, selector, project)
|
|
var want = []int{0, 1, 2, 3}
|
|
|
|
assert.Equal(t, want, joined)
|
|
}
|