github.com/kat-co/cmd@v0.0.0-20140616103059-5da365f9d57e/output_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd_test 5 6 import ( 7 "launchpad.net/gnuflag" 8 gc "launchpad.net/gocheck" 9 10 "github.com/juju/cmd" 11 "github.com/juju/cmd/cmdtesting" 12 ) 13 14 // OutputCommand is a command that uses the output.go formatters. 15 type OutputCommand struct { 16 cmd.CommandBase 17 out cmd.Output 18 value interface{} 19 } 20 21 func (c *OutputCommand) Info() *cmd.Info { 22 return &cmd.Info{ 23 Name: "output", 24 Args: "<something>", 25 Purpose: "I like to output", 26 Doc: "output", 27 } 28 } 29 30 func (c *OutputCommand) SetFlags(f *gnuflag.FlagSet) { 31 c.out.AddFlags(f, "smart", cmd.DefaultFormatters) 32 } 33 34 func (c *OutputCommand) Init(args []string) error { 35 return cmd.CheckEmpty(args) 36 } 37 38 func (c *OutputCommand) Run(ctx *cmd.Context) error { 39 return c.out.Write(ctx, c.value) 40 } 41 42 // use a struct to control field ordering. 43 var defaultValue = struct { 44 Juju int 45 Puppet bool 46 }{1, false} 47 48 var outputTests = map[string][]struct { 49 value interface{} 50 output string 51 }{ 52 "": { 53 {nil, ""}, 54 {"", ""}, 55 {1, "1\n"}, 56 {-1, "-1\n"}, 57 {1.1, "1.1\n"}, 58 {true, "True\n"}, 59 {false, "False\n"}, 60 {"hello", "hello\n"}, 61 {"\n\n\n", "\n\n\n\n"}, 62 {"foo: bar", "foo: bar\n"}, 63 {[]string{"blam", "dink"}, "blam\ndink\n"}, 64 {map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"}, 65 }, 66 "smart": { 67 {nil, ""}, 68 {"", ""}, 69 {1, "1\n"}, 70 {-1, "-1\n"}, 71 {1.1, "1.1\n"}, 72 {true, "True\n"}, 73 {false, "False\n"}, 74 {"hello", "hello\n"}, 75 {"\n\n\n", "\n\n\n\n"}, 76 {"foo: bar", "foo: bar\n"}, 77 {[]string{"blam", "dink"}, "blam\ndink\n"}, 78 {[2]string{"blam", "dink"}, "blam\ndink\n"}, 79 {map[interface{}]interface{}{"foo": "bar"}, "foo: bar\n"}, 80 }, 81 "json": { 82 {nil, "null\n"}, 83 {"", `""` + "\n"}, 84 {1, "1\n"}, 85 {-1, "-1\n"}, 86 {1.1, "1.1\n"}, 87 {true, "true\n"}, 88 {false, "false\n"}, 89 {"hello", `"hello"` + "\n"}, 90 {"\n\n\n", `"\n\n\n"` + "\n"}, 91 {"foo: bar", `"foo: bar"` + "\n"}, 92 {[]string{}, `[]` + "\n"}, 93 {[]string{"blam", "dink"}, `["blam","dink"]` + "\n"}, 94 {defaultValue, `{"Juju":1,"Puppet":false}` + "\n"}, 95 }, 96 "yaml": { 97 {nil, ""}, 98 {"", `""` + "\n"}, 99 {1, "1\n"}, 100 {-1, "-1\n"}, 101 {1.1, "1.1\n"}, 102 {true, "true\n"}, 103 {false, "false\n"}, 104 {"hello", "hello\n"}, 105 {"\n\n\n", "'\n\n\n\n'\n"}, 106 {"foo: bar", "'foo: bar'\n"}, 107 {[]string{"blam", "dink"}, "- blam\n- dink\n"}, 108 {defaultValue, "juju: 1\npuppet: false\n"}, 109 }, 110 } 111 112 func (s *CmdSuite) TestOutputFormat(c *gc.C) { 113 for format, tests := range outputTests { 114 c.Logf("format %s", format) 115 var args []string 116 if format != "" { 117 args = []string{"--format", format} 118 } 119 for i, t := range tests { 120 c.Logf(" test %d", i) 121 ctx := cmdtesting.Context(c) 122 result := cmd.Main(&OutputCommand{value: t.value}, ctx, args) 123 c.Check(result, gc.Equals, 0) 124 c.Check(bufferString(ctx.Stdout), gc.Equals, t.output) 125 c.Check(bufferString(ctx.Stderr), gc.Equals, "") 126 } 127 } 128 } 129 130 func (s *CmdSuite) TestUnknownOutputFormat(c *gc.C) { 131 ctx := cmdtesting.Context(c) 132 result := cmd.Main(&OutputCommand{}, ctx, []string{"--format", "cuneiform"}) 133 c.Check(result, gc.Equals, 2) 134 c.Check(bufferString(ctx.Stdout), gc.Equals, "") 135 c.Check(bufferString(ctx.Stderr), gc.Matches, ".*: unknown format \"cuneiform\"\n") 136 } 137 138 // Py juju allowed both --format json and --format=json. This test verifies that juju is 139 // being built against a version of the gnuflag library (rev 14 or above) that supports 140 // this argument format. 141 // LP #1059921 142 func (s *CmdSuite) TestFormatAlternativeSyntax(c *gc.C) { 143 ctx := cmdtesting.Context(c) 144 result := cmd.Main(&OutputCommand{}, ctx, []string{"--format=json"}) 145 c.Assert(result, gc.Equals, 0) 146 c.Assert(bufferString(ctx.Stdout), gc.Equals, "null\n") 147 }