func TestTextMarshalling(t *testing.T) { var d date.Date cases := []struct { value date.Date want string }{ {date.New(-11111, time.February, 3), "-11111-02-03"}, {date.New(-1, time.December, 31), "-0001-12-31"}, {date.New(0, time.January, 1), "0000-01-01"}, {date.New(1, time.January, 1), "0001-01-01"}, {date.New(1970, time.January, 1), "1970-01-01"}, {date.New(2012, time.June, 25), "2012-06-25"}, {date.New(12345, time.June, 7), "+12345-06-07"}, } for _, c := range cases { bytes, err := c.value.MarshalText() if err != nil { t.Errorf("Text(%v) marshal error %v", c, err) } else if string(bytes) != c.want { t.Errorf("Text(%v) == %v, want %v", c.value, string(bytes), c.want) } else { err = d.UnmarshalText(bytes) if err != nil { t.Errorf("Text(%v) unmarshal error %v", c.value, err) } } } }
func TestPredicates(t *testing.T) { // The list of case dates must be sorted in ascending order cases := []struct { year int month time.Month day int }{ {-1234, time.February, 5}, {0, time.April, 12}, {1, time.January, 1}, {1946, time.February, 4}, {1970, time.January, 1}, {1976, time.April, 1}, {1999, time.December, 1}, {1111111, time.June, 21}, } for i, ci := range cases { di := date.New(ci.year, ci.month, ci.day) for j, cj := range cases { dj := date.New(cj.year, cj.month, cj.day) p := di.Equal(dj) q := i == j if p != q { t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q) } p = di.Before(dj) q = i < j if p != q { t.Errorf("Before(%v, %v) == %v, want %v", di, dj, p, q) } p = di.After(dj) q = i > j if p != q { t.Errorf("After(%v, %v) == %v, want %v", di, dj, p, q) } p = di == dj q = i == j if p != q { t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q) } p = di != dj q = i != j if p != q { t.Errorf("Equal(%v, %v) == %v, want %v", di, dj, p, q) } } } // Test IsZero zero := date.Date{} if !zero.IsZero() { t.Errorf("IsZero(%v) == false, want true", zero) } today := date.Today() if today.IsZero() { t.Errorf("IsZero(%v) == true, want false", today) } }
func TestInvalidText(t *testing.T) { cases := []struct { value string want string }{ {`not-a-date`, `Date.ParseISO: cannot parse not-a-date`}, {`215-08-15`, `Date.ParseISO: cannot parse 215-08-15`}, } for _, c := range cases { var d date.Date err := d.UnmarshalText([]byte(c.value)) if err == nil || err.Error() != c.want { t.Errorf("InvalidText(%v) == %v, want %v", c.value, err, c.want) } } }
func TestInvalidJSON(t *testing.T) { cases := []struct { value string want string }{ {`"not-a-date"`, `Date.ParseISO: cannot parse not-a-date`}, {`2015-08-15"`, `Date.UnmarshalJSON: missing double quotes (2015-08-15")`}, {`"2015-08-15`, `Date.UnmarshalJSON: missing double quotes ("2015-08-15)`}, {`"215-08-15"`, `Date.ParseISO: cannot parse 215-08-15`}, } for _, c := range cases { var d date.Date err := d.UnmarshalJSON([]byte(c.value)) if err == nil || err.Error() != c.want { t.Errorf("InvalidJSON(%v) == %v, want %v", c.value, err, c.want) } } }
func TestInvalidGob(t *testing.T) { cases := []struct { bytes []byte want string }{ {[]byte{}, "Date.UnmarshalBinary: no data"}, {[]byte{1, 2, 3}, "Date.UnmarshalBinary: invalid length"}, } for _, c := range cases { var ignored date.Date err := ignored.GobDecode(c.bytes) if err == nil || err.Error() != c.want { t.Errorf("InvalidGobDecode(%v) == %v, want %v", c.bytes, err, c.want) } err = ignored.UnmarshalBinary(c.bytes) if err == nil || err.Error() != c.want { t.Errorf("InvalidUnmarshalBinary(%v) == %v, want %v", c.bytes, err, c.want) } } }
func same(d date.Date, t time.Time) bool { yd, wd := d.ISOWeek() yt, wt := t.ISOWeek() return d.Year() == t.Year() && d.Month() == t.Month() && d.Day() == t.Day() && d.Weekday() == t.Weekday() && d.YearDay() == t.YearDay() && yd == yt && wd == wt }