json_test.go 1.2 KB

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