Repository: samber/lo
File: intersect_test.go (Go)

Code before:
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithout(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Without([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
	result2 := Without([]int{0, 7}, 0, 1, 2, 3, 4, 5)
	result3 := Without([]int{}, 0, 1, 2, 3, 4, 5)
	result4 := Without([]int{0, 1, 2}, 0, 1, 2)
	result5 := Without([]int{})
	is.Equal(result1, []int{10})
	is.Equal(result2, []int{7})
	is.Equal(result3, []int{})
	is.Equal(result4, []int{})
	is.Equal(result5, []int{})

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := Without(allStrings, "")
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithoutBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	type user struct {
		name string
		age  int
	}

	result1 := WithoutBy([]user{{name: "nick"}, {name: "peter"}},
		func(item user) string {
			return item.name
		}, "nick", "lily")
	result2 := WithoutBy([]user{}, func(item user) int { return item.age }, 1, 2, 3)
	result3 := WithoutBy([]user{}, func(item user) string { return item.name })
	is.Equal(result1, []user{{name: "peter"}})
	is.Equal(result2, []user{})
	is.Equal(result3, []user{})
}

func TestWithoutEmpty(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := WithoutEmpty([]int{0, 1, 2})
	result2 := WithoutEmpty([]int{1, 2})
	result3 := WithoutEmpty([]int{})

Review comment (refactor):
Can also add this in the test , This gives a more real life example:


```
func main() {
    // Example usage
    users := []User{
        {ID: 1, Name: "Alice"},
        {ID: 2, Name: "Bob"},
        {ID: 3, Name: "Charlie"},
    }

    // Exclude users with IDs 2 and 3
    excludedIDs := []int{2, 3}

    // Extract function to get the user ID
    extractID := func(user User) int {
        return user.ID
    }

    // Filtering users
    filteredUsers := WithoutBy(users, extractID, excludedIDs...)

    // Output the filtered users
    for _, user := range filteredUsers {
        fmt.Println(user.Name)
    }
}
```

Code after:
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithout(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := Without([]int{0, 2, 10}, 0, 1, 2, 3, 4, 5)
	result2 := Without([]int{0, 7}, 0, 1, 2, 3, 4, 5)
	result3 := Without([]int{}, 0, 1, 2, 3, 4, 5)
	result4 := Without([]int{0, 1, 2}, 0, 1, 2)
	result5 := Without([]int{})
	is.Equal(result1, []int{10})
	is.Equal(result2, []int{7})
	is.Equal(result3, []int{})
	is.Equal(result4, []int{})
	is.Equal(result5, []int{})

	type myStrings []string
	allStrings := myStrings{"", "foo", "bar"}
	nonempty := Without(allStrings, "")
	is.IsType(nonempty, allStrings, "type preserved")
}

func TestWithoutBy(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	type User struct {
		Name string
		Age  int
	}

	result1 := WithoutBy([]User{{Name: "nick"}, {Name: "peter"}},
		func(item User) string {
			return item.Name
		}, "nick", "lily")
	result2 := WithoutBy([]User{}, func(item User) int { return item.Age }, 1, 2, 3)
	result3 := WithoutBy([]User{}, func(item User) string { return item.Name })
	is.Equal(result1, []User{{Name: "peter"}})
	is.Equal(result2, []User{})
	is.Equal(result3, []User{})
}

func TestWithoutEmpty(t *testing.T) {
	t.Parallel()
	is := assert.New(t)

	result1 := WithoutEmpty([]int{0, 1, 2})
	result2 := WithoutEmpty([]int{1, 2})
	result3 := WithoutEmpty([]int{})