| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package subcommand |
| 6 |
| 7 import ( |
| 8 "errors" |
| 9 "flag" |
| 10 "testing" |
| 11 |
| 12 . "github.com/smartystreets/goconvey/convey" |
| 13 ) |
| 14 |
| 15 func TestNew(t *testing.T) { |
| 16 t.Parallel() |
| 17 Convey("When creating new subcommands", t, func() { |
| 18 reg := ®istry{make(map[string]*Subcommand)} |
| 19 |
| 20 Convey("you can register one command", func() { |
| 21 first := reg.new("first", "foo", "foofoo", nil, nil) |
| 22 So(first, ShouldNotBeNil) |
| 23 So(len(reg.subcommands), ShouldEqual, 1) |
| 24 |
| 25 Convey("and you can register a second command with a dif
ferent name", func() { |
| 26 second := reg.new("second", "bar", "barbar", nil
, nil) |
| 27 So(second, ShouldNotBeNil) |
| 28 So(len(reg.subcommands), ShouldEqual, 2) |
| 29 }) |
| 30 |
| 31 Convey("but you cannot register two commands with the sa
me name", func() { |
| 32 So(func() { reg.new("first", "bar", "barbar", ni
l, nil) }, ShouldPanic) |
| 33 }) |
| 34 }) |
| 35 }) |
| 36 } |
| 37 |
| 38 func TestGet(t *testing.T) { |
| 39 t.Parallel() |
| 40 Convey("When retrieving a subcommand", t, func() { |
| 41 reg := ®istry{make(map[string]*Subcommand)} |
| 42 cmd := reg.new("cmd", "short", "long", nil, nil) |
| 43 |
| 44 Convey("unknown names result in nil", func() { |
| 45 So(reg.get("test"), ShouldBeNil) |
| 46 }) |
| 47 |
| 48 Convey("recognized names should be identical to initial object",
func() { |
| 49 So(reg.get("cmd"), ShouldResemble, cmd) |
| 50 }) |
| 51 }) |
| 52 } |
| 53 |
| 54 func TestInitFlags(t *testing.T) { |
| 55 t.Parallel() |
| 56 Convey("When collecting flags from subcommands", t, func() { |
| 57 reg := ®istry{make(map[string]*Subcommand)} |
| 58 flags := flag.NewFlagSet("flags", flag.ContinueOnError) |
| 59 |
| 60 Convey("nil flag functions work fine", func() { |
| 61 sameflags := flag.NewFlagSet("flags", flag.ContinueOnErr
or) |
| 62 cmd := reg.new("cmd", "short", "long", nil, nil) |
| 63 cmd.InitFlags(flags) |
| 64 So(flags, ShouldResemble, sameflags) |
| 65 }) |
| 66 |
| 67 Convey("if new flags are defined", func() { |
| 68 // Define the flag function for the command. |
| 69 flagFn := func(f *flag.FlagSet) { |
| 70 f.String("number", "one", "a numeric string") |
| 71 f.Float64("pie", 3.14, "pi") |
| 72 } |
| 73 cmd := reg.new("cmd", "short", "long", flagFn, nil) |
| 74 |
| 75 Convey("pre-existing flags are left untouched", func() { |
| 76 flags.Bool("really", false, "really do it") |
| 77 cmd.InitFlags(flags) |
| 78 So(flags.Lookup("really"), ShouldNotBeNil) |
| 79 }) |
| 80 |
| 81 Convey("new flags are successfully added", func() { |
| 82 cmd.InitFlags(flags) |
| 83 So(flags.Lookup("pie"), ShouldNotBeNil) |
| 84 }) |
| 85 |
| 86 Convey("duplicate flags are disallowed", func() { |
| 87 flags.Int("number", 1, "an int") |
| 88 So(func() { cmd.InitFlags(flags) }, ShouldPanic) |
| 89 }) |
| 90 }) |
| 91 }) |
| 92 } |
| 93 |
| 94 func TestRun(t *testing.T) { |
| 95 t.Parallel() |
| 96 Convey("When running a subcommand", t, func() { |
| 97 reg := ®istry{make(map[string]*Subcommand)} |
| 98 flags := flag.NewFlagSet("flags", flag.ContinueOnError) |
| 99 |
| 100 Convey("nil run functions return nil", func() { |
| 101 cmd := reg.new("cmd", "short", "long", nil, nil) |
| 102 So(cmd.Run(flags), ShouldBeNil) |
| 103 }) |
| 104 |
| 105 Convey("success is propagated", func() { |
| 106 cmd := reg.new("cmd", "short", "long", nil, func(f *flag
.FlagSet) error { return nil }) |
| 107 So(cmd.Run(flags), ShouldBeNil) |
| 108 }) |
| 109 |
| 110 Convey("errors are propagated", func() { |
| 111 err := errors.New("an error") |
| 112 cmd := reg.new("cmd", "short", "long", nil, func(f *flag
.FlagSet) error { return err }) |
| 113 So(cmd.Run(flags), ShouldEqual, err) |
| 114 }) |
| 115 }) |
| 116 } |
| 117 |
| 118 func ExampleSubcommand_Help() { |
| 119 reg := ®istry{make(map[string]*Subcommand)} |
| 120 cmd := reg.new("test", "short", "long", nil, nil) |
| 121 flags := flag.NewFlagSet("test", flag.ContinueOnError) |
| 122 cmd.Help(flags) |
| 123 // Output: |
| 124 // short |
| 125 // |
| 126 // long |
| 127 } |
| 128 |
| 129 func ExampleTabulate() { |
| 130 reg := ®istry{make(map[string]*Subcommand)} |
| 131 _ = reg.new("first", "foo", "foofoo", nil, nil) |
| 132 _ = reg.new("second", "bar", "barbar", nil, nil) |
| 133 reg.tabulate() |
| 134 // Output: |
| 135 // first foo |
| 136 // second bar |
| 137 } |
| OLD | NEW |