github.com/qioalice/ekago/v3@v3.3.2-0.20221202205325-5c262d586ee4/ekatime/time_test.go (about)

     1  // Copyright © 2020. All rights reserved.
     2  // Author: Ilya Stroy.
     3  // Contacts: iyuryevich@pm.me, https://github.com/qioalice
     4  // License: https://opensource.org/licenses/MIT
     5  
     6  package ekatime_test
     7  
     8  import (
     9  	"encoding/json"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/qioalice/ekago/v3/ekatime"
    14  
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestTime_String(t *testing.T) {
    19  	t1 := ekatime.NewTime(14, 2, 13)
    20  	t2 := ekatime.NewTime(23, 59, 59)
    21  	t3 := ekatime.NewTime(0, 0, 0)
    22  
    23  	require.EqualValues(t, "14:02:13", t1.String())
    24  	require.EqualValues(t, "23:59:59", t2.String())
    25  	require.EqualValues(t, "00:00:00", t3.String())
    26  }
    27  
    28  func BenchmarkTime_String_Cached(b *testing.B) {
    29  	t0 := ekatime.NewTime(14, 2, 13)
    30  	b.ResetTimer()
    31  	b.ReportAllocs()
    32  	for i := 0; i < b.N; i++ {
    33  		_ = t0.String()
    34  	}
    35  }
    36  
    37  func BenchmarkTime_String_FmtSprintf(b *testing.B) {
    38  	hh, mm, ss := ekatime.NewTime(14, 2, 13).Split()
    39  	b.ResetTimer()
    40  	b.ReportAllocs()
    41  	for i := 0; i < b.N; i++ {
    42  		_ = fmt.Sprintf("%02d:%02d:%02d", hh, mm, ss)
    43  	}
    44  }
    45  
    46  func TestTime_ParseFrom(t *testing.T) {
    47  	bt1 := []byte("000000")   // valid
    48  	bt2 := []byte("23:59:59") // valid
    49  	bt3 := []byte("23:59:5")  // invalid
    50  	bt4 := []byte("23:5959")  // invalid
    51  	bt5 := []byte("2359:5")   // invalid
    52  
    53  	var (
    54  		t1, t2, t3, t4, t5 ekatime.Time
    55  	)
    56  
    57  	err1 := t1.ParseFrom(bt1)
    58  	err2 := t2.ParseFrom(bt2)
    59  	err3 := t3.ParseFrom(bt3)
    60  	err4 := t4.ParseFrom(bt4)
    61  	err5 := t5.ParseFrom(bt5)
    62  
    63  	require.Equal(t, ekatime.NewTime(0, 0, 0), t1)
    64  	require.Equal(t, ekatime.NewTime(23, 59, 59), t2)
    65  
    66  	require.NoError(t, err1)
    67  	require.NoError(t, err2)
    68  	require.Error(t, err3)
    69  	require.Error(t, err4)
    70  	require.Error(t, err5)
    71  }
    72  
    73  func BenchmarkTime_ParseFrom(b *testing.B) {
    74  	bt0 := []byte("23:59:59")
    75  	var tt ekatime.Time
    76  	b.ResetTimer()
    77  	b.ReportAllocs()
    78  	for i := 0; i < b.N; i++ {
    79  		_ = tt.ParseFrom(bt0)
    80  	}
    81  }
    82  
    83  func TestTime_MarshalJSON(t *testing.T) {
    84  	var tt = struct {
    85  		T ekatime.Time `json:"t"`
    86  	}{
    87  		T: ekatime.NewTime(13, 14, 15),
    88  	}
    89  	d, err := json.Marshal(&tt)
    90  
    91  	require.NoError(t, err)
    92  	require.EqualValues(t, string(d), `{"t":"13:14:15"}`)
    93  }
    94  
    95  func TestTime_Replace(t *testing.T) {
    96  	tt := ekatime.NewTime(12, 13, 14)
    97  
    98  	tt = tt.Replace(13, 1, 2)
    99  	require.Equal(t, ekatime.NewTime(13, 1, 2), tt)
   100  
   101  	tt = tt.Replace(20, -2, 4)
   102  	require.Equal(t, ekatime.NewTime(20, 1, 4), tt)
   103  
   104  	tt = tt.Replace(1, 0, -50)
   105  	require.Equal(t, ekatime.NewTime(1, 0, 4), tt)
   106  
   107  	tt = tt.Replace(24, 61, 30)
   108  	require.Equal(t, ekatime.NewTime(1, 0, 30), tt)
   109  }
   110  
   111  func TestTime_Add(t *testing.T) {
   112  	tt := ekatime.NewTime(12, 13, 14)
   113  
   114  	tt = tt.Add(1, 2, 3)
   115  	require.Equal(t, ekatime.NewTime(13, 15, 17), tt)
   116  
   117  	tt = tt.Add(-3, 0, 20)
   118  	require.Equal(t, ekatime.NewTime(10, 15, 37), tt)
   119  
   120  	tt = tt.Add(-23, 0, 61)
   121  	require.Equal(t, ekatime.NewTime(11, 16, 38), tt)
   122  
   123  	tt = tt.Add(0, -60, 0)
   124  	require.Equal(t, ekatime.NewTime(10, 16, 38), tt)
   125  
   126  	tt = tt.Add(127, 0, 0)
   127  	require.Equal(t, ekatime.NewTime(17, 16, 38), tt)
   128  }