github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/models/api/v1/time_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestUnmarshall(t *testing.T) {
    10  	time := &Time{}
    11  	err := time.UnmarshalJSON([]byte(`"2006-01-02T15:04:05.000+0700"`))
    12  
    13  	if err != nil {
    14  		t.Fatalf("Error: %s", err)
    15  	}
    16  
    17  	time = &Time{}
    18  	err = time.UnmarshalJSON([]byte("abc"))
    19  
    20  	if err == nil {
    21  		t.Fatalf("Expected time.UnmarshalJSON to throw an error")
    22  	}
    23  }
    24  
    25  func TestMarshall(t *testing.T) {
    26  
    27  	time := &Time{}
    28  	in := `"2006-01-02T15:04:05.000+0700"`
    29  
    30  	err := time.UnmarshalJSON([]byte(in))
    31  	if err != nil {
    32  		t.Fatalf("Unexpected error %s", err)
    33  	}
    34  
    35  	outB, err := time.MarshalJSON()
    36  	if err != nil {
    37  		t.Fatalf("Unexpected error %s", err)
    38  	}
    39  
    40  	out := string(outB)
    41  
    42  	if in != out {
    43  		t.Fatalf("Expected %s got %s", in, out)
    44  	}
    45  }
    46  
    47  type testStruct struct{ Time Time }
    48  
    49  func Test(t *testing.T) {
    50  	out, err := json.Marshal(testStruct{Time(time.Now())})
    51  	if err != nil {
    52  		t.Fatalf("Failed due: %s", err)
    53  	}
    54  
    55  	t.Log(string(out))
    56  }