github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/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  	"testing"
    19  
    20  	"github.com/stretchr/testify/assert"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestArgParser(t *testing.T) {
    25  	tests := []struct {
    26  		ap              *ArgParser
    27  		args            []string
    28  		expectedErr     error
    29  		expectedOptions map[string]string
    30  		expectedArgs    []string
    31  	}{
    32  		{
    33  			NewArgParser(),
    34  			[]string{},
    35  			nil,
    36  			map[string]string{},
    37  			[]string{},
    38  		},
    39  		{
    40  			NewArgParser(),
    41  			[]string{"arg1", "arg2"},
    42  			nil,
    43  			map[string]string{},
    44  			[]string{"arg1", "arg2"},
    45  		},
    46  		{
    47  			NewArgParser(),
    48  			[]string{"--unknown_flag"},
    49  			UnknownArgumentParam{"unknown_flag"},
    50  			map[string]string{},
    51  			[]string{},
    52  		},
    53  		{
    54  			NewArgParser(),
    55  			[]string{"--help"},
    56  			ErrHelp,
    57  			map[string]string{},
    58  			[]string{},
    59  		},
    60  		{
    61  			NewArgParser(),
    62  			[]string{"-h"},
    63  			ErrHelp,
    64  			map[string]string{},
    65  			[]string{},
    66  		},
    67  		{
    68  			NewArgParser(),
    69  			[]string{"help"},
    70  			nil,
    71  			map[string]string{},
    72  			[]string{"help"},
    73  		},
    74  		{
    75  			NewArgParser().SupportsString("param", "p", "", ""),
    76  			[]string{"--param", "value", "arg1"},
    77  			nil,
    78  			map[string]string{"param": "value"},
    79  			[]string{"arg1"},
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		apr, err := test.ap.Parse(test.args)
    85  		require.Equal(t, test.expectedErr, err)
    86  
    87  		if err == nil {
    88  			assert.Equal(t, test.expectedOptions, apr.options)
    89  			assert.Equal(t, test.expectedArgs, apr.args)
    90  		}
    91  	}
    92  }