Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package sample_test
- import (
- "bytes"
- "io"
- "testing"
- "github.com/xxx/sample"
- )
- type nopCloser struct {
- io.Writer
- }
- func (nopCloser) Close() error { return nil }
- func TestOutput(t *testing.T) {
- var outputMap map[string]*bytes.Buffer
- createFile := func(name string) (io.WriteCloser, error) {
- b := &bytes.Buffer{}
- outputMap[name] = b
- return nopCloser{b}, nil
- }
- resetFunc := sample.SetCreateFile(createFile)
- defer resetFunc()
- cases := []struct {
- caseName string
- multi bool
- expected map[string]string // map[filename]contents
- }{
- {
- caseName: "multi", multi: true,
- expected: map[string]string{
- "sample_foo.txt": "foo\n",
- "sample_bar.txt": "bar\n",
- },
- },
- {
- caseName: "non-multi", multi: false,
- expected: map[string]string{
- "sample.txt": "foo\nbar\n",
- },
- },
- }
- for _, c := range cases {
- t.Run(c.caseName, func(t *testing.T) {
- outputMap = map[string]*bytes.Buffer{}
- sample.Output([]string{"foo", "bar"}, c.multi)
- if len(c.expected) != len(outputMap) {
- t.Errorf("the number of output files doesn't match (expected=%d, actual=%d)", len(c.expected), len(outputMap))
- return
- }
- for k, v := range c.expected {
- if a, ok := outputMap[k]; !ok {
- t.Errorf("file doesn't exist in output files (filename=%s)", k)
- return
- } else if a.String() != v {
- t.Errorf("file contents don't match (expected=%s, actual=%s)", v, a.String())
- return
- }
- }
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement