github.com/xtls/xray-core@v1.8.12-0.20240518155711-3168d27b0bdb/infra/conf/cfgcommon/duration/duration_test.go (about)

     1  package duration_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/xtls/xray-core/infra/conf/cfgcommon/duration"
     9  )
    10  
    11  type testWithDuration struct {
    12  	Duration duration.Duration
    13  }
    14  
    15  func TestDurationJSON(t *testing.T) {
    16  	expected := &testWithDuration{
    17  		Duration: duration.Duration(time.Hour),
    18  	}
    19  	data, err := json.Marshal(expected)
    20  	if err != nil {
    21  		t.Error(err)
    22  		return
    23  	}
    24  	actual := &testWithDuration{}
    25  	err = json.Unmarshal(data, &actual)
    26  	if err != nil {
    27  		t.Error(err)
    28  		return
    29  	}
    30  	if actual.Duration != expected.Duration {
    31  		t.Errorf("expected: %s, actual: %s", time.Duration(expected.Duration), time.Duration(actual.Duration))
    32  	}
    33  }