In this part, you will write a unit test for the Searcher
component. Review
the documentation on testing. Then, in a file called
searcher_test.go
, write a couple unit tests for the Search
method.
- The query
"pig"
should return 🐖, 🐗, 🐷, and 🐽.
- The query
"PiG"
should return 🐖, 🐗, 🐷, and 🐽.
- The query
"black cat"
should return 🐈⬛.
- The query
"foo bar baz"
should return []string{}
.
Make sure to run every unit test with all the Runner
s returned by
weavertest.AllRunners
.
Solution.
|
package main |
|
|
|
import ( |
|
"context" |
|
"fmt" |
|
"testing" |
|
|
|
"github.com/ServiceWeaver/weaver/weavertest" |
|
"github.com/google/go-cmp/cmp" |
|
) |
|
|
|
func TestSearch(t *testing.T) { |
|
type test struct { |
|
query string |
|
want []string |
|
} |
|
|
|
for _, test := range []test{ |
|
{"pig", []string{"🐖", "🐗", "🐷", "🐽"}}, |
|
{"PiG", []string{"🐖", "🐗", "🐷", "🐽"}}, |
|
{"black cat", []string{"🐈\u200d⬛"}}, |
|
{"foo bar baz", []string{}}, |
|
} { |
|
for _, runner := range weavertest.AllRunners() { |
|
runner.Name = fmt.Sprintf("%s/%q", runner.Name, test.query) |
|
runner.Test(t, func(t *testing.T, searcher Searcher) { |
|
got, err := searcher.Search(context.Background(), test.query) |
|
if err != nil { |
|
t.Fatalf("Search: %v", err) |
|
} |
|
if diff := cmp.Diff(test.want, got); diff != "" { |
|
t.Fatalf("Search (-want,+got):\n%s", diff) |
|
} |
|
}) |
|
} |
|
} |
|
} |
Finally, run the unit tests with go test
.
$ go test .
PASS
ok emojis 0.491s
⬅️ Previous Part
⚫
Next Part ➡️