github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/gadget/edition/number_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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 edition_test
    21  
    22  import (
    23  	"testing"
    24  
    25  	. "gopkg.in/check.v1"
    26  	"gopkg.in/yaml.v2"
    27  
    28  	"github.com/snapcore/snapd/gadget/edition"
    29  )
    30  
    31  type editionSuite struct{}
    32  
    33  var _ = Suite(&editionSuite{})
    34  
    35  func TestCommand(t *testing.T) { TestingT(t) }
    36  
    37  var mockEditionYaml = []byte(`
    38  foo:
    39    edition: 1
    40  `)
    41  
    42  func (s *editionSuite) TestUnmarshalIntegration(c *C) {
    43  	type editionStruct struct {
    44  		Edition edition.Number `yaml:"edition"`
    45  	}
    46  
    47  	for _, tc := range []struct {
    48  		input          string
    49  		expectedNumber edition.Number
    50  		expectedErr    string
    51  	}{
    52  		{"edition: 1", edition.Number(1), ""},
    53  		{"edition: 0", edition.Number(0), ""},
    54  		{"edition: 9999999", edition.Number(9999999), ""},
    55  		{"edition: -1", edition.Number(0), `"edition" must be a positive number, not "-1"`},
    56  		{"edition: random-string", edition.Number(0), `"edition" must be a positive number, not "random-string"`},
    57  		{"edition: NaN", edition.Number(0), `"edition" must be a positive number, not "NaN"`},
    58  		{"edition: 3.14", edition.Number(0), `"edition" must be a positive number, not "3.14"`},
    59  	} {
    60  		var en editionStruct
    61  		err := yaml.Unmarshal([]byte(tc.input), &en)
    62  		if tc.expectedErr != "" {
    63  			c.Assert(err, ErrorMatches, tc.expectedErr, Commentf(tc.input))
    64  		} else {
    65  			c.Assert(err, IsNil, Commentf(tc.input))
    66  			c.Check(en.Edition, Equals, tc.expectedNumber, Commentf(tc.input))
    67  		}
    68  	}
    69  }