github.com/mitranim/gg@v0.1.17/time_milli_test.go (about)

     1  package gg_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/mitranim/gg"
     8  	"github.com/mitranim/gg/gtest"
     9  )
    10  
    11  func TestTimeMilli(t *testing.T) {
    12  	t.Run(`UTC`, func(t *testing.T) {
    13  		defer gtest.Catch(t)
    14  		defer gg.SnapSwap(&time.Local, nil)
    15  		testTimeMilli(t)
    16  	})
    17  
    18  	t.Run(`non_UTC`, func(t *testing.T) {
    19  		defer gtest.Catch(t)
    20  		defer gg.SnapSwap(&time.Local, time.FixedZone(``, 60*60*3))
    21  		testTimeMilli(t)
    22  	})
    23  }
    24  
    25  func testTimeMilli(t *testing.T) {
    26  	t.Run(`IsNull`, func(t *testing.T) {
    27  		defer gtest.Catch(t)
    28  
    29  		gtest.True(gg.TimeMilli(0).IsNull())
    30  		gtest.False(gg.TimeMilli(-1).IsNull())
    31  		gtest.False(gg.TimeMilli(1).IsNull())
    32  	})
    33  
    34  	t.Run(`Clear`, func(t *testing.T) {
    35  		defer gtest.Catch(t)
    36  
    37  		gtest.NotPanic((*gg.TimeMilli)(nil).Clear)
    38  
    39  		tar := gg.TimeMilli(10)
    40  
    41  		tar.Clear()
    42  		gtest.Zero(tar)
    43  
    44  		tar.Clear()
    45  		gtest.Zero(tar)
    46  	})
    47  
    48  	t.Run(`Time`, func(t *testing.T) {
    49  		defer gtest.Catch(t)
    50  
    51  		gtest.Eq(gg.TimeMilli(0).Time(), time.UnixMilli(0))
    52  		gtest.Eq(gg.TimeMilli(1).Time(), time.UnixMilli(1))
    53  		gtest.Eq(gg.TimeMilli(123).Time(), time.UnixMilli(123))
    54  		gtest.Eq(gg.TimeMilli(123456).Time(), time.UnixMilli(123456))
    55  	})
    56  
    57  	t.Run(`Get`, func(t *testing.T) {
    58  		defer gtest.Catch(t)
    59  
    60  		gtest.Equal(gg.TimeMilli(0).Get(), any(nil))
    61  		gtest.Equal(gg.TimeMilli(1).Get(), any(time.UnixMilli(1)))
    62  		gtest.Equal(gg.TimeMilli(123).Get(), any(time.UnixMilli(123)))
    63  		gtest.Equal(gg.TimeMilli(123456).Get(), any(time.UnixMilli(123456)))
    64  	})
    65  
    66  	t.Run(`SetInt64`, func(t *testing.T) {
    67  		defer gtest.Catch(t)
    68  
    69  		var tar gg.TimeMilli
    70  
    71  		tar.SetInt64(123)
    72  		gtest.Eq(tar, 123)
    73  
    74  		tar.SetInt64(0)
    75  		gtest.Zero(tar)
    76  
    77  		tar.SetInt64(-123)
    78  		gtest.Eq(tar, -123)
    79  	})
    80  
    81  	t.Run(`SetTime`, func(t *testing.T) {
    82  		defer gtest.Catch(t)
    83  
    84  		var tar gg.TimeMilli
    85  
    86  		tar.SetTime(time.UnixMilli(-123))
    87  		gtest.Eq(tar, -123)
    88  
    89  		tar.SetTime(time.UnixMilli(0))
    90  		gtest.Zero(tar)
    91  
    92  		tar.SetTime(time.UnixMilli(123))
    93  		gtest.Eq(tar, 123)
    94  	})
    95  
    96  	t.Run(`Parse`, func(t *testing.T) {
    97  		defer gtest.Catch(t)
    98  		testTimeMilliParse(t, (*gg.TimeMilli).Parse)
    99  	})
   100  
   101  	t.Run(`String`, func(t *testing.T) {
   102  		defer gtest.Catch(t)
   103  		testTimeMilliString(gg.TimeMilli.String)
   104  	})
   105  
   106  	t.Run(`AppenderTo`, func(t *testing.T) {
   107  		defer gtest.Catch(t)
   108  
   109  		testTimeMilliString(gg.AppenderString[gg.TimeMilli])
   110  
   111  		test := func(src string, tar gg.TimeMilli, exp string) {
   112  			gtest.Eq(
   113  				gg.ToString(tar.AppendTo(gg.ToBytes(src))),
   114  				exp,
   115  			)
   116  		}
   117  
   118  		test(`<prefix>`, gg.TimeMilli(0), `<prefix>`)
   119  		test(`<prefix>`, gg.TimeMilli(1), `<prefix>1`)
   120  		test(`<prefix>`, gg.TimeMilli(-1), `<prefix>-1`)
   121  		test(`<prefix>`, gg.TimeMilli(123), `<prefix>123`)
   122  		test(`<prefix>`, gg.TimeMilli(-123), `<prefix>-123`)
   123  	})
   124  
   125  	t.Run(`MarshalText`, func(t *testing.T) {
   126  		defer gtest.Catch(t)
   127  		testTimeMilliString(timeMilliStringViaMarshalText)
   128  	})
   129  
   130  	t.Run(`UnmarshalText`, func(t *testing.T) {
   131  		defer gtest.Catch(t)
   132  		testTimeMilliParse(t, timeMilliParseViaUnmarshalText)
   133  	})
   134  
   135  	t.Run(`MarshalJSON`, func(t *testing.T) {
   136  		defer gtest.Catch(t)
   137  
   138  		test := func(src gg.TimeMilli, exp string) {
   139  			gtest.Eq(gg.ToString(gg.Try1(src.MarshalJSON())), exp)
   140  		}
   141  
   142  		test(0, `null`)
   143  		test(123, `123`)
   144  		test(-123, `-123`)
   145  	})
   146  
   147  	t.Run(`UnmarshalJSON`, func(t *testing.T) {
   148  		defer gtest.Catch(t)
   149  
   150  		test := func(src string, exp gg.TimeMilli) {
   151  			var tar gg.TimeMilli
   152  			gtest.NoError(tar.UnmarshalJSON(gg.ToBytes(src)))
   153  			gtest.Eq(tar, exp)
   154  		}
   155  
   156  		test(`null`, 0)
   157  
   158  		test(`123`, 123)
   159  		test(`-123`, -123)
   160  
   161  		test(`"0001-01-01T00:00:00Z"`, gg.TimeMilli(timeZeroToUnixMilli()))
   162  		test(`"1234-05-06T07:08:09.123456789Z"`, -23215049510877)
   163  		test(`"9234-05-06T07:08:09.123456789Z"`, 229240566489123)
   164  	})
   165  
   166  	t.Run(`Scan`, func(t *testing.T) {
   167  		defer gtest.Catch(t)
   168  
   169  		test := func(src any, exp gg.TimeMilli) {
   170  			var tar gg.TimeMilli
   171  
   172  			tar = 0
   173  			gtest.NoError(tar.Scan(src))
   174  			gtest.Eq(tar, exp)
   175  
   176  			tar = 123
   177  			gtest.NoError(tar.Scan(src))
   178  			gtest.Eq(tar, exp)
   179  		}
   180  
   181  		test(nil, 0)
   182  		test((*time.Time)(nil), 0)
   183  		test(``, 0)
   184  		test([]byte(nil), 0)
   185  		test([]byte{}, 0)
   186  
   187  		test(time.UnixMilli(0), 0)
   188  		test(time.UnixMilli(123), 123)
   189  		test(time.UnixMilli(-123), -123)
   190  
   191  		test(int64(0), 0)
   192  		test(int64(123), 123)
   193  		test(int64(-123), -123)
   194  
   195  		test(gg.TimeMilli(0), 0)
   196  		test(gg.TimeMilli(123), 123)
   197  		test(gg.TimeMilli(-123), -123)
   198  
   199  		test(`0`, 0)
   200  		test(`123`, 123)
   201  		test(`-123`, -123)
   202  
   203  		test(`0001-01-01T00:00:00Z`, gg.TimeMilli(timeZeroToUnixMilli()))
   204  		test(`1234-05-06T07:08:09.123456789Z`, -23215049510877)
   205  		test(`9234-05-06T07:08:09.123456789Z`, 229240566489123)
   206  	})
   207  }
   208  
   209  func testTimeMilliString(fun func(gg.TimeMilli) string) {
   210  	gtest.Eq(fun(gg.TimeMilli(0)), ``)
   211  	gtest.Eq(fun(gg.TimeMilli(1)), `1`)
   212  	gtest.Eq(fun(gg.TimeMilli(-1)), `-1`)
   213  	gtest.Eq(fun(gg.TimeMilli(123)), `123`)
   214  	gtest.Eq(fun(gg.TimeMilli(-123)), `-123`)
   215  }
   216  
   217  func timeMilliStringViaMarshalText(src gg.TimeMilli) string {
   218  	return gg.ToString(gg.Try1(src.MarshalText()))
   219  }
   220  
   221  func testTimeMilliParse(
   222  	t *testing.T,
   223  	fun func(*gg.TimeMilli, string) error,
   224  ) {
   225  	t.Run(`invalid`, func(t *testing.T) {
   226  		defer gtest.Catch(t)
   227  
   228  		var tar gg.TimeMilli
   229  
   230  		gtest.ErrorStr(
   231  			`parsing time "wtf" as "2006-01-02T15:04:05Z07:00"`,
   232  			fun(&tar, `wtf`),
   233  		)
   234  		gtest.Zero(tar)
   235  	})
   236  
   237  	t.Run(`empty`, func(t *testing.T) {
   238  		defer gtest.Catch(t)
   239  
   240  		tar := gg.TimeMilli(123)
   241  		gtest.NoError(fun(&tar, ``))
   242  		gtest.Zero(tar)
   243  	})
   244  
   245  	t.Run(`integer`, func(t *testing.T) {
   246  		defer gtest.Catch(t)
   247  
   248  		test := func(src string, exp gg.TimeMilli) {
   249  			var tar gg.TimeMilli
   250  			gtest.NoError(fun(&tar, src))
   251  			gtest.Eq(tar, exp)
   252  		}
   253  
   254  		test(`0`, 0)
   255  		test(`-0`, 0)
   256  		test(`+0`, 0)
   257  
   258  		test(`1`, 1)
   259  		test(`-1`, -1)
   260  		test(`+1`, +1)
   261  
   262  		test(`12`, 12)
   263  		test(`-12`, -12)
   264  		test(`+12`, +12)
   265  
   266  		test(`123`, 123)
   267  		test(`-123`, -123)
   268  		test(`+123`, +123)
   269  	})
   270  
   271  	t.Run(`RFC3339`, func(t *testing.T) {
   272  		defer gtest.Catch(t)
   273  
   274  		test := func(src string, exp gg.TimeMilli) {
   275  			var tar gg.TimeMilli
   276  			gtest.NoError(fun(&tar, src))
   277  			gtest.Eq(tar, exp)
   278  
   279  			inst := timeParse(src)
   280  
   281  			gtest.Eq(
   282  				tar.Time(),
   283  				time.UnixMilli(inst.UnixMilli()).In(inst.Location()),
   284  			)
   285  		}
   286  
   287  		test(`0001-01-01T00:00:00Z`, gg.TimeMilli(timeZeroToUnixMilli()))
   288  		test(`1234-05-06T07:08:09.123456789Z`, -23215049510877)
   289  		test(`9234-05-06T07:08:09.123456789Z`, 229240566489123)
   290  	})
   291  }
   292  
   293  func timeMilliParseViaUnmarshalText(tar *gg.TimeMilli, src string) error {
   294  	return tar.UnmarshalText(gg.ToBytes(src))
   295  }
   296  
   297  func timeZeroToUnixMilli() int64 { return time.Time{}.UnixMilli() }
   298  
   299  func BenchmarkTimeMilli_Parse_integer(b *testing.B) {
   300  	for ind := 0; ind < b.N; ind++ {
   301  		gg.TimeMilliParse(`-1234567890123`)
   302  	}
   303  }
   304  
   305  func BenchmarkTimeMilli_Parse_RFC3339(b *testing.B) {
   306  	for ind := 0; ind < b.N; ind++ {
   307  		gg.TimeMilliParse(`1234-05-06T07:08:09Z`)
   308  	}
   309  }