git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/timex/max_test.go (about)

     1  package timex
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestMax(t *testing.T) {
    10  	now := time.Now().UTC()
    11  	afterNow := time.Now().UTC().Add(time.Hour)
    12  	type args struct {
    13  		x     time.Time
    14  		y     time.Time
    15  		times []time.Time
    16  	}
    17  	tests := []struct {
    18  		name string
    19  		args args
    20  		want time.Time
    21  	}{
    22  		{
    23  			name: "x < y",
    24  			args: args{
    25  				x:     now,
    26  				y:     afterNow,
    27  				times: []time.Time{},
    28  			},
    29  			want: afterNow,
    30  		},
    31  		{
    32  			name: "x > y",
    33  			args: args{
    34  				x:     afterNow,
    35  				y:     now,
    36  				times: []time.Time{},
    37  			},
    38  			want: afterNow,
    39  		},
    40  		{
    41  			name: "x > y",
    42  			args: args{
    43  				x:     afterNow,
    44  				y:     now,
    45  				times: []time.Time{},
    46  			},
    47  			want: afterNow,
    48  		},
    49  		{
    50  			name: "x = y < times",
    51  			args: args{
    52  				x: now,
    53  				y: now,
    54  				times: []time.Time{
    55  					now,
    56  					now,
    57  					afterNow,
    58  					now,
    59  				},
    60  			},
    61  			want: afterNow,
    62  		},
    63  		{
    64  			name: "x = y > times",
    65  			args: args{
    66  				x: afterNow,
    67  				y: afterNow,
    68  				times: []time.Time{
    69  					now,
    70  					now,
    71  					now,
    72  				},
    73  			},
    74  			want: afterNow,
    75  		},
    76  		{
    77  			name: "x = y",
    78  			args: args{
    79  				x:     now,
    80  				y:     now,
    81  				times: []time.Time{},
    82  			},
    83  			want: now,
    84  		},
    85  		{
    86  			name: "x = y = times",
    87  			args: args{
    88  				x: now,
    89  				y: now,
    90  				times: []time.Time{
    91  					now,
    92  					now,
    93  					now,
    94  					now,
    95  				},
    96  			},
    97  			want: now,
    98  		},
    99  	}
   100  	for _, tt := range tests {
   101  		t.Run(tt.name, func(t *testing.T) {
   102  			if got := Max(tt.args.x, tt.args.y, tt.args.times...); !reflect.DeepEqual(got, tt.want) {
   103  				t.Errorf("Max() = %v, want %v", got, tt.want)
   104  			}
   105  		})
   106  	}
   107  }