Created
January 27, 2020 22:20
-
-
Save podhmo/a74511489a3806a6e51572202161faed to your computer and use it in GitHub Desktop.
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 news | |
// FuncNews ... | |
type FuncNews struct { | |
Get func() ([]string, error) | |
} | |
// News ... | |
type News *FuncNews |
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 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