gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/snap/revision_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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 snap_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"strconv"
    25  
    26  	. "gopkg.in/check.v1"
    27  	"gopkg.in/yaml.v2"
    28  
    29  	"github.com/snapcore/snapd/snap"
    30  )
    31  
    32  // Keep this in sync between snap and client packages.
    33  
    34  type revisionSuite struct{}
    35  
    36  var _ = Suite(&revisionSuite{})
    37  
    38  func (s revisionSuite) TestString(c *C) {
    39  	c.Assert(snap.R(0).String(), Equals, "unset")
    40  	c.Assert(snap.R(10).String(), Equals, "10")
    41  	c.Assert(snap.R(-9).String(), Equals, "x9")
    42  }
    43  
    44  func (s revisionSuite) TestUnset(c *C) {
    45  	c.Assert(snap.R(0).Unset(), Equals, true)
    46  	c.Assert(snap.R(10).Unset(), Equals, false)
    47  	c.Assert(snap.R(-9).Unset(), Equals, false)
    48  }
    49  
    50  func (s revisionSuite) TestLocal(c *C) {
    51  	c.Assert(snap.R(0).Local(), Equals, false)
    52  	c.Assert(snap.R(10).Local(), Equals, false)
    53  	c.Assert(snap.R(-9).Local(), Equals, true)
    54  }
    55  
    56  func (s revisionSuite) TestStore(c *C) {
    57  	c.Assert(snap.R(0).Store(), Equals, false)
    58  	c.Assert(snap.R(10).Store(), Equals, true)
    59  	c.Assert(snap.R(-9).Store(), Equals, false)
    60  }
    61  
    62  func (s revisionSuite) TestJSON(c *C) {
    63  	for _, n := range []int{0, 10, -9} {
    64  		r := snap.R(n)
    65  		data, err := json.Marshal(snap.R(n))
    66  		c.Assert(err, IsNil)
    67  		c.Assert(string(data), Equals, `"`+r.String()+`"`)
    68  
    69  		var got snap.Revision
    70  		err = json.Unmarshal(data, &got)
    71  		c.Assert(err, IsNil)
    72  		c.Assert(got, Equals, r)
    73  
    74  		got = snap.Revision{}
    75  		err = json.Unmarshal([]byte(strconv.Itoa(r.N)), &got)
    76  		c.Assert(err, IsNil)
    77  		c.Assert(got, Equals, r)
    78  	}
    79  }
    80  
    81  func (s revisionSuite) TestYAML(c *C) {
    82  	for _, v := range []struct {
    83  		n int
    84  		s string
    85  	}{
    86  		{0, "unset"},
    87  		{10, `"10"`},
    88  		{-9, "x9"},
    89  	} {
    90  		r := snap.R(v.n)
    91  		data, err := yaml.Marshal(snap.R(v.n))
    92  		c.Assert(err, IsNil)
    93  		c.Assert(string(data), Equals, v.s+"\n")
    94  
    95  		var got snap.Revision
    96  		err = yaml.Unmarshal(data, &got)
    97  		c.Assert(err, IsNil)
    98  		c.Assert(got, Equals, r)
    99  
   100  		got = snap.Revision{}
   101  		err = json.Unmarshal([]byte(strconv.Itoa(r.N)), &got)
   102  		c.Assert(err, IsNil)
   103  		c.Assert(got, Equals, r)
   104  	}
   105  }
   106  
   107  func (s revisionSuite) ParseRevision(c *C) {
   108  	type testItem struct {
   109  		s string
   110  		n int
   111  		e string
   112  	}
   113  
   114  	var tests = []testItem{{
   115  		s: "unset",
   116  		n: 0,
   117  	}, {
   118  		s: "x1",
   119  		n: -1,
   120  	}, {
   121  		s: "1",
   122  		n: 1,
   123  	}, {
   124  		s: "x-1",
   125  		e: `invalid snap revision: "x-1"`,
   126  	}, {
   127  		s: "x0",
   128  		e: `invalid snap revision: "x0"`,
   129  	}, {
   130  		s: "-1",
   131  		e: `invalid snap revision: "-1"`,
   132  	}, {
   133  		s: "0",
   134  		e: `invalid snap revision: "0"`,
   135  	}}
   136  
   137  	for _, test := range tests {
   138  		r, err := snap.ParseRevision(test.s)
   139  		if test.e != "" {
   140  			c.Assert(err.Error(), Equals, test.e)
   141  			continue
   142  		}
   143  		c.Assert(r, Equals, snap.R(test.n))
   144  	}
   145  }
   146  
   147  func (s *revisionSuite) TestR(c *C) {
   148  	type testItem struct {
   149  		v interface{}
   150  		n int
   151  		e string
   152  	}
   153  
   154  	var tests = []testItem{{
   155  		v: 0,
   156  		n: 0,
   157  	}, {
   158  		v: -1,
   159  		n: -1,
   160  	}, {
   161  		v: 1,
   162  		n: 1,
   163  	}, {
   164  		v: "unset",
   165  		n: 0,
   166  	}, {
   167  		v: "x1",
   168  		n: -1,
   169  	}, {
   170  		v: "1",
   171  		n: 1,
   172  	}, {
   173  		v: "x-1",
   174  		e: `invalid snap revision: "x-1"`,
   175  	}, {
   176  		v: "x0",
   177  		e: `invalid snap revision: "x0"`,
   178  	}, {
   179  		v: "-1",
   180  		e: `invalid snap revision: "-1"`,
   181  	}, {
   182  		v: "0",
   183  		e: `invalid snap revision: "0"`,
   184  	}, {
   185  		v: int64(1),
   186  		e: `cannot use 1 \(int64\) as a snap revision`,
   187  	}}
   188  
   189  	for _, test := range tests {
   190  		if test.e != "" {
   191  			f := func() { snap.R(test.v) }
   192  			c.Assert(f, PanicMatches, test.e)
   193  			continue
   194  		}
   195  
   196  		c.Assert(snap.R(test.v), Equals, snap.R(test.n))
   197  	}
   198  }