github.com/orangenpresse/up@v0.6.0/config/duration_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/tj/assert"
     9  )
    10  
    11  func TestDuration_UnmarshalJSON(t *testing.T) {
    12  	t.Run("numeric seconds", func(t *testing.T) {
    13  		s := `{
    14        "timeout": 5
    15      }`
    16  
    17  		var c struct {
    18  			Timeout Duration
    19  		}
    20  
    21  		err := json.Unmarshal([]byte(s), &c)
    22  		assert.NoError(t, err, "unmarshal")
    23  
    24  		assert.Equal(t, Duration(5*time.Second), c.Timeout)
    25  	})
    26  
    27  	t.Run("string duration", func(t *testing.T) {
    28  		s := `{
    29        "timeout": "1.5m"
    30      }`
    31  
    32  		var c struct {
    33  			Timeout Duration
    34  		}
    35  
    36  		err := json.Unmarshal([]byte(s), &c)
    37  		assert.NoError(t, err, "unmarshal")
    38  
    39  		assert.Equal(t, Duration(90*time.Second), c.Timeout)
    40  	})
    41  }