-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput_test.go
113 lines (104 loc) · 2.74 KB
/
output_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cmd_test
import (
"launchpad.net/gnuflag"
. "launchpad.net/gocheck"
"launchpad.net/juju-core/cmd"
)
// OutputCommand is a command that uses the output.go formatters.
type OutputCommand struct {
out cmd.Output
value interface{}
}
func (c *OutputCommand) Info() *cmd.Info {
return &cmd.Info{"output", "<something>", "I like to output", "output"}
}
func (c *OutputCommand) Init(f *gnuflag.FlagSet, args []string) error {
c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
if err := f.Parse(true, args); err != nil {
return err
}
return cmd.CheckEmpty(f.Args())
}
func (c *OutputCommand) Run(ctx *cmd.Context) error {
return c.out.Write(ctx, c.value)
}
// use a struct to control field ordering.
var defaultValue = struct {
Juju int
Puppet bool
}{1, false}
var outputTests = map[string][]struct {
value interface{}
output string
}{
"": {
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
},
"smart": {
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", "hello\n"},
{"\n\n\n", "\n\n\n\n"},
{"foo: bar", "foo: bar\n"},
{[]string{"blam", "dink"}, "blam\ndink\n"},
{map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"},
},
"json": {
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", `"hello"` + "\n"},
{"\n\n\n", `"\n\n\n"` + "\n"},
{"foo: bar", `"foo: bar"` + "\n"},
{[]string{"blam", "dink"}, `["blam","dink"]` + "\n"},
{defaultValue, `{"Juju":1,"Puppet":false}` + "\n"},
},
"yaml": {
{1, "1\n"},
{-1, "-1\n"},
{1.1, "1.1\n"},
{true, "true\n"},
{false, "false\n"},
{"hello", "hello\n"},
{"\n\n\n", "'\n\n\n\n'\n"},
{"foo: bar", "'foo: bar'\n"},
{[]string{"blam", "dink"}, "- blam\n- dink\n"},
{defaultValue, "juju: 1\npuppet: false\n"},
},
}
func (s *CmdSuite) TestOutputFormat(c *C) {
for format, tests := range outputTests {
c.Logf("format %s", format)
args := []string{}
if format != "" {
args = []string{"--format", format}
}
for i, t := range tests {
c.Logf(" test %d", i)
ctx := dummyContext(c)
result := cmd.Main(&OutputCommand{value: t.value}, ctx, args)
c.Assert(result, Equals, 0)
c.Assert(bufferString(ctx.Stdout), Equals, t.output)
c.Assert(bufferString(ctx.Stderr), Equals, "")
}
}
ctx := dummyContext(c)
result := cmd.Main(&OutputCommand{}, ctx, []string{"--format", "cuneiform"})
c.Assert(result, Equals, 2)
c.Assert(bufferString(ctx.Stdout), Equals, "")
c.Assert(bufferString(ctx.Stderr), Matches, ".*: unknown format \"cuneiform\"\n")
}