go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/strpair/pair_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 strpair
    16  
    17  import (
    18  	"fmt"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  )
    23  
    24  func ExampleParse_hierarchical() {
    25  	pairs := []string{
    26  		"foo:bar",
    27  		"swarming_tag:a:1",
    28  		"swarming_tag:b:2",
    29  	}
    30  	tags := ParseMap(ParseMap(pairs)["swarming_tag"])
    31  	fmt.Println(tags.Get("a"))
    32  	fmt.Println(tags.Get("b"))
    33  	// Output:
    34  	// 1
    35  	// 2
    36  }
    37  
    38  func TestPairs(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	Convey("ParseMap invalid", t, func() {
    42  		k, v := Parse("foo")
    43  		So(k, ShouldEqual, "foo")
    44  		So(v, ShouldEqual, "")
    45  	})
    46  
    47  	m := Map{
    48  		"b": []string{"1"},
    49  		"a": []string{"2"},
    50  	}
    51  
    52  	Convey("Map", t, func() {
    53  		Convey("Format", func() {
    54  			So(m.Format(), ShouldResemble, []string{
    55  				"a:2",
    56  				"b:1",
    57  			})
    58  		})
    59  		Convey("Get non-existent", func() {
    60  			So(m.Get("c"), ShouldEqual, "")
    61  		})
    62  		Convey("Get with empty", func() {
    63  			m2 := m.Copy()
    64  			m2["c"] = nil
    65  			So(m2.Get("c"), ShouldEqual, "")
    66  		})
    67  		Convey("Contains", func() {
    68  			So(m.Contains("a", "2"), ShouldBeTrue)
    69  			So(m.Contains("a", "1"), ShouldBeFalse)
    70  			So(m.Contains("b", "1"), ShouldBeTrue)
    71  			So(m.Contains("c", "1"), ShouldBeFalse)
    72  		})
    73  		Convey("Set", func() {
    74  			m2 := m.Copy()
    75  			m2.Set("c", "3")
    76  			So(m2, ShouldResemble, Map{
    77  				"b": []string{"1"},
    78  				"a": []string{"2"},
    79  				"c": []string{"3"},
    80  			})
    81  		})
    82  		Convey("Del", func() {
    83  			m.Del("a")
    84  			So(m, ShouldResemble, Map{
    85  				"b": []string{"1"},
    86  			})
    87  		})
    88  	})
    89  }