github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/strutil/map_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package strutil_test
    21  
    22  import (
    23  	. "gopkg.in/check.v1"
    24  	"gopkg.in/yaml.v2"
    25  
    26  	"github.com/snapcore/snapd/strutil"
    27  )
    28  
    29  type orderedMapSuite struct{}
    30  
    31  var _ = Suite(&orderedMapSuite{})
    32  
    33  func (s *orderedMapSuite) TestBasic(c *C) {
    34  	om := strutil.NewOrderedMap(
    35  		"K1", "bar",
    36  		"K2", "baz",
    37  		"K0", "foo",
    38  	)
    39  
    40  	for i := 0; i < 100; i++ {
    41  		c.Assert(om.Keys(), DeepEquals, []string{"K1", "K2", "K0"})
    42  	}
    43  
    44  	c.Check(om.Get("K1"), Equals, "bar")
    45  	c.Check(om.Get("K2"), Equals, "baz")
    46  	c.Check(om.Get("K0"), Equals, "foo")
    47  
    48  	om.Del("K2")
    49  	c.Assert(om.Keys(), DeepEquals, []string{"K1", "K0"})
    50  
    51  	om.Set("K9", "foobar")
    52  	c.Assert(om.Keys(), DeepEquals, []string{"K1", "K0", "K9"})
    53  	c.Check(om.Get("K9"), Equals, "foobar")
    54  
    55  	om2 := om.Copy()
    56  	c.Assert(om2.Keys(), DeepEquals, []string{"K1", "K0", "K9"})
    57  
    58  	// replaces existing value, inserted at the end
    59  	om.Set("K1", "newbar")
    60  	c.Assert(om.Keys(), DeepEquals, []string{"K0", "K9", "K1"})
    61  	c.Check(om.Get("K1"), Equals, "newbar")
    62  }
    63  
    64  func (s *orderedMapSuite) TestYamlTrivial(c *C) {
    65  	yamlStr := []byte(`
    66  k: v
    67  k2: v2
    68  k0: v0
    69  `)
    70  	var om strutil.OrderedMap
    71  	err := yaml.Unmarshal(yamlStr, &om)
    72  	c.Assert(err, IsNil)
    73  	c.Check(om.Keys(), DeepEquals, []string{"k", "k2", "k0"})
    74  }
    75  
    76  func (s *orderedMapSuite) TestYamlDupe(c *C) {
    77  	yamlStr := []byte(`
    78  k: v
    79  k0: v0
    80  k: v
    81  `)
    82  	var om strutil.OrderedMap
    83  	err := yaml.Unmarshal(yamlStr, &om)
    84  	c.Assert(err, ErrorMatches, `found duplicate key "k"`)
    85  }
    86  
    87  func (s *orderedMapSuite) TestYamlErr(c *C) {
    88  	yamlStr := []byte(`
    89  k: 
    90   nested: var
    91  `)
    92  	var om strutil.OrderedMap
    93  	err := yaml.Unmarshal(yamlStr, &om)
    94  	c.Assert(err, ErrorMatches, "(?m)yaml: unmarshal error.*")
    95  }