bitbucket.org/ai69/amoy@v0.2.3/date_test.go (about)

     1  package amoy
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestDate_Preview(t *testing.T) {
    10  	t.Logf("ZeroDate = %v", ZeroDate)
    11  	t.Logf("MinDate  = %v", MinDate)
    12  	t.Logf("MaxDate  = %v", MaxDate)
    13  
    14  	t.Logf("Today    : %v", Today())
    15  	t.Logf("UTCToday : %v", UTCToday())
    16  	t.Logf("Yesterday: %v", Yesterday())
    17  	t.Logf("UTCYesterday: %v", UTCYesterday())
    18  	t.Logf("Tomorrow : %v", Tomorrow())
    19  	t.Logf("UTCTomorrow : %v", UTCTomorrow())
    20  
    21  	t.Logf("1stDayOfYear   : %v", FirstDayOfYear())
    22  	t.Logf("LastDayOfYear  : %v", LastDayOfYear())
    23  	t.Logf("1stDayOfMonth  : %v", FirstDayOfMonth())
    24  	t.Logf("LastDayOfMonth : %v", LastDayOfMonth())
    25  
    26  	t.Logf("ThisYear  : %v", ThisYear())
    27  	t.Logf("LastYear  : %v", LastYear())
    28  	t.Logf("NextYear  : %v", NextYear())
    29  }
    30  
    31  func TestDate_Sub(t *testing.T) {
    32  	tests := []struct {
    33  		name string
    34  		d    Date
    35  		t    Date
    36  		want time.Duration
    37  	}{
    38  		{"Sub Zero", NewDate(2021, 10, 1), NewDate(2021, 10, 1), ZeroDuration},
    39  		{"Sub 1d", NewDate(2021, 10, 2), NewDate(2021, 10, 3), Days(-1)},
    40  		{"Sub -1d", NewDate(2021, 10, 3), NewDate(2021, 10, 2), Days(1)},
    41  		{"Sub 10d", NewDate(2022, 11, 11), NewDate(2022, 11, 1), Days(10)},
    42  		{"First-1d", NewDate(1, 1, 1), NewDate(0, 12, 31), Days(1)},
    43  		{"Last+1d", NewDate(9999, 12, 31), NewDate(10000, 1, 1), Days(-1)},
    44  	}
    45  	for _, tt := range tests {
    46  		t.Run(tt.name, func(t *testing.T) {
    47  			if got := tt.d.Sub(tt.t); !reflect.DeepEqual(got, tt.want) {
    48  				t.Errorf("%v Sub(%v): got %v, want %v", tt.d, tt.t, got, tt.want)
    49  			}
    50  		})
    51  	}
    52  }
    53  
    54  func TestDate_Add(t *testing.T) {
    55  	tests := []struct {
    56  		name string
    57  		d    Date
    58  		t    time.Duration
    59  		want Date
    60  	}{
    61  		{"Add Zero", NewDate(2021, 10, 1), ZeroDuration, NewDate(2021, 10, 1)},
    62  		{"Add 1d", NewDate(2021, 10, 2), Days(1), NewDate(2021, 10, 3)},
    63  		{"Add -1d", NewDate(2021, 10, 3), Days(-1), NewDate(2021, 10, 2)},
    64  		{"First-1d", NewDate(1, 1, 1), Days(-1), NewDate(0, 12, 31)},
    65  		{"Last+1d", NewDate(9999, 12, 31), Days(1), NewDate(10000, 1, 1)},
    66  	}
    67  	for _, tt := range tests {
    68  		t.Run(tt.name, func(t *testing.T) {
    69  			if got := tt.d.Add(tt.t); !reflect.DeepEqual(got, tt.want) {
    70  				t.Errorf("%v Add(%v): got %v, want %v", tt.d, tt.t, got, tt.want)
    71  			}
    72  		})
    73  	}
    74  }
    75  
    76  func TestDate_AddDay(t *testing.T) {
    77  	tests := []struct {
    78  		name string
    79  		d    Date
    80  		n    int
    81  		want Date
    82  	}{
    83  		{"Add Zero", NewDate(2021, 10, 1), 0, NewDate(2021, 10, 1)},
    84  		{"Add 1d", NewDate(2021, 10, 2), 1, NewDate(2021, 10, 3)},
    85  		{"Add -1d", NewDate(2021, 10, 3), -1, NewDate(2021, 10, 2)},
    86  		{"Add 100d", NewDate(2021, 10, 2), 100, NewDate(2022, 1, 10)},
    87  		{"Add -100d", NewDate(2021, 10, 3), -100, NewDate(2021, 6, 25)},
    88  		{"Add 1000d", NewDate(2021, 1, 1), 1000, NewDate(2023, 9, 28)},
    89  		{"Add -1000d", NewDate(2021, 1, 1), -1000, NewDate(2018, 4, 7)},
    90  	}
    91  	for _, tt := range tests {
    92  		t.Run(tt.name, func(t *testing.T) {
    93  			if got := tt.d.AddDay(tt.n); !reflect.DeepEqual(got, tt.want) {
    94  				t.Errorf("%v AddDay(%v): got %v, want %v", tt.d, tt.n, got, tt.want)
    95  			}
    96  		})
    97  	}
    98  }
    99  
   100  func TestDate_AddMonth(t *testing.T) {
   101  	tests := []struct {
   102  		name string
   103  		d    Date
   104  		n    int
   105  		want Date
   106  	}{
   107  		{"Add Zero", NewDate(2021, 10, 1), 0, NewDate(2021, 10, 1)},
   108  		{"Add 1m", NewDate(2021, 10, 2), 1, NewDate(2021, 11, 2)},
   109  		{"Add -1m", NewDate(2021, 10, 3), -1, NewDate(2021, 9, 3)},
   110  		{"Add 3m", NewDate(2021, 10, 5), 3, NewDate(2022, 1, 5)},
   111  		{"Add -12m", NewDate(2021, 10, 6), -12, NewDate(2020, 10, 6)},
   112  		{"Add 100m", NewDate(2021, 1, 1), 100, NewDate(2029, 5, 1)},
   113  		{"Add -100m", NewDate(2021, 1, 1), -100, NewDate(2012, 9, 1)},
   114  		{"Add 1000m", NewDate(2021, 1, 1), 1000, NewDate(2104, 5, 1)},
   115  		{"Add -1000m", NewDate(2021, 1, 1), -1000, NewDate(1937, 9, 1)},
   116  	}
   117  	for _, tt := range tests {
   118  		t.Run(tt.name, func(t *testing.T) {
   119  			if got := tt.d.AddMonth(tt.n); !reflect.DeepEqual(got, tt.want) {
   120  				t.Errorf("%v AddMonth(%v): got %v, want %v", tt.d, tt.n, got, tt.want)
   121  			}
   122  		})
   123  	}
   124  }
   125  
   126  func TestDate_AddYear(t *testing.T) {
   127  	tests := []struct {
   128  		name string
   129  		d    Date
   130  		n    int
   131  		want Date
   132  	}{
   133  		{"Add Zero", NewDate(2021, 10, 1), 0, NewDate(2021, 10, 1)},
   134  		{"Add 1y", NewDate(2021, 10, 2), 1, NewDate(2022, 10, 2)},
   135  		{"Add -1y", NewDate(2021, 10, 3), -1, NewDate(2020, 10, 3)},
   136  		{"Add 3y", NewDate(2021, 10, 5), 3, NewDate(2024, 10, 5)},
   137  		{"Add -6y", NewDate(2021, 10, 6), -6, NewDate(2015, 10, 6)},
   138  	}
   139  	for _, tt := range tests {
   140  		t.Run(tt.name, func(t *testing.T) {
   141  			if got := tt.d.AddYear(tt.n); !reflect.DeepEqual(got, tt.want) {
   142  				t.Errorf("%v AddYear(%v): got %v, want %v", tt.d, tt.n, got, tt.want)
   143  			}
   144  		})
   145  	}
   146  }
   147  
   148  func TestDate_After(t *testing.T) {
   149  	tests := []struct {
   150  		name string
   151  		a    Date
   152  		b    Date
   153  		want bool
   154  	}{
   155  		{"Same", NewDate(2021, 10, 10), NewDate(2021, 10, 10), false},
   156  		{"Day_Before", NewDate(2021, 10, 9), NewDate(2021, 10, 10), false},
   157  		{"Day_After", NewDate(2021, 10, 11), NewDate(2021, 10, 10), true},
   158  		{"Month_Before", NewDate(2021, 10, 10), NewDate(2021, 11, 10), false},
   159  		{"Month_After", NewDate(2021, 10, 10), NewDate(2021, 9, 10), true},
   160  		{"Year_Before", NewDate(2021, 10, 10), NewDate(2022, 10, 10), false},
   161  		{"Year_After", NewDate(2021, 10, 10), NewDate(2020, 10, 10), true},
   162  	}
   163  	for _, tt := range tests {
   164  		t.Run(tt.name, func(t *testing.T) {
   165  			if got := tt.a.After(tt.b); got != tt.want {
   166  				t.Errorf("%v After(%v): got %v, want %v", tt.a, tt.b, got, tt.want)
   167  			}
   168  		})
   169  	}
   170  }
   171  
   172  func TestDate_Before(t *testing.T) {
   173  	tests := []struct {
   174  		name string
   175  		a    Date
   176  		b    Date
   177  		want bool
   178  	}{
   179  		{"Same", NewDate(2021, 10, 10), NewDate(2021, 10, 10), false},
   180  		{"Day_Before", NewDate(2021, 10, 9), NewDate(2021, 10, 10), true},
   181  		{"Day_After", NewDate(2021, 10, 11), NewDate(2021, 10, 10), false},
   182  		{"Month_Before", NewDate(2021, 10, 10), NewDate(2021, 11, 10), true},
   183  		{"Month_After", NewDate(2021, 10, 10), NewDate(2021, 9, 10), false},
   184  		{"Year_Before", NewDate(2021, 10, 10), NewDate(2022, 10, 10), true},
   185  		{"Year_After", NewDate(2021, 10, 10), NewDate(2020, 10, 10), false},
   186  	}
   187  	for _, tt := range tests {
   188  		t.Run(tt.name, func(t *testing.T) {
   189  			if got := tt.a.Before(tt.b); got != tt.want {
   190  				t.Errorf("%v Before(%v): got %v, want %v", tt.a, tt.b, got, tt.want)
   191  			}
   192  		})
   193  	}
   194  }
   195  
   196  func TestDate_Equal_IsSameDay(t *testing.T) {
   197  	tests := []struct {
   198  		name string
   199  		a    Date
   200  		b    Date
   201  		want bool
   202  	}{
   203  		{"Same", NewDate(2021, 10, 10), NewDate(2021, 10, 10), true},
   204  		{"Day_Before", NewDate(2021, 10, 9), NewDate(2021, 10, 10), false},
   205  		{"Day_After", NewDate(2021, 10, 11), NewDate(2021, 10, 10), false},
   206  		{"Month_Before", NewDate(2021, 10, 10), NewDate(2021, 11, 10), false},
   207  		{"Month_After", NewDate(2021, 10, 10), NewDate(2021, 9, 10), false},
   208  		{"Year_Before", NewDate(2021, 10, 10), NewDate(2022, 10, 10), false},
   209  		{"Year_After", NewDate(2021, 10, 10), NewDate(2020, 10, 10), false},
   210  	}
   211  	for _, tt := range tests {
   212  		t.Run(tt.name, func(t *testing.T) {
   213  			if got := tt.a.Equal(tt.b); got != tt.want {
   214  				t.Errorf("%v Equal(%v): got %v, want %v", tt.a, tt.b, got, tt.want)
   215  			}
   216  			if got := tt.b.IsSameDay(tt.a); got != tt.want {
   217  				t.Errorf("%v IsSameDay(%v): got %v, want %v", tt.b, tt.a, got, tt.want)
   218  			}
   219  		})
   220  	}
   221  }
   222  
   223  func TestDate_IsSameYear(t *testing.T) {
   224  	tests := []struct {
   225  		name string
   226  		a    Date
   227  		b    Date
   228  		want bool
   229  	}{
   230  		{"Same", NewDate(2021, 10, 10), NewDate(2021, 10, 10), true},
   231  		{"Day_Before", NewDate(2021, 10, 9), NewDate(2021, 10, 10), true},
   232  		{"Day_After", NewDate(2021, 10, 11), NewDate(2021, 10, 10), true},
   233  		{"Month_Before", NewDate(2021, 10, 10), NewDate(2021, 11, 10), true},
   234  		{"Month_After", NewDate(2021, 10, 10), NewDate(2021, 9, 10), true},
   235  		{"Year_Before", NewDate(2021, 10, 10), NewDate(2022, 10, 10), false},
   236  		{"Year_After", NewDate(2021, 10, 10), NewDate(2020, 10, 10), false},
   237  	}
   238  	for _, tt := range tests {
   239  		t.Run(tt.name, func(t *testing.T) {
   240  			if got := tt.a.IsSameYear(tt.b); got != tt.want {
   241  				t.Errorf("%v IsSameYear(%v): got %v, want %v", tt.a, tt.b, got, tt.want)
   242  			}
   243  		})
   244  	}
   245  }
   246  
   247  func TestDate_IsSameMonth(t *testing.T) {
   248  	tests := []struct {
   249  		name string
   250  		a    Date
   251  		b    Date
   252  		want bool
   253  	}{
   254  		{"Same", NewDate(2021, 10, 10), NewDate(2021, 10, 10), true},
   255  		{"Day_Before", NewDate(2021, 10, 9), NewDate(2021, 10, 10), true},
   256  		{"Day_After", NewDate(2021, 10, 11), NewDate(2021, 10, 10), true},
   257  		{"Month_Before", NewDate(2021, 10, 10), NewDate(2021, 11, 10), false},
   258  		{"Month_After", NewDate(2021, 10, 10), NewDate(2021, 9, 10), false},
   259  		{"Year_Before", NewDate(2021, 10, 10), NewDate(2022, 10, 10), false},
   260  		{"Year_After", NewDate(2021, 10, 10), NewDate(2020, 10, 10), false},
   261  	}
   262  	for _, tt := range tests {
   263  		t.Run(tt.name, func(t *testing.T) {
   264  			if got := tt.a.IsSameMonth(tt.b); got != tt.want {
   265  				t.Errorf("%v IsSameMonth(%v): got %v, want %v", tt.a, tt.b, got, tt.want)
   266  			}
   267  		})
   268  	}
   269  }
   270  
   271  func TestNewDate(t *testing.T) {
   272  	tests := []struct {
   273  		name  string
   274  		year  int
   275  		month int
   276  		day   int
   277  		want  Date
   278  	}{
   279  		{"Normal", 2021, 10, 19, Date{2021, 10, 19}},
   280  		{"Underflow Day", 2021, 10, 0, Date{2021, 9, 30}},
   281  		{"Overflow Day", 2021, 9, 31, Date{2021, 10, 1}},
   282  		{"Overflow Month", 2021, 14, 1, Date{2022, 2, 1}},
   283  		{"Overflow Day & Month", 2021, 14, 40, Date{2022, 3, 12}},
   284  	}
   285  	for _, tt := range tests {
   286  		t.Run(tt.name, func(t *testing.T) {
   287  			if got := NewDate(tt.year, tt.month, tt.day); !reflect.DeepEqual(got, tt.want) {
   288  				t.Errorf("NewDate(%d, %d, %d) = %v, want %v", tt.year, tt.month, tt.day, got, tt.want)
   289  			}
   290  		})
   291  	}
   292  }
   293  
   294  func TestParseDate(t *testing.T) {
   295  	tests := []struct {
   296  		name    string
   297  		s       string
   298  		want    *Date
   299  		wantErr bool
   300  	}{
   301  		{"Normal", "2021-10-01", &Date{2021, 10, 1}, false},
   302  		{"Extra", "2023-02-07 18:11:10 PST", nil, true},
   303  		{"Short", "2021-10-1", nil, true},
   304  		{"Overflow", "2021-09-31", nil, true},
   305  	}
   306  	for _, tt := range tests {
   307  		t.Run(tt.name, func(t *testing.T) {
   308  			got, err := ParseDate(tt.s)
   309  			if (err != nil) != tt.wantErr {
   310  				t.Errorf("ParseDate() error = %v, wantErr %v", err, tt.wantErr)
   311  				return
   312  			}
   313  			if !reflect.DeepEqual(got, tt.want) {
   314  				t.Errorf("ParseDate() got = %v, want %v", got, tt.want)
   315  			}
   316  		})
   317  	}
   318  }
   319  
   320  func TestDate_MarshalJSON(t *testing.T) {
   321  	tests := []struct {
   322  		name    string
   323  		d       Date
   324  		want    []byte
   325  		wantErr bool
   326  	}{
   327  		{"Normal", NewDate(2010, 12, 23), []byte(`"2010-12-23"`), false},
   328  		{"Digits", NewDate(2011, 1, 2), []byte(`"2011-01-02"`), false},
   329  		{"Zero", Date{}, []byte(`"0000-00-00"`), false},
   330  		{"Overflow", NewDate(10000, 1, 2), nil, true},
   331  	}
   332  	for _, tt := range tests {
   333  		t.Run(tt.name, func(t *testing.T) {
   334  			got, err := tt.d.MarshalJSON()
   335  			if (err != nil) != tt.wantErr {
   336  				t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
   337  				return
   338  			}
   339  			if !reflect.DeepEqual(got, tt.want) {
   340  				t.Errorf("MarshalJSON() got = %s, want %s", got, tt.want)
   341  			}
   342  		})
   343  	}
   344  }
   345  
   346  func TestDate_MarshalText(t *testing.T) {
   347  	tests := []struct {
   348  		name    string
   349  		d       Date
   350  		want    []byte
   351  		wantErr bool
   352  	}{
   353  		{"Normal", NewDate(2010, 12, 23), []byte(`2010-12-23`), false},
   354  		{"Digits", NewDate(2011, 1, 2), []byte(`2011-01-02`), false},
   355  		{"Zero", Date{}, []byte(`0000-00-00`), false},
   356  		{"Overflow", NewDate(10000, 1, 2), nil, true},
   357  	}
   358  	for _, tt := range tests {
   359  		t.Run(tt.name, func(t *testing.T) {
   360  			gotText, err := tt.d.MarshalText()
   361  			if (err != nil) != tt.wantErr {
   362  				t.Errorf("MarshalText() error = %v, wantErr %v", err, tt.wantErr)
   363  				return
   364  			}
   365  			if !reflect.DeepEqual(gotText, tt.want) {
   366  				t.Errorf("MarshalText() got = %s, want %s", gotText, tt.want)
   367  			}
   368  		})
   369  	}
   370  }
   371  
   372  func TestDate_UnmarshalJSON(t *testing.T) {
   373  	tests := []struct {
   374  		name    string
   375  		want    *Date
   376  		data    []byte
   377  		wantErr bool
   378  	}{
   379  		{"Normal", &Date{2010, 12, 23}, []byte(`"2010-12-23"`), false},
   380  		{"Digits", &Date{2011, 1, 2}, []byte(`"2011-01-02"`), false},
   381  		{"Zero", nil, []byte(`0000-00-00`), true},
   382  		{"Overflow", nil, []byte(`2011-01-32`), true},
   383  	}
   384  	for _, tt := range tests {
   385  		t.Run(tt.name, func(t *testing.T) {
   386  			var got Date
   387  			if err := got.UnmarshalJSON(tt.data); (err != nil) != tt.wantErr {
   388  				t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
   389  			}
   390  			if tt.want == nil {
   391  				return
   392  			}
   393  			if !reflect.DeepEqual(got, *tt.want) {
   394  				t.Errorf("UnmarshalJSON() got = %v, want %v", got, tt.want)
   395  			}
   396  		})
   397  	}
   398  }
   399  
   400  func TestDate_UnmarshalText(t *testing.T) {
   401  	tests := []struct {
   402  		name    string
   403  		want    *Date
   404  		data    []byte
   405  		wantErr bool
   406  	}{
   407  		{"Normal", &Date{2010, 12, 23}, []byte(`2010-12-23`), false},
   408  		{"Digits", &Date{2011, 1, 2}, []byte(`2011-01-02`), false},
   409  		{"Zero", nil, []byte(`0000-00-00`), true},
   410  		{"Overflow", nil, []byte(`2011-01-32`), true},
   411  	}
   412  	for _, tt := range tests {
   413  		t.Run(tt.name, func(t *testing.T) {
   414  			var got Date
   415  			if err := got.UnmarshalText(tt.data); (err != nil) != tt.wantErr {
   416  				t.Errorf("UnmarshalText() error = %v, wantErr %v", err, tt.wantErr)
   417  			}
   418  			if tt.want == nil {
   419  				return
   420  			}
   421  			if !reflect.DeepEqual(got, *tt.want) {
   422  				t.Errorf("UnmarshalText() got = %v, want %v", got, tt.want)
   423  			}
   424  		})
   425  	}
   426  }