github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/specs/types_test.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package specs_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/caas/specs"
    11  	"github.com/juju/juju/testing"
    12  )
    13  
    14  type typesSuite struct {
    15  	testing.BaseSuite
    16  }
    17  
    18  var _ = gc.Suite(&typesSuite{})
    19  
    20  var strVal = specs.IntOrString{Type: specs.String, StrVal: "10%"}
    21  var intVal = specs.IntOrString{Type: specs.Int, IntVal: 10}
    22  
    23  func (s *typesSuite) TestString(c *gc.C) {
    24  	c.Assert(strVal.String(), gc.DeepEquals, `10%`)
    25  	c.Assert(intVal.String(), gc.DeepEquals, `10`)
    26  }
    27  
    28  func (s *typesSuite) TestIntValue(c *gc.C) {
    29  	c.Assert(strVal.IntValue(), gc.DeepEquals, 0)
    30  	c.Assert(intVal.IntValue(), gc.DeepEquals, 10)
    31  }
    32  
    33  func (s *typesSuite) TestMarshalJSON(c *gc.C) {
    34  	o, err := strVal.MarshalJSON()
    35  	c.Assert(err, jc.ErrorIsNil)
    36  	c.Assert(o, gc.DeepEquals, []byte(`"10%"`))
    37  
    38  	o, err = intVal.MarshalJSON()
    39  	c.Assert(err, jc.ErrorIsNil)
    40  	c.Assert(o, gc.DeepEquals, []byte(`10`))
    41  }
    42  
    43  func (s *typesSuite) TestUnmarshalJSON(c *gc.C) {
    44  	var strVal1, intVal1 specs.IntOrString
    45  	err := strVal1.UnmarshalJSON([]byte(`"10%"`))
    46  	c.Assert(err, jc.ErrorIsNil)
    47  	c.Assert(strVal1, gc.DeepEquals, strVal)
    48  
    49  	err = intVal1.UnmarshalJSON([]byte(`10`))
    50  	c.Assert(err, jc.ErrorIsNil)
    51  	c.Assert(intVal1, gc.DeepEquals, intVal)
    52  }