github.com/ubuntu-core/snappy@v0.0.0-20210827154228-9e584df982bb/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  func (s *editionSuite) TestUnmarshalIntegration(c *C) {
    38  	type editionStruct struct {
    39  		Edition edition.Number `yaml:"edition"`
    40  	}
    41  
    42  	for _, tc := range []struct {
    43  		input          string
    44  		expectedNumber edition.Number
    45  		expectedErr    string
    46  	}{
    47  		{"edition: 1", edition.Number(1), ""},
    48  		{"edition: 0", edition.Number(0), ""},
    49  		{"edition: 9999999", edition.Number(9999999), ""},
    50  		{"edition: -1", edition.Number(0), `"edition" must be a positive number, not "-1"`},
    51  		{"edition: random-string", edition.Number(0), `"edition" must be a positive number, not "random-string"`},
    52  		{"edition: NaN", edition.Number(0), `"edition" must be a positive number, not "NaN"`},
    53  		{"edition: 3.14", edition.Number(0), `"edition" must be a positive number, not "3.14"`},
    54  	} {
    55  		var en editionStruct
    56  		err := yaml.Unmarshal([]byte(tc.input), &en)
    57  		if tc.expectedErr != "" {
    58  			c.Assert(err, ErrorMatches, tc.expectedErr, Commentf(tc.input))
    59  		} else {
    60  			c.Assert(err, IsNil, Commentf(tc.input))
    61  			c.Check(en.Edition, Equals, tc.expectedNumber, Commentf(tc.input))
    62  		}
    63  	}
    64  }