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

     1  // Copyright 2018 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 flag
    16  
    17  import (
    18  	"flag"
    19  	"io"
    20  	"testing"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  )
    24  
    25  func TestCommaList(t *testing.T) {
    26  	t.Parallel()
    27  	Convey("Given a FlagSet with a CommaList flag", t, func() {
    28  		fs := flag.NewFlagSet("test", flag.ContinueOnError)
    29  		fs.Usage = func() {}
    30  		fs.SetOutput(io.Discard)
    31  		var s []string
    32  		fs.Var(CommaList(&s), "list", "Some list")
    33  		Convey("When parsing with flag absent", func() {
    34  			err := fs.Parse([]string{})
    35  			Convey("The parsed slice should be empty", func() {
    36  				So(err, ShouldEqual, nil)
    37  				So(len(s), ShouldEqual, 0)
    38  			})
    39  		})
    40  		Convey("When parsing a single item", func() {
    41  			err := fs.Parse([]string{"-list", "foo"})
    42  			Convey("The parsed slice should contain the item", func() {
    43  				So(err, ShouldEqual, nil)
    44  				So(s, ShouldResemble, []string{"foo"})
    45  			})
    46  		})
    47  		Convey("When parsing multiple items", func() {
    48  			err := fs.Parse([]string{"-list", "foo,bar,spam"})
    49  			Convey("The parsed slice should contain the items", func() {
    50  				So(err, ShouldEqual, nil)
    51  				So(s, ShouldResemble, []string{"foo", "bar", "spam"})
    52  			})
    53  		})
    54  	})
    55  }