github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/timeout/timeout_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 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 timeout
    21  
    22  import (
    23  	"encoding/json"
    24  	"testing"
    25  	"time"
    26  
    27  	. "gopkg.in/check.v1"
    28  	"gopkg.in/yaml.v2"
    29  )
    30  
    31  // Hook up check.v1 into the "go test" runner
    32  func Test(t *testing.T) { TestingT(t) }
    33  
    34  type TimeoutTestSuite struct {
    35  }
    36  
    37  var _ = Suite(&TimeoutTestSuite{})
    38  
    39  func (s *TimeoutTestSuite) TestTimeoutMarshal(c *C) {
    40  	bs, err := Timeout(DefaultTimeout).MarshalJSON()
    41  	c.Assert(err, IsNil)
    42  	c.Check(string(bs), Equals, `"30s"`)
    43  }
    44  
    45  type testT struct {
    46  	T Timeout `yaml:"T"`
    47  }
    48  
    49  func (s *TimeoutTestSuite) TestTimeoutMarshalIndirect(c *C) {
    50  	bs, err := json.Marshal(testT{DefaultTimeout})
    51  	c.Assert(err, IsNil)
    52  	c.Check(string(bs), Equals, `{"T":"30s"}`)
    53  }
    54  
    55  func (s *TimeoutTestSuite) TestTimeoutUnmarshalJSON(c *C) {
    56  	var t testT
    57  	c.Assert(json.Unmarshal([]byte(`{"T": "17ms"}`), &t), IsNil)
    58  	c.Check(t, DeepEquals, testT{T: Timeout(17 * time.Millisecond)})
    59  }
    60  
    61  func (s *TimeoutTestSuite) TestTimeoutUnmarshalYAML(c *C) {
    62  	var t testT
    63  	c.Assert(yaml.Unmarshal([]byte(`T: 17ms`), &t), IsNil)
    64  	c.Check(t, DeepEquals, testT{T: Timeout(17 * time.Millisecond)})
    65  }