func TestReverse(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{1, 2, 3}).Reverse().Out(&result) e.Expect(err).ToBeNil() e.Expect(result[0]).ToEqual(3) }
func TestLastIndexOf(t *testing.T) { e := expect.New(t) var result int = -1 err := pipeline.In("abba").LastIndexOf('a', 0).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToEqual(3) }
func TestConcat(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{}).Concat([]int{1, 2, 3}, []int{4, 5, 6}).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(6) }
func TestCompact(t *testing.T) { e := expect.New(t) var result1 []interface{} err := pipeline.In([]interface{}{1, nil, 2, 'a', nil}).Compact().Out(&result1) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(result1)).ToEqual(fmt.Sprint([]interface{}{1, 2, 'a'})) }
func TestUnion(t *testing.T) { e := expect.New(t) var res []int err := pipeline.In([]int{1, 2}).Union([]int{2, 3}, []int{3, 4}).Out(&res) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(res)).ToEqual(fmt.Sprint([]int{1, 2, 3, 4})) }
func TestIntersection(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{1, 2, 4}).Intersection([]int{3, 2, 1}, []int{2, 5, 6}).Out(&result) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(result)).ToEqual(fmt.Sprint([]int{2})) }
func TestGroupBy(t *testing.T) { e := expect.New(t) var result map[int][]Person err := pipeline.In([]Person{{12, "John"}, {12, "Jane"}, {20, "Joe"}}). GroupBy(func(el interface{}, i int) interface{} { return el.(Person).Age }).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(2) e.Expect(len(result[12])).ToEqual(2) e.Expect(result[12][0].Age).ToEqual(12) var result1 map[string][]map[string]string err = pipeline.In([]map[string]string{ {"product": "trousers", "category": "clothes"}, {"product": "beer", "category": "drinks"}, {"product": "coat", "category": "clothes"}, }). GroupBy(func(el interface{}, i int) interface{} { return el.(map[string]string)["category"] }).Out(&result1) e.Expect(err).ToBeNil() e.Expect(len(result1)).ToEqual(2) e.Expect(len(result1["clothes"])).ToEqual(2) e.Expect(len(result1["drinks"])).ToEqual(1) }
func TestMap(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{1, 2, 3}).Map(func(element interface{}, index int) interface{} { return element.(int) * 2 }).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(3) e.Expect(result[0]).ToEqual(2) var result2 *[]int err = pipeline.In(&[]int{1, 2, 3}).Out(&result2) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(result2)).ToEqual(fmt.Sprint(&[]int{1, 2, 3})) var result3 string err = pipeline.In("foo").Out(&result3) e.Expect(err).ToBeNil() e.Expect(result3).ToBe("foo") var result4 []string err = pipeline.In([]int{1, 2, 3}).Out(&result4) e.Expect(err).Not().ToBeNil() t.Log(err) }
func TestLast(t *testing.T) { e := expect.New(t) var result int err := pipeline.In([]int{1, 3, 6}).Last().Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToEqual(6) }
func TestCompositeExpression(t *testing.T) { e := expect.New(t) expr := &expression.Composite{expression.OR, []string{"u.group_id = 1"}} e.Expect(expr.Length()).ToBe(1) expr.Add("u.group_id = 2") e.Expect(expr.Length()).ToBe(2) }
func TestComparison(t *testing.T) { e := expect.New(t) for _, fixture := range provideDataForComparison() { part := expr().Comparison(fixture.x, fixture.Type, fixture.y) e.Expect(part).ToEqual(fixture.expected) } }
func TestUnique(t *testing.T) { e := expect.New(t) var result []string err := pipeline.In([]string{"a", "b", "b", "a"}).Unique().Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(2) e.Expect(result[1]).ToEqual("b") }
func TestToMap(t *testing.T) { e := expect.New(t) in := map[string]string{"a": "angel", "b": "bookmark", "c": "card"} result := pipeline.In(in).ToMap(func(val interface{}, key interface{}) (interface{}, interface{}) { return key, val }).MustOut() e.Expect(result.(map[interface{}]interface{})["angel"]).ToEqual("a") }
func TestFlatten(t *testing.T) { e := expect.New(t) var result []int Error := pipeline.In([]interface{}{[]int{1, 2}, 3, []int{4, 5}}).Flatten().Out(&result) e.Expect(Error).ToBeNil() e.Expect(len(result)).ToEqual(5) t.Log(result) }
func TestChunk(t *testing.T) { e := expect.New(t) var result [][]int err := pipeline.In([]int{1, 2, 3, 4, 5}).Chunk(2).Out(&result) e.Expect(err).ToBeNil() t.Log(result) e.Expect(fmt.Sprint(result)).ToEqual(fmt.Sprint([][]int{{1, 2}, {3, 4}, {5}})) }
func TestPush(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{2, 3, 4}).Push(5, 6, 7).Out(&result) e.Expect(err).ToBeNil() for i, val := range []int{2, 3, 4, 5, 6, 7} { e.Expect(result[i]).ToEqual(val) } }
func TestSome(t *testing.T) { e := expect.New(t) var result bool err := pipeline.In([]int{1, 3, 6}).Some(func(element interface{}, index int) bool { return element.(int)%2 == 0 }).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeTrue() }
func TestUnshift(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{3, 4}).Unshift(1, 2).Out(&result) e.Expect(err).ToBeNil() for i, val := range []int{1, 2, 3, 4} { e.Expect(result[i]).ToEqual(val) } }
func TestSlice(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{1, 2, 3, 4}).Slice(0, 2).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(3) for i, val := range []int{1, 2, 3} { e.Expect(result[i]).ToEqual(val) } }
func TestAndX(t *testing.T) { e := expect.New(t) for _, fixture := range provideDataForAndX() { composite := expr().AndX() for _, part := range fixture.parts { composite.Add(part) } e.Expect(composite.String()).ToEqual(fixture.expected) } }
func TestWithout(t *testing.T) { e := expect.New(t) var result []int err := pipeline.In([]int{1, 2, 3, 4}).Without(1, 3).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToEqual(2) for i, val := range []int{2, 4} { e.Expect(result[i]).ToEqual(val) } }
func TestIndexOf(t *testing.T) { e := expect.New(t) var result int = -1 err := pipeline.In([]string{"i", "j", "k", "l"}).IndexOf("k", 0).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToEqual(2) var res int = -1 err = pipeline.In("foobar").IndexOf('a', 0).Out(&res) e.Expect(err).ToBeNil() e.Expect(res).ToEqual(4) }
func TestMapReduce(t *testing.T) { e := expect.New(t) var result int err := pipeline.In([]int{1, 2, 3}).Map(func(element interface{}, index int) interface{} { return element.(int) * 2 }).Reduce(func(result interface{}, element interface{}, index int) interface{} { return result.(int) + element.(int) }, 0).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToEqual(12) }
func TestIterable(t *testing.T) { const ( monday = "monday" tuesday = "tuesday" wednesday = "wednesday" ) m := map[string]string{monday: "studies", tuesday: "date", wednesday: "training"} e := expect.New(t) e.Expect(m).Not().ToBeNil() e.Expect(pipeline.IsIterable(m)).ToBeTrue() i := pipeline.NewIterable(m) e.Expect(i.Length()).ToEqual(3) }
func TestZip(t *testing.T) { e := expect.New(t) var result1 [][]interface{} err := pipeline.In([][]int{{1, 2, 3}}).Zip().Out(&result1) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(result1)).ToEqual(fmt.Sprint([][]interface{}{{1}, {2}, {3}})) err = pipeline.In([][]interface{}{{1, 2, 3}, {"John", "Jane", "David"}}).Zip().Out(&result1) e.Expect(err).ToBeNil() e.Expect(fmt.Sprint(result1)). ToEqual(fmt.Sprint([][]interface{}{{1, "John"}, {2, "Jane"}, {3, "David"}})) err = pipeline.In([][]interface{}{{"US", "FR"}, {"John", "Jane", "David"}, {true}}).Zip().Out(&result1) e.Expect(fmt.Sprint(result1)). ToEqual(fmt.Sprint([][]interface{}{{"US", "John", true}, {"FR", "Jane", nil}, {nil, "David", nil}})) }
func TestEvery(t *testing.T) { e := expect.New(t) var result bool err := pipeline.In([]int{2, 4, 6}).Every(func(element interface{}, index int) bool { return element.(int)%2 == 0 }).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeTrue() err = pipeline.In([]int{2, 4, 5}).Every(func(element interface{}, index int) bool { return element.(int)%2 == 0 }).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeFalse() }
func TestReduceRight(t *testing.T) { e := expect.New(t) var result string err := pipeline.In("kayak").ReduceRight(func(r interface{}, e interface{}, i int) interface{} { return r.(string) + string(e.(rune)) }, "").Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToEqual("kayak") var result2 int pipeline.In([]int{1, 2, 3}).ReduceRight(func(r interface{}, e interface{}, i int) interface{} { return r.(int) - e.(int) }, nil).Out(&result2) e.Expect(result2).ToEqual(0) }
func TestEqual(t *testing.T) { e := expect.New(t) var result bool err := pipeline.In([]int{1, 2, 3}).Equals([]int{1, 2, 3}, []int{1, 2, 3}).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeTrue() err = pipeline.In([]int{1, 2, 3}).Equals([]int{1, 2, 3}).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeTrue() err = pipeline.In([]int{1, 2, 3}).Equals().Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeTrue() err = pipeline.In([]int{1, 2, 3}).Equals([]int{1, 2}).Out(&result) e.Expect(err).ToBeNil() e.Expect(result).ToBeFalse() }
func TestFilter(t *testing.T) { e := expect.New(t) var result []string err := pipeline.In([]string{"a", "b", "c"}).Filter(func(element interface{}, index int) bool { return element.(string) != "a" }).Out(&result) e.Expect(err).ToBeNil() e.Expect(len(result)).ToBe(2) products := Products{{0, "Iphone 6", 0, 500}, {1, "HTC one", 0, 300}, {2, "Apple Watch", 1, 600}, {3, "ThinkPad", 2, 250}} var sample []Product Error := pipeline.In(products). Filter(func(el interface{}, i int) bool { return el.(Product).Price < 499 }). Out(&sample) e.Expect(Error).ToBeNil() e.Expect(len(sample)).ToEqual(2) e.Expect(sample[0].Price).ToEqual(300) }
func TestUserValidator(t *testing.T) { e := expect.New(t) user := &pressmark.User{} userValidator := pressmark.UserValidator{} errors, err := userValidator.Validate(user) t.Log(errors) e.Expect(err).Not().ToBeNil() e.Expect(len(errors)).ToBe(2) user.Email = "*****@*****.**" errors, err = userValidator.Validate(user) t.Log(errors) e.Expect(len(errors)).ToBe(1) user.Email = "john.com" errors, err = userValidator.Validate(user) e.Expect(len(errors)).ToBe(2) user.Email = "john@bar" errors, err = userValidator.Validate(user) e.Expect(len(errors)).ToBe(2) user = &pressmark.User{Name: "John Doe", Email: "*****@*****.**"} errors, err = userValidator.Validate(user) t.Log(errors) e.Expect(len(errors)).ToBe(0) user.Name = strings.Repeat("a", 60) errors, err = userValidator.Validate(user) e.Expect(len(errors)).ToBe(1) user = &pressmark.User{Name: "John Doe", Email: strings.Repeat("a", 300) + "@acme.com"} errors, err = userValidator.Validate(user) e.Expect(len(errors)).ToBe(1) t.Log(errors) user = &pressmark.User{Name: "John Doe", Email: "*****@*****.**"} errors, err = userValidator.Validate(user) e.Expect(len(errors)).ToBe(0) }