go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/flag/int64slice_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  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  )
    23  
    24  func TestInt64SliceFlag(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	Convey("one", t, func() {
    28  		var flag int64SliceFlag
    29  		So(flag.Set("1"), ShouldBeNil)
    30  		So(flag.Get(), ShouldResemble, []int64{1})
    31  		So(flag.String(), ShouldEqual, "1")
    32  	})
    33  
    34  	Convey("many", t, func() {
    35  		var flag int64SliceFlag
    36  		So(flag.Set("-1"), ShouldBeNil)
    37  		So(flag.Set("0"), ShouldBeNil)
    38  		So(flag.Set("1"), ShouldBeNil)
    39  		So(flag.Get(), ShouldResemble, []int64{-1, 0, 1})
    40  		So(flag.String(), ShouldEqual, "-1, 0, 1")
    41  	})
    42  
    43  	Convey("error", t, func() {
    44  		var flag int64SliceFlag
    45  		So(flag.Set("0x00"), ShouldErrLike, "values must be 64-bit integers")
    46  		So(flag, ShouldBeEmpty)
    47  		So(flag, ShouldBeEmpty)
    48  		So(flag.Get(), ShouldBeEmpty)
    49  		So(flag.String(), ShouldBeEmpty)
    50  	})
    51  
    52  	Convey("mixed", t, func() {
    53  		var flag int64SliceFlag
    54  		So(flag.Set("-1"), ShouldBeNil)
    55  		So(flag.Set("0x00"), ShouldErrLike, "values must be 64-bit integers")
    56  		So(flag.Set("1"), ShouldBeNil)
    57  		So(flag.Get(), ShouldResemble, []int64{-1, 1})
    58  		So(flag.String(), ShouldEqual, "-1, 1")
    59  	})
    60  }
    61  
    62  func TestInt64Slice(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	Convey("one", t, func() {
    66  		var i []int64
    67  		So(Int64Slice(&i).Set("1"), ShouldBeNil)
    68  		So(i, ShouldResemble, []int64{1})
    69  	})
    70  
    71  	Convey("many", t, func() {
    72  		var i []int64
    73  		So(Int64Slice(&i).Set("-1"), ShouldBeNil)
    74  		So(Int64Slice(&i).Set("0"), ShouldBeNil)
    75  		So(Int64Slice(&i).Set("1"), ShouldBeNil)
    76  		So(i, ShouldResemble, []int64{-1, 0, 1})
    77  	})
    78  
    79  	Convey("error", t, func() {
    80  		var i []int64
    81  		So(Int64Slice(&i).Set("0x00"), ShouldErrLike, "values must be 64-bit integers")
    82  		So(i, ShouldBeEmpty)
    83  	})
    84  
    85  	Convey("mixed", t, func() {
    86  		var i []int64
    87  		So(Int64Slice(&i).Set("-1"), ShouldBeNil)
    88  		So(Int64Slice(&i).Set("0x00"), ShouldErrLike, "values must be 64-bit integers")
    89  		So(Int64Slice(&i).Set("1"), ShouldBeNil)
    90  		So(i, ShouldResemble, []int64{-1, 1})
    91  	})
    92  }