2015-12-15 14:49:41 -05:00
|
|
|
package jsonlog
|
2015-05-29 09:09:35 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Testing to ensure 'year' fields is between 0 and 9999
|
2015-12-15 14:49:41 -05:00
|
|
|
func TestFastTimeMarshalJSONWithInvalidDate(t *testing.T) {
|
2015-05-29 09:09:35 -04:00
|
|
|
aTime := time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local)
|
2015-12-15 14:49:41 -05:00
|
|
|
json, err := FastTimeMarshalJSON(aTime)
|
2015-05-29 09:09:35 -04:00
|
|
|
if err == nil {
|
2015-12-15 14:49:41 -05:00
|
|
|
t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
|
2015-05-29 09:09:35 -04:00
|
|
|
}
|
|
|
|
anotherTime := time.Date(10000, 1, 1, 0, 0, 0, 0, time.Local)
|
2015-12-15 14:49:41 -05:00
|
|
|
json, err = FastTimeMarshalJSON(anotherTime)
|
2015-05-29 09:09:35 -04:00
|
|
|
if err == nil {
|
2015-12-15 14:49:41 -05:00
|
|
|
t.Fatalf("FastTimeMarshalJSON should throw an error, but was '%v'", json)
|
2015-05-29 09:09:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-15 14:49:41 -05:00
|
|
|
func TestFastTimeMarshalJSON(t *testing.T) {
|
2015-05-29 09:09:35 -04:00
|
|
|
aTime := time.Date(2015, 5, 29, 11, 1, 2, 3, time.UTC)
|
2015-12-15 14:49:41 -05:00
|
|
|
json, err := FastTimeMarshalJSON(aTime)
|
2015-05-29 09:09:35 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected := "\"2015-05-29T11:01:02.000000003Z\""
|
|
|
|
if json != expected {
|
|
|
|
t.Fatalf("Expected %v, got %v", expected, json)
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := time.LoadLocation("Europe/Paris")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
aTime = time.Date(2015, 5, 29, 11, 1, 2, 3, location)
|
2015-12-15 14:49:41 -05:00
|
|
|
json, err = FastTimeMarshalJSON(aTime)
|
2015-05-29 09:09:35 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
expected = "\"2015-05-29T11:01:02.000000003+02:00\""
|
|
|
|
if json != expected {
|
|
|
|
t.Fatalf("Expected %v, got %v", expected, json)
|
|
|
|
}
|
|
|
|
}
|