stack_test.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package formatter
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestStackContextWrite(t *testing.T) {
  8. cases := []struct {
  9. context Context
  10. expected string
  11. }{
  12. // Errors
  13. {
  14. Context{Format: "{{InvalidFunction}}"},
  15. `Template parsing error: template: :1: function "InvalidFunction" not defined
  16. `,
  17. },
  18. {
  19. Context{Format: "{{nil}}"},
  20. `Template parsing error: template: :1:2: executing "" at <nil>: nil is not a command
  21. `,
  22. },
  23. // Table format
  24. {
  25. Context{Format: NewStackFormat("table")},
  26. `NAME SERVICES
  27. baz 2
  28. bar 1
  29. `,
  30. },
  31. {
  32. Context{Format: NewStackFormat("table {{.Name}}")},
  33. `NAME
  34. baz
  35. bar
  36. `,
  37. },
  38. // Custom Format
  39. {
  40. Context{Format: NewStackFormat("{{.Name}}")},
  41. `baz
  42. bar
  43. `,
  44. },
  45. }
  46. stacks := []*Stack{
  47. {Name: "baz", Services: 2},
  48. {Name: "bar", Services: 1},
  49. }
  50. for _, testcase := range cases {
  51. out := bytes.NewBufferString("")
  52. testcase.context.Output = out
  53. err := StackWrite(testcase.context, stacks)
  54. if err != nil {
  55. assert.Error(t, err, testcase.expected)
  56. } else {
  57. assert.Equal(t, out.String(), testcase.expected)
  58. }
  59. }
  60. }