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