github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/argparser/parser_test.go (about) 1 // Copyright 2020 Dolthub, Inc. 2 // 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 package argparser 16 17 import ( 18 "errors" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestArgParser(t *testing.T) { 26 tests := []struct { 27 ap *ArgParser 28 args []string 29 expectedErr error 30 expectedOptions map[string]string 31 expectedArgs []string 32 }{ 33 { 34 NewArgParserWithVariableArgs("test"), 35 []string{}, 36 nil, 37 map[string]string{}, 38 []string{}, 39 }, 40 { 41 NewArgParserWithVariableArgs("test"), 42 []string{"arg1", "arg2"}, 43 nil, 44 map[string]string{}, 45 []string{"arg1", "arg2"}, 46 }, 47 { 48 NewArgParserWithVariableArgs("test"), 49 []string{"--unknown_flag"}, 50 UnknownArgumentParam{"unknown_flag"}, 51 map[string]string{}, 52 []string{}, 53 }, 54 { 55 NewArgParserWithVariableArgs("test"), 56 []string{"--help"}, 57 ErrHelp, 58 map[string]string{}, 59 []string{}, 60 }, 61 { 62 NewArgParserWithVariableArgs("test"), 63 []string{"-h"}, 64 ErrHelp, 65 map[string]string{}, 66 []string{}, 67 }, 68 { 69 NewArgParserWithVariableArgs("test"), 70 []string{"help"}, 71 nil, 72 map[string]string{}, 73 []string{"help"}, 74 }, 75 { 76 NewArgParserWithVariableArgs("test").SupportsString("param", "p", "", ""), 77 []string{"--param", "value", "arg1"}, 78 nil, 79 map[string]string{"param": "value"}, 80 []string{"arg1"}, 81 }, 82 { 83 NewArgParserWithVariableArgs("test").SupportsString("param", "p", "", ""), 84 []string{"-pvalue"}, 85 nil, 86 map[string]string{"param": "value"}, 87 []string{}, 88 }, 89 { 90 NewArgParserWithVariableArgs("test").SupportsString("param", "p", "", ""), 91 []string{"--paramvalue"}, 92 UnknownArgumentParam{"paramvalue"}, 93 map[string]string{}, 94 []string{}, 95 }, 96 { 97 NewArgParserWithMaxArgs("test", 1), 98 []string{"foo", "bar"}, 99 errors.New("error: test has too many positional arguments. Expected at most 1, found 2: foo, bar"), 100 map[string]string{}, 101 []string{}, 102 }, 103 } 104 105 for _, test := range tests { 106 apr, err := test.ap.Parse(test.args) 107 require.Equal(t, test.expectedErr, err) 108 109 if err == nil { 110 assert.Equal(t, test.expectedOptions, apr.options) 111 assert.Equal(t, test.expectedArgs, apr.Args) 112 } 113 } 114 }