package rfc3339 import ( "encoding/json" "fmt" "reflect" "testing" ) func TestIsLeapYear(t *testing.T) { tests := []struct { val uint16 exp bool } { { 1800, false }, { 1900, false }, { 1996, true }, { 1997, false }, { 1998, false }, { 1999, false }, { 2000, true }, { 2001, false }, { 2002, false }, { 2003, false }, { 2004, true }, { 2005, false }, { 2006, false }, { 2007, false }, { 2008, true }, { 2009, false }, { 2010, false }, { 2011, false }, { 2012, true }, { 2013, false }, { 2014, false }, { 2015, false }, { 2016, true }, { 2017, false }, { 2018, false }, { 2019, false }, { 2020, true }, { 2021, false }, { 2022, false }, { 2023, false }, { 2024, true }, { 2100, false }, { 2400, true }, } for _, test := range(tests) { t.Run(fmt.Sprintf("%d", test.val), func(t *testing.T) { got := isLeapYear(test.val) if got != test.exp { t.Errorf("got %v, exp %v", got, test.exp) } }) } } func TestGetMaxMonthDay(t *testing.T) { tests := []struct { y uint16 m uint16 exp uint16 } { { 2000, 2, 29 }, // test leap year { 2001, 2, 28 }, // test non-leap year { 2022, 1, 31 }, // test full non-leap year { 2022, 2, 28 }, { 2022, 3, 31 }, { 2022, 4, 30 }, { 2022, 5, 31 }, { 2022, 6, 30 }, { 2022, 7, 31 }, { 2022, 8, 31 }, { 2022, 9, 30 }, { 2022, 10, 31 }, { 2022, 11, 30 }, { 2022, 12, 31 }, } for _, test := range(tests) { t.Run(fmt.Sprintf("%04d-%02d", test.y, test.m), func(t *testing.T) { got := getMaxMonthDay(test.y, test.m) if got != test.exp { t.Errorf("got %v, exp %v", got, test.exp) } }) } } func TestParseDateComponent(t *testing.T) { tests := []struct { name string val string min uint16 max uint16 exp bool } { { "bad-uint", "abcd", 2000, 2001, false }, { "y-pass", "2000", 2000, 2001, true }, { "y-fail-lo", "1999", 2000, 2001, false }, { "y-fail-hi", "2002", 2000, 2001, false }, { "m-pass-1", "1", 1, 12, true }, { "m-pass-2", "2", 1, 12, true }, { "m-pass-3", "3", 1, 12, true }, { "m-pass-4", "4", 1, 12, true }, { "m-pass-5", "5", 1, 12, true }, { "m-pass-6", "6", 1, 12, true }, { "m-pass-7", "7", 1, 12, true }, { "m-pass-8", "8", 1, 12, true }, { "m-pass-9", "9", 1, 12, true }, { "m-pass-10", "10", 1, 12, true }, { "m-pass-11", "11", 1, 12, true }, { "m-pass-12", "12", 1, 12, true }, { "m-fail-lo", "0", 1, 12, false }, { "m-fail-hi", "13", 1, 12, false }, { "d-pass-1", "1", 1, 31, true }, { "d-pass-10", "10", 1, 31, true }, { "d-pass-30", "10", 1, 31, true }, { "d-fail-lo", "0", 1, 31, false }, { "d-fail-hi", "32", 1, 31, false }, } for _, test := range(tests) { t.Run(test.name, func(t *testing.T) { got, err := parseDateComponent("a", []byte(test.val), test.min, test.max) if err != nil && test.exp == true { t.Error(err) } else if err == nil && test.exp == false { t.Errorf("got %v, exp error", got) } }) } } func TestNewDate(t *testing.T) { tests := []struct { name string val string exp bool } { { "pass", "2000-01-03", true }, { "pass-lo", "1999-01-01", true }, { "pass-hi", "2126-12-31", true }, { "fail", "asdf", false }, { "fail-y-lo", "1998-01-03", false }, { "fail-y-hi", "2128-01-03", false }, { "fail-m-lo", "2126-00-03", false }, { "fail-m-hi", "2126-13-03", false }, { "fail-d-lo", "2126-01-00", false }, { "fail-d-hi", "2126-01-32", false }, { "fail-d-hi", "2126-02-29", false }, { "fail-d-hi", "2126-03-32", false }, { "fail-d-hi", "2126-04-31", false }, { "fail-d-hi", "2126-05-32", false }, { "fail-d-hi", "2126-06-31", false }, } for _, test := range(tests) { t.Run(test.name, func(t *testing.T) { if got, err := NewDate([]byte(test.val)); err != nil && test.exp { t.Error(err) } else if err == nil && !test.exp { t.Errorf("got %v, exp error", got) } }) } } func TestGetComponents(t *testing.T) { type date struct { y uint16 m uint16 d uint16 } tests := []struct { val string exp date } { { "2022-10-31", date { 2022, 10, 31 } }, } for _, test := range(tests) { t.Run(test.val, func(t *testing.T) { // create date dt, err := NewDate([]byte(test.val)) if err != nil { t.Error(err) return } // get components y, m, d := dt.GetComponents() got := date { y, m, d } // check components if !reflect.DeepEqual(got, test.exp) { t.Errorf("got %v, exp %v", got, test.exp) } }) } } func TestYear(t *testing.T) { tests := []struct { val string exp uint16 } { { "2022-10-31", 2022 }, } for _, test := range(tests) { t.Run(test.val, func(t *testing.T) { // create date dt, err := NewDate([]byte(test.val)) if err != nil { t.Error(err) return } // get year got := dt.Year() // check components if got != test.exp { t.Errorf("got year %d, exp %d", got, test.exp) } }) } } func TestMonth(t *testing.T) { tests := []struct { val string exp uint16 } { { "2022-10-31", 10 }, } for _, test := range(tests) { t.Run(test.val, func(t *testing.T) { // create date dt, err := NewDate([]byte(test.val)) if err != nil { t.Error(err) return } // get month got := dt.Month() // check components if got != test.exp { t.Errorf("got month %d, exp %d", got, test.exp) } }) } } func TestDay(t *testing.T) { tests := []struct { val string exp uint16 } { { "2022-10-31", 31 }, } for _, test := range(tests) { t.Run(test.val, func(t *testing.T) { // create date dt, err := NewDate([]byte(test.val)) if err != nil { t.Error(err) return } // get day got := dt.Day() // check components if got != test.exp { t.Errorf("got day %d, exp %d", got, test.exp) } }) } } func TestString(t *testing.T) { tests := []string { "2022-10-31", } for _, test := range(tests) { t.Run(test, func(t *testing.T) { // create date dt, err := NewDate([]byte(test)) if err != nil { t.Error(err) return } // get/check string got := dt.String() if got != test { t.Errorf("got %s, exp %s", got, test) } }) } } func TestDateUnmarshalJSON(t *testing.T) { passTests := []struct { val string exp string } { { `"2022-02-03"`, "2022-02-03" }, } for _, test := range(passTests) { var got Date if err := json.Unmarshal([]byte(test.val), &got); err != nil { t.Error(err) } else if got.String() != test.exp { t.Errorf("got \"%s\", exp \"%s\"", got.String(), test.exp) } } failTests := []struct { name string val string } { { "fail-str", "asdf" }, { "fail-parse", "\"asdf\"" }, } for _, test := range(failTests) { t.Run(test.name, func(t *testing.T) { var got Date if err := got.UnmarshalJSON([]byte(test.val)); err == nil { t.Errorf("got \"%v\" exp error", got) } }) } } func TestDateMarshalJSON(t *testing.T) { tests := []struct { val string exp string } { { "2022-10-31", `"2022-10-31"` }, } for _, test := range(tests) { t.Run(test.val, func(t *testing.T) { // create date dt, err := NewDate([]byte(test.val)) if err != nil { t.Error(err) return } // get/check string if got, err := dt.MarshalJSON(); err != nil { t.Error(err) } else if string(got) != test.exp { t.Errorf("got \"%s\", exp \"%s\"", got, test.exp) } }) } }