github.com/kbehouse/nsc@v0.0.6/cmd/numberparams_test.go (about) 1 /* 2 * Copyright 2018 The NATS Authors 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package cmd 17 18 import ( 19 "fmt" 20 "testing" 21 22 "github.com/spf13/cobra" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func numberEditorCmd(params *NumberParams) *cobra.Command { 27 cmd := &cobra.Command{ 28 Use: "env", 29 Short: fmt.Sprintf("Prints and manage the %s environment", GetToolName()), 30 SilenceErrors: false, 31 SilenceUsage: false, 32 Example: "env", 33 RunE: func(cmd *cobra.Command, args []string) error { 34 if InteractiveFlag { 35 if err := params.Edit("enter a number"); err != nil { 36 return err 37 } 38 } 39 return nil 40 }, 41 } 42 cmd.Flags().VarP(params, "number", "n", "some number") 43 44 return cmd 45 } 46 47 func TestNumberParams_BindFlags(t *testing.T) { 48 var p NumberParams 49 cmd := numberEditorCmd(&p) 50 _, _, err := ExecuteCmd(cmd, "--number", "10") 51 require.NoError(t, err) 52 53 require.Equal(t, NumberParams(10), p) 54 } 55 56 func TestNumberParams_Edit(t *testing.T) { 57 var p NumberParams 58 cmd := numberEditorCmd(&p) 59 _, _, err := ExecuteInteractiveCmd(cmd, []interface{}{"404"}) 60 require.NoError(t, err) 61 62 require.Equal(t, NumberParams(404), p) 63 }