Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created January 27, 2020 22:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/a74511489a3806a6e51572202161faed to your computer and use it in GitHub Desktop.
Save podhmo/a74511489a3806a6e51572202161faed to your computer and use it in GitHub Desktop.
package news
// FuncNews ...
type FuncNews struct {
Get func() ([]string, error)
}
// News ...
type News *FuncNews
package news
import (
"fmt"
"io"
"strings"
"testing"
)
func Use(title string, news News, w io.Writer) error {
fmt.Fprintf(w, "# %s\n", title)
fmt.Fprintln(w, "")
titles, err := news.Get()
if err != nil {
return err
}
for _, title := range titles {
fmt.Fprintf(w, "- %s\n", title)
}
return nil
}
func TestIt(t *testing.T) {
news := &FuncNews{
Get: func() ([]string, error) {
return []string{
"foo",
"bar",
"boo",
}, nil
},
}
var b strings.Builder
if err := Use("my news", news, &b); err != nil {
t.Fatalf("something wrong: %s", err)
}
want := `# my news
- foo
- bar
- boo
`
got := b.String()
if want != got {
t.Errorf("want %s, but %s", want, got)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment