|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +func TestBuildcommandNameOutputSingleCommand(t *testing.T) { |
| 26 | + command := &cobra.Command{Use: "command"} |
| 27 | + |
| 28 | + commandNameOutput := getPeerCommandFromCobraCommand(command) |
| 29 | + |
| 30 | + assertEqual(t, "command", commandNameOutput) |
| 31 | +} |
| 32 | + |
| 33 | +func TestBuildcommandNameOutputNilCommand(t *testing.T) { |
| 34 | + var command *cobra.Command |
| 35 | + |
| 36 | + commandNameOutput := getPeerCommandFromCobraCommand(command) |
| 37 | + |
| 38 | + assertEqual(t, "", commandNameOutput) |
| 39 | +} |
| 40 | + |
| 41 | +func TestBuildcommandNameOutputTwoCommands(t *testing.T) { |
| 42 | + rootCommand := &cobra.Command{Use: "rootcommand"} |
| 43 | + childCommand := &cobra.Command{Use: "childcommand"} |
| 44 | + rootCommand.AddCommand(childCommand) |
| 45 | + |
| 46 | + commandNameOutput := getPeerCommandFromCobraCommand(childCommand) |
| 47 | + |
| 48 | + assertEqual(t, "childcommand", commandNameOutput) |
| 49 | +} |
| 50 | + |
| 51 | +func TestBuildcommandNameOutputThreeCommands(t *testing.T) { |
| 52 | + rootCommand := &cobra.Command{Use: "rootcommand"} |
| 53 | + childCommand := &cobra.Command{Use: "childcommand"} |
| 54 | + leafCommand := &cobra.Command{Use: "leafCommand"} |
| 55 | + rootCommand.AddCommand(childCommand) |
| 56 | + childCommand.AddCommand(leafCommand) |
| 57 | + |
| 58 | + commandNameOutput := getPeerCommandFromCobraCommand(leafCommand) |
| 59 | + |
| 60 | + assertEqual(t, "childcommand", commandNameOutput) |
| 61 | +} |
| 62 | + |
| 63 | +func TestBuildcommandNameOutputFourCommands(t *testing.T) { |
| 64 | + rootCommand := &cobra.Command{Use: "rootcommand"} |
| 65 | + childCommand := &cobra.Command{Use: "childcommand"} |
| 66 | + secondChildCommand := &cobra.Command{Use: "secondChildCommand"} |
| 67 | + leafCommand := &cobra.Command{Use: "leafCommand"} |
| 68 | + |
| 69 | + rootCommand.AddCommand(childCommand) |
| 70 | + childCommand.AddCommand(secondChildCommand) |
| 71 | + secondChildCommand.AddCommand(leafCommand) |
| 72 | + |
| 73 | + commandNameOutput := getPeerCommandFromCobraCommand(leafCommand) |
| 74 | + |
| 75 | + assertEqual(t, "childcommand", commandNameOutput) |
| 76 | +} |
| 77 | + |
| 78 | +func assertEqual(t *testing.T, expected interface{}, actual interface{}) { |
| 79 | + if expected != actual { |
| 80 | + t.Errorf("Expected %v, got %v", expected, actual) |
| 81 | + } |
| 82 | +} |
0 commit comments