Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. package sample_test
  2.  
  3. import (
  4. "bytes"
  5. "io"
  6. "testing"
  7.  
  8. "github.com/xxx/sample"
  9. )
  10.  
  11. type nopCloser struct {
  12. io.Writer
  13. }
  14.  
  15. func (nopCloser) Close() error { return nil }
  16.  
  17. func TestOutput(t *testing.T) {
  18.  
  19. var outputMap map[string]*bytes.Buffer
  20. createFile := func(name string) (io.WriteCloser, error) {
  21. b := &bytes.Buffer{}
  22. outputMap[name] = b
  23. return nopCloser{b}, nil
  24. }
  25. resetFunc := sample.SetCreateFile(createFile)
  26. defer resetFunc()
  27.  
  28. cases := []struct {
  29. caseName string
  30. multi bool
  31. expected map[string]string // map[filename]contents
  32. }{
  33. {
  34. caseName: "multi", multi: true,
  35. expected: map[string]string{
  36. "sample_foo.txt": "foo\n",
  37. "sample_bar.txt": "bar\n",
  38. },
  39. },
  40. {
  41. caseName: "non-multi", multi: false,
  42. expected: map[string]string{
  43. "sample.txt": "foo\nbar\n",
  44. },
  45. },
  46. }
  47.  
  48. for _, c := range cases {
  49. t.Run(c.caseName, func(t *testing.T) {
  50. outputMap = map[string]*bytes.Buffer{}
  51. sample.Output([]string{"foo", "bar"}, c.multi)
  52.  
  53. if len(c.expected) != len(outputMap) {
  54. t.Errorf("the number of output files doesn't match (expected=%d, actual=%d)", len(c.expected), len(outputMap))
  55. return
  56. }
  57. for k, v := range c.expected {
  58. if a, ok := outputMap[k]; !ok {
  59. t.Errorf("file doesn't exist in output files (filename=%s)", k)
  60. return
  61. } else if a.String() != v {
  62. t.Errorf("file contents don't match (expected=%s, actual=%s)", v, a.String())
  63. return
  64. }
  65. }
  66. })
  67. }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement