github.com/crewjam/saml@v0.4.14/duration_test.go (about)

     1  package saml
     2  
     3  import (
     4  	"errors"
     5  	"strconv"
     6  	"testing"
     7  	"time"
     8  
     9  	"gotest.tools/assert"
    10  	is "gotest.tools/assert/cmp"
    11  )
    12  
    13  var durationMarshalTests = []struct {
    14  	in       time.Duration
    15  	expected []byte
    16  }{
    17  	{0, nil},
    18  	{time.Nanosecond, []byte("PT0.000000001S")},
    19  	{time.Millisecond, []byte("PT0.001S")},
    20  	{time.Second, []byte("PT1S")},
    21  	{time.Minute, []byte("PT1M")},
    22  	{time.Hour, []byte("PT1H")},
    23  	{-time.Hour, []byte("-PT1H")},
    24  	{2*time.Hour + 3*time.Minute + 4*time.Second + 5*time.Nanosecond, []byte("PT2H3M4.000000005S")},
    25  }
    26  
    27  func TestDuration(t *testing.T) {
    28  	for i, testCase := range durationMarshalTests {
    29  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    30  			actual, err := Duration(testCase.in).MarshalText()
    31  			assert.Check(t, err)
    32  			assert.Check(t, is.DeepEqual(testCase.expected, actual))
    33  		})
    34  	}
    35  }
    36  
    37  var durationUnmarshalTests = []struct {
    38  	in       []byte
    39  	expected time.Duration
    40  	err      error
    41  }{
    42  	{nil, 0, nil},
    43  	{[]byte("PT0.0000000001S"), 0, nil},
    44  	{[]byte("PT0.000000001S"), time.Nanosecond, nil},
    45  	{[]byte("PT0.001S"), time.Millisecond, nil},
    46  	{[]byte("PT1S"), time.Second, nil},
    47  	{[]byte("PT1M"), time.Minute, nil},
    48  	{[]byte("PT1H"), time.Hour, nil},
    49  	{[]byte("-PT1H"), -time.Hour, nil},
    50  	{[]byte("P1D"), 24 * time.Hour, nil},
    51  	{[]byte("P1M"), 720 * time.Hour, nil},
    52  	{[]byte("P1Y"), 8760 * time.Hour, nil},
    53  	{[]byte("P2Y3M4DT5H6M7.000000008S"), 19781*time.Hour + 6*time.Minute + 7*time.Second + 8*time.Nanosecond, nil},
    54  	{[]byte("P0Y0M0DT0H0M0S"), 0, nil},
    55  	{[]byte("PT0001.0000S"), time.Second, nil},
    56  	{[]byte(""), 0, errors.New("invalid duration ()")},
    57  	{[]byte("12345"), 0, errors.New("invalid duration (12345)")},
    58  	{[]byte("P1D1M1Y"), 0, errors.New("invalid duration (P1D1M1Y)")},
    59  	{[]byte("P1H1M1S"), 0, errors.New("invalid duration (P1H1M1S)")},
    60  	{[]byte("PT1S1M1H"), 0, errors.New("invalid duration (PT1S1M1H)")},
    61  	{[]byte(" P1Y "), 0, errors.New("invalid duration ( P1Y )")},
    62  	{[]byte("P"), 0, errors.New("invalid duration (P)")},
    63  	{[]byte("-P"), 0, errors.New("invalid duration (-P)")},
    64  	{[]byte("PT"), 0, errors.New("invalid duration (PT)")},
    65  	{[]byte("P1YMD"), 0, errors.New("invalid duration (P1YMD)")},
    66  	{[]byte("P1YT"), 0, errors.New("invalid duration (P1YT)")},
    67  	{[]byte("P-1Y"), 0, errors.New("invalid duration (P-1Y)")},
    68  	{[]byte("P1.5Y"), 0, errors.New("invalid duration (P1.5Y)")},
    69  	{[]byte("PT1.S"), 0, errors.New("invalid duration (PT1.S)")},
    70  }
    71  
    72  func TestDurationUnmarshal(t *testing.T) {
    73  	for i, testCase := range durationUnmarshalTests {
    74  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    75  			var actual Duration
    76  			err := actual.UnmarshalText(testCase.in)
    77  			if testCase.err == nil {
    78  				assert.Check(t, err)
    79  			} else {
    80  				assert.Check(t, is.Error(err, testCase.err.Error()))
    81  			}
    82  			assert.Check(t, is.Equal(Duration(testCase.expected), actual))
    83  		})
    84  	}
    85  }