Created
July 7, 2020 20:19
-
-
Save eexit/3ac22b378991a879282a9d9bd5c6baf4 to your computer and use it in GitHub Desktop.
Go basic string slice filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== RUN Test_remove | |
=== RUN Test_remove/want_and_list_are_empty | |
=== RUN Test_remove/list_is_empty | |
=== RUN Test_remove/list_contains_only_one_occurence_of_wanted | |
=== RUN Test_remove/list_contains_only_occurences_of_wanted | |
=== RUN Test_remove/list_contains_one_occurence_of_wanted | |
=== RUN Test_remove/list_does_not_contain_occurence_of_wanted | |
--- PASS: Test_remove (0.00s) | |
--- PASS: Test_remove/want_and_list_are_empty (0.00s) | |
--- PASS: Test_remove/list_is_empty (0.00s) | |
--- PASS: Test_remove/list_contains_only_one_occurence_of_wanted (0.00s) | |
--- PASS: Test_remove/list_contains_only_occurences_of_wanted (0.00s) | |
--- PASS: Test_remove/list_contains_one_occurence_of_wanted (0.00s) | |
--- PASS: Test_remove/list_does_not_contain_occurence_of_wanted (0.00s) | |
PASS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package remove | |
// remove loops over a slice of strings and remove want | |
func remove(want string, list []string) []string { | |
out := []string{} | |
for _, e := range list { | |
if e != want { | |
out = append(out, e) | |
} | |
} | |
return out | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package remove | |
import ( | |
"reflect" | |
"testing" | |
) | |
func Test_remove(t *testing.T) { | |
type args struct { | |
want string | |
list []string | |
} | |
tests := []struct { | |
name string | |
args args | |
want []string | |
}{ | |
{ | |
name: "want and list are empty", | |
args: args{want: "", list: []string{}}, | |
want: []string{}, | |
}, | |
{ | |
name: "list is empty", | |
args: args{want: "a", list: []string{}}, | |
want: []string{}, | |
}, | |
{ | |
name: "list contains only one occurence of wanted", | |
args: args{want: "a", list: []string{"a"}}, | |
want: []string{}, | |
}, | |
{ | |
name: "list contains only occurences of wanted", | |
args: args{want: "a", list: []string{"a", "a", "a"}}, | |
want: []string{}, | |
}, | |
{ | |
name: "list contains one occurence of wanted", | |
args: args{want: "a", list: []string{"b", "a", "c"}}, | |
want: []string{"b", "c"}, | |
}, | |
{ | |
name: "list does not contain occurence of wanted", | |
args: args{want: "a", list: []string{"b", "c", "d"}}, | |
want: []string{"b", "c", "d"}, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
if got := remove(tt.args.want, tt.args.list); !reflect.DeepEqual(got, tt.want) { | |
t.Errorf("remove() = %v, want %v", got, tt.want) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment