github.com/nf/docker@v1.8.1/pkg/timeutils/json_test.go (about)

     1  package timeutils
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  // Testing to ensure 'year' fields is between 0 and 9999
     9  func TestFastMarshalJSONWithInvalidDate(t *testing.T) {
    10  	aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
    11  	json, err := FastMarshalJSON(aTime)
    12  	if err == nil {
    13  		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
    14  	}
    15  	anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
    16  	json, err = FastMarshalJSON(anotherTime)
    17  	if err == nil {
    18  		t.Fatalf("FastMarshalJSON should throw an error, but was '%v'", json)
    19  	}
    20  
    21  }
    22  
    23  func TestFastMarshalJSON(t *testing.T) {
    24  	aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
    25  	json, err := FastMarshalJSON(aTime)
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	expected := "\"2015-05-29T11:01:02.000000003Z\""
    30  	if json != expected {
    31  		t.Fatalf("Expected %v, got %v", expected, json)
    32  	}
    33  
    34  	location, err := time.LoadLocation("Europe/Paris")
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
    39  	json, err = FastMarshalJSON(aTime)
    40  	if err != nil {
    41  		t.Fatal(err)
    42  	}
    43  	expected = "\"2015-05-29T11:01:02.000000003+02:00\""
    44  	if json != expected {
    45  		t.Fatalf("Expected %v, got %v", expected, json)
    46  	}
    47  }