From 7ffb9aeeec49f8f4b53db6cb22b56dc70a26ce70 Mon Sep 17 00:00:00 2001 From: gouguoyin <245629560@qq.com> Date: Sat, 31 Jul 2021 14:24:43 +0800 Subject: [PATCH] add coverage scenarios for unit tests --- calendar.lunar_test.go | 51 +++++++++++++++++++++++++++++++++++------- comparer_test.go | 13 ++++++----- creator_test.go | 1 + json_test.go | 4 ++-- parser_test.go | 12 ++++++---- traveler_test.go | 30 +++++++++++++++---------- 6 files changed, 80 insertions(+), 31 deletions(-) diff --git a/calendar.lunar_test.go b/calendar.lunar_test.go index 6fb49aa..ef584fd 100755 --- a/calendar.lunar_test.go +++ b/calendar.lunar_test.go @@ -129,10 +129,18 @@ func TestLunar_Month(t *testing.T) { {4, "00:00:00", 0}, {5, "0000-00-00 00:00:00", 0}, - {6, "2020-04-23", 4}, - {7, "2020-05-01", 4}, - {8, "2020-08-05", 6}, - {9, "2021-07-07", 5}, + {6, "2021-01-05", 11}, + {7, "2021-02-05", 12}, + {8, "2021-03-05", 1}, + {9, "2021-04-05", 2}, + {10, "2021-05-05", 3}, + {11, "2021-06-05", 4}, + {12, "2021-07-05", 5}, + {13, "2021-08-05", 6}, + {14, "2021-09-05", 7}, + {15, "2021-10-05", 8}, + {16, "2021-11-05", 10}, + {17, "2021-12-05", 11}, } for _, test := range tests { @@ -156,10 +164,37 @@ func TestLunar_Day(t *testing.T) { {4, "00:00:00", 0}, {5, "0000-00-00 00:00:00", 0}, - {6, "2020-04-23", 1}, - {7, "2020-05-01", 9}, - {8, "2020-08-05", 16}, - {9, "2021-07-07", 28}, + {6, "2020-08-01", 12}, + {7, "2020-08-02", 13}, + {8, "2020-08-03", 14}, + {9, "2020-08-04", 15}, + {10, "2020-08-05", 16}, + {11, "2020-08-06", 17}, + {12, "2020-08-07", 18}, + {13, "2020-08-08", 19}, + {14, "2020-08-09", 20}, + {15, "2020-08-10", 21}, + {16, "2020-08-11", 22}, + {17, "2020-08-12", 23}, + {18, "2020-08-13", 24}, + {19, "2020-08-14", 25}, + {20, "2020-08-15", 26}, + {21, "2020-08-16", 27}, + {22, "2020-08-17", 28}, + {23, "2020-08-18", 29}, + {24, "2020-08-19", 1}, + {25, "2020-08-20", 2}, + {26, "2020-08-21", 3}, + {27, "2020-08-22", 4}, + {28, "2020-08-23", 5}, + {29, "2020-08-24", 6}, + {30, "2020-08-25", 7}, + {31, "2020-08-26", 8}, + {32, "2020-08-27", 9}, + {33, "2020-08-28", 10}, + {34, "2020-08-29", 11}, + {35, "2020-08-30", 12}, + {36, "2020-08-31", 13}, } for _, test := range tests { diff --git a/comparer_test.go b/comparer_test.go index 13530e4..91a631e 100755 --- a/comparer_test.go +++ b/comparer_test.go @@ -157,11 +157,13 @@ func TestCarbon_IsLeapYear(t *testing.T) { {4, "00:00:00", false}, {5, "0000-00-00 00:00:00", false}, - {6, "2016-01-01", true}, - {7, "2017-01-01", false}, - {8, "2018-01-01", false}, - {9, "2019-01-01", false}, - {10, "2020-01-01", true}, + {6, "2015-01-01", false}, + {7, "2016-01-01", true}, + {8, "2017-01-01", false}, + {9, "2018-01-01", false}, + {10, "2019-01-01", false}, + {11, "2020-01-01", true}, + {12, "2021-01-01", false}, } for _, test := range tests { @@ -191,6 +193,7 @@ func TestCarbon_IsLongYear(t *testing.T) { {9, "2018-01-01", false}, {10, "2019-01-01", false}, {11, "2020-01-01", true}, + {12, "2021-01-01", false}, } for _, test := range tests { diff --git a/creator_test.go b/creator_test.go index 4aa4c6c..406ba25 100755 --- a/creator_test.go +++ b/creator_test.go @@ -17,6 +17,7 @@ func TestCarbon_CreateFromTimestamp(t *testing.T) { }{ {1, -1, "1970-01-01 07:59:59"}, {2, 0, "1970-01-01 08:00:00"}, + {2, 1, "1970-01-01 08:00:01"}, {4, 1577855655, "2020-01-01 13:14:15"}, {5, 1604074084682, "2020-10-31 00:08:04"}, {6, 1604074196366540, "2020-10-31 00:09:56"}, diff --git a/json_test.go b/json_test.go index edda583..1c5cbc3 100644 --- a/json_test.go +++ b/json_test.go @@ -81,7 +81,7 @@ func TestCarbon_UnmarshalJSON1(t *testing.T) { if err != nil { t.Fatal(err) } - fmt.Printf("Json string parse to person1:\n%+v\n", *person) + fmt.Printf("Json string parse to person:\n%+v\n", *person) } func TestCarbon_UnmarshalJSON2(t *testing.T) { @@ -103,7 +103,7 @@ func TestCarbon_UnmarshalJSON2(t *testing.T) { if err != nil { t.Fatal(err) } - fmt.Printf("Json string parse to person2:\n%+v\n", *person) + fmt.Printf("Json string parse to person:\n%+v\n", *person) } func TestError_UnmarshalJSON(t *testing.T) { diff --git a/parser_test.go b/parser_test.go index 65fb833..6ae0798 100755 --- a/parser_test.go +++ b/parser_test.go @@ -84,12 +84,14 @@ func TestCarbon_ParseByFormat(t *testing.T) { } func TestError_ParseByFormat(t *testing.T) { + assert := assert.New(t) + value, format, timezone := "2020-08-50", "Y-m-d", "xxx" c1 := ParseByFormat(value, format) - assert.Equal(t, invalidFormatError(value, format), c1.Error, "Should catch an exception in ParseByFormat()") + assert.Equal(invalidFormatError(value, format), c1.Error, "Should catch an exception in ParseByFormat()") c2 := ParseByFormat(value, format, timezone) - assert.Equal(t, invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByFormat()") + assert.Equal(invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByFormat()") } func TestCarbon_ParseByLayout(t *testing.T) { @@ -126,10 +128,12 @@ func TestCarbon_ParseByLayout(t *testing.T) { } func TestError_ParseByLayout(t *testing.T) { + assert := assert.New(t) + value, layout, timezone := "2020-08-50", "2006-01-02", "xxx" c1 := ParseByLayout(value, layout) - assert.Equal(t, invalidLayoutError(value, layout), c1.Error, "Should catch an exception in ParseByLayout()") + assert.Equal(invalidLayoutError(value, layout), c1.Error, "Should catch an exception in ParseByLayout()") c2 := ParseByLayout(value, layout, timezone) - assert.Equal(t, invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByLayout()") + assert.Equal(invalidTimezoneError(timezone), c2.Error, "Should catch an exception in ParseByLayout()") } diff --git a/traveler_test.go b/traveler_test.go index 1be9a21..c6fc132 100755 --- a/traveler_test.go +++ b/traveler_test.go @@ -553,7 +553,8 @@ func TestCarbon_AddQuarters(t *testing.T) { {8, "2020-01-01", 2, "2020-07-01"}, {9, "2020-02-28", 2, "2020-08-28"}, {10, "2020-02-29", 2, "2020-08-29"}, - {11, "2020-08-31", 2, "2021-03-03"}, + {11, "2020-08-05", 2, "2021-02-05"}, + {12, "2020-08-31", 2, "2021-03-03"}, } for _, test := range tests { @@ -583,7 +584,8 @@ func TestCarbon_AddQuartersNoOverflow(t *testing.T) { {8, "2020-01-01", 2, "2020-07-01"}, {9, "2020-02-28", 2, "2020-08-28"}, {10, "2020-02-29", 2, "2020-08-29"}, - {11, "2020-08-31", 2, "2021-02-28"}, + {11, "2020-08-05", 2, "2021-02-05"}, + {12, "2020-08-31", 2, "2021-02-28"}, } for _, test := range tests { @@ -613,7 +615,8 @@ func TestCarbon_SubQuarters(t *testing.T) { {8, "2020-01-01", 2, "2019-07-01"}, {9, "2020-02-28", 2, "2019-08-28"}, {10, "2020-02-29", 2, "2019-08-29"}, - {11, "2020-08-31", 2, "2020-03-02"}, + {11, "2020-08-05", 2, "2020-02-05"}, + {12, "2020-08-31", 2, "2020-03-02"}, } for _, test := range tests { @@ -643,7 +646,8 @@ func TestCarbon_SubQuartersNoOverflow(t *testing.T) { {8, "2020-01-01", 2, "2019-07-01"}, {9, "2020-02-28", 2, "2019-08-28"}, {10, "2020-02-29", 2, "2019-08-29"}, - {11, "2020-08-31", 2, "2020-02-29"}, + {11, "2020-08-05", 2, "2020-02-05"}, + {12, "2020-08-31", 2, "2020-02-29"}, } for _, test := range tests { @@ -729,10 +733,11 @@ func TestCarbon_SubQuarter(t *testing.T) { {6, "2019-04-01", "2019-01-01"}, {7, "2019-04-30", "2019-01-30"}, - {8, "2020-05-01", "2020-02-01"}, - {9, "2020-05-31", "2020-03-02"}, - {10, "2020-04-01", "2020-01-01"}, - {11, "2020-04-30", "2020-01-30"}, + {8, "2020-04-01", "2020-01-01"}, + {9, "2020-04-30", "2020-01-30"}, + {10, "2020-05-01", "2020-02-01"}, + {11, "2020-05-31", "2020-03-02"}, + {12, "2020-08-05", "2020-05-05"}, } for _, test := range tests { @@ -758,10 +763,11 @@ func TestCarbon_SubQuarterNoOverflow(t *testing.T) { {6, "2019-04-01", "2019-01-01"}, {7, "2019-04-30", "2019-01-30"}, - {8, "2020-05-01", "2020-02-01"}, - {9, "2020-05-31", "2020-02-29"}, - {10, "2020-04-01", "2020-01-01"}, - {11, "2020-04-30", "2020-01-30"}, + {8, "2020-04-01", "2020-01-01"}, + {9, "2020-04-30", "2020-01-30"}, + {10, "2020-05-01", "2020-02-01"}, + {11, "2020-05-31", "2020-02-29"}, + {12, "2020-08-05", "2020-05-05"}, } for _, test := range tests {