go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/flag/fixflagpos/fix_test.go (about)

     1  // Copyright 2017 The LUCI Authors.
     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 fixflagpos
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestFixSubcommands(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	s := func(args ...string) []string {
    27  		return args
    28  	}
    29  
    30  	Convey(`Fix`, t, func() {
    31  		tests := []struct {
    32  			Name string
    33  			In   []string
    34  			Out  []string
    35  		}{
    36  			{Name: "empty"},
    37  
    38  			{Name: "no func",
    39  				In:  s("hello", "world", "-flag", "100"),
    40  				Out: s("-flag", "100", "hello", "world")},
    41  
    42  			{Name: "no flags",
    43  				In:  s("hello", "world", "thing", "100"),
    44  				Out: s("hello", "world", "thing", "100")},
    45  
    46  			{Name: "positional after flags",
    47  				In:  s("-flag", "100", "hello", "world"),
    48  				Out: s("-flag", "100", "hello", "world")},
    49  
    50  			{Name: "positional mixed with flags",
    51  				In:  s("hello", "-flag", "100", "world"),
    52  				Out: s("-flag", "100", "world", "hello")},
    53  
    54  			{Name: "using --",
    55  				In:  s("hello", "--", "-flag", "100", "world"),
    56  				Out: s("--", "-flag", "100", "world", "hello")},
    57  		}
    58  		for _, tc := range tests {
    59  			tc := tc
    60  			Convey(tc.Name, func() {
    61  				So(Fix(tc.In), ShouldResemble, tc.Out)
    62  			})
    63  		}
    64  	})
    65  
    66  	Convey(`FixSubcommands`, t, func() {
    67  		tests := []struct {
    68  			Name string
    69  			In   []string
    70  			Out  []string
    71  		}{
    72  			{Name: "empty"},
    73  
    74  			{Name: "no flags",
    75  				In:  s("hello", "world", "thing", "100"),
    76  				Out: s("hello", "world", "thing", "100")},
    77  
    78  			{Name: "single subcommand",
    79  				In:  s("hello", "world", "-flag", "100"),
    80  				Out: s("hello", "-flag", "100", "world")},
    81  		}
    82  		for _, tc := range tests {
    83  			tc := tc
    84  			Convey(tc.Name, func() {
    85  				So(FixSubcommands(tc.In), ShouldResemble, tc.Out)
    86  			})
    87  		}
    88  	})
    89  }