From 011d3ec64ffd8ac884013d4a2ca5519f604b5d99 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 1 Nov 2023 19:22:11 -0700 Subject: [PATCH] initial commits --- temporal.go | 6 +-- temporal_filter_test.go | 28 ++++++----- temporal_gostring_test.go | 79 ++++++++++++++++--------------- temporal_is_defunct_test.go | 4 +- temporal_is_nothing_test.go | 4 +- temporal_marshaljson_bool_test.go | 10 ++-- 6 files changed, 68 insertions(+), 63 deletions(-) diff --git a/temporal.go b/temporal.go index 687e35d..890e626 100644 --- a/temporal.go +++ b/temporal.go @@ -25,11 +25,11 @@ func Permanent[T any](value T) Temporal[T] { } } -func Temporary[T any](value T, until int64) Temporal[T] { +func Temporary[T any](value T, until time.Time) Temporal[T] { return Temporal[T]{ istemporary:true, value:value, - until:until, + until:until.Unix(), } } @@ -64,7 +64,7 @@ func (receiver Temporal[T]) GoString() string { return fmt.Sprintf("tmp.Permanent[%T](%#v)", receiver.value, receiver.value) } - return fmt.Sprintf("tmp.Temporary[%T](%#v, %d)", receiver.value, receiver.value, receiver.until) + return fmt.Sprintf("tmp.Temporary[%T](%#v, time.Unix(%d, 0))", receiver.value, receiver.value, receiver.until) } func (receiver Temporal[T]) IsDefunct() bool { diff --git a/temporal_filter_test.go b/temporal_filter_test.go index 5c3378a..3480cd6 100644 --- a/temporal_filter_test.go +++ b/temporal_filter_test.go @@ -3,6 +3,8 @@ package tmp_test import ( "testing" + "time" + "sourcecode.social/reiver/go-tmp" ) @@ -43,46 +45,46 @@ func TestTemporal_Filter_int(t *testing.T) { { - Temporal: tmp.Temporary[int](-2, 9_223_372_036_854_775_807), // int64 problem - Expected: tmp.Temporary[int](-2, 9_223_372_036_854_775_807), + Temporal: tmp.Temporary[int](-2, time.Unix(9_223_372_036_854_775_807,0)), // int64 problem + Expected: tmp.Temporary[int](-2, time.Unix(9_223_372_036_854_775_807,0)), }, { - Temporal: tmp.Temporary[int](-1, 9_223_372_036_854_775_806), // int64 problem + Temporal: tmp.Temporary[int](-1, time.Unix(9_223_372_036_854_775_806,0)), // int64 problem Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](0, 9_223_372_036_854_775_805), // int64 problem - Expected: tmp.Temporary[int](0, 9_223_372_036_854_775_805), + Temporal: tmp.Temporary[int](0, time.Unix(9_223_372_036_854_775_805,0)), // int64 problem + Expected: tmp.Temporary[int](0, time.Unix(9_223_372_036_854_775_805,0)), }, { - Temporal: tmp.Temporary[int](1, 9_223_372_036_854_775_804), // int64 problem + Temporal: tmp.Temporary[int](1, time.Unix(9_223_372_036_854_775_804,0)), // int64 problem Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](2, 9_223_372_036_854_775_803), - Expected: tmp.Temporary[int](2, 9_223_372_036_854_775_803), // int64 problem + Temporal: tmp.Temporary[int](2, time.Unix(9_223_372_036_854_775_803,0)), + Expected: tmp.Temporary[int](2, time.Unix(9_223_372_036_854_775_803,0)), // int64 problem }, { - Temporal: tmp.Temporary[int](-2, 111), // supposed to be in the past + Temporal: tmp.Temporary[int](-2, time.Unix(111,0)), // supposed to be in the past Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](-1, 111), // supposed to be in the past + Temporal: tmp.Temporary[int](-1, time.Unix(111,0)), // supposed to be in the past Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](0, 111), // supposed to be in the past + Temporal: tmp.Temporary[int](0, time.Unix(111,0)), // supposed to be in the past Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](1, 111), // supposed to be in the past + Temporal: tmp.Temporary[int](1, time.Unix(111,0)), // supposed to be in the past Expected: tmp.Nothing[int](), }, { - Temporal: tmp.Temporary[int](2, 111), // supposed to be in the past + Temporal: tmp.Temporary[int](2, time.Unix(111,0)), // supposed to be in the past Expected: tmp.Nothing[int](), }, } diff --git a/temporal_gostring_test.go b/temporal_gostring_test.go index da258d8..a528634 100644 --- a/temporal_gostring_test.go +++ b/temporal_gostring_test.go @@ -4,6 +4,7 @@ import ( "testing" "fmt" + "time" "sourcecode.social/reiver/go-tmp" ) @@ -193,112 +194,112 @@ func TestTemporal_GoString_temporary(t *testing.T) { tests := []struct{ Value any - Until int64 + Until time.Time Expected string }{ { Value: "", - Until: 0, - Expected: `tmp.Temporary[string]("", 0)`, + Until: time.Unix(0, 0), + Expected: `tmp.Temporary[string]("", time.Unix(0, 0))`, }, { Value: "once twice thrice fource", - Until: 1234, - Expected: `tmp.Temporary[string]("once twice thrice fource", 1234)`, + Until: time.Unix(1234, 0), + Expected: `tmp.Temporary[string]("once twice thrice fource", time.Unix(1234, 0))`, }, { Value: "apple banana cherry", - Until: 979899, - Expected: `tmp.Temporary[string]("apple banana cherry", 979899)`, + Until: time.Unix(979899, 0), + Expected: `tmp.Temporary[string]("apple banana cherry", time.Unix(979899, 0))`, }, { Value: uint8 (0x0), - Until: 101, - Expected: `tmp.Temporary[uint8](0x0, 101)`, + Until: time.Unix(101, 0), + Expected: `tmp.Temporary[uint8](0x0, time.Unix(101, 0))`, }, { Value: uint8 (0x1), - Until: 111, - Expected: `tmp.Temporary[uint8](0x1, 111)`, + Until: time.Unix(111, 0), + Expected: `tmp.Temporary[uint8](0x1, time.Unix(111, 0))`, }, { Value: uint8 (0x2), - Until: 121, - Expected: `tmp.Temporary[uint8](0x2, 121)`, + Until: time.Unix(121, 0), + Expected: `tmp.Temporary[uint8](0x2, time.Unix(121, 0))`, }, { Value: uint8 (0xfe), - Until: 989, - Expected: `tmp.Temporary[uint8](0xfe, 989)`, + Until: time.Unix(989, 0), + Expected: `tmp.Temporary[uint8](0xfe, time.Unix(989, 0))`, }, { Value: uint8 (0xff), - Until: 999, - Expected: `tmp.Temporary[uint8](0xff, 999)`, + Until: time.Unix(999, 0), + Expected: `tmp.Temporary[uint8](0xff, time.Unix(999, 0))`, }, { Value: uint16 (0x0), - Until: 303, - Expected: `tmp.Temporary[uint16](0x0, 303)`, + Until: time.Unix(303, 0), + Expected: `tmp.Temporary[uint16](0x0, time.Unix(303, 0))`, }, { Value: uint16 (0x1), - Until: 313, - Expected: `tmp.Temporary[uint16](0x1, 313)`, + Until: time.Unix(313, 0), + Expected: `tmp.Temporary[uint16](0x1, time.Unix(313, 0))`, }, { Value: uint16 (0x2), - Until: 323, - Expected: `tmp.Temporary[uint16](0x2, 323)`, + Until: time.Unix(323, 0), + Expected: `tmp.Temporary[uint16](0x2, time.Unix(323, 0))`, }, { Value: uint16 (0xfe), - Until: 383, - Expected: `tmp.Temporary[uint16](0xfe, 383)`, + Until: time.Unix(383, 0), + Expected: `tmp.Temporary[uint16](0xfe, time.Unix(383, 0))`, }, { Value: uint16 (0xff), - Until: 393, - Expected: `tmp.Temporary[uint16](0xff, 393)`, + Until: time.Unix(393, 0), + Expected: `tmp.Temporary[uint16](0xff, time.Unix(393, 0))`, }, { Value: uint16 (0x100), - Until: 3003, - Expected: `tmp.Temporary[uint16](0x100, 3003)`, + Until: time.Unix(3003, 0), + Expected: `tmp.Temporary[uint16](0x100, time.Unix(3003, 0))`, }, { Value: uint16 (0x101), - Until: 3113, - Expected: `tmp.Temporary[uint16](0x101, 3113)`, + Until: time.Unix(3113, 0), + Expected: `tmp.Temporary[uint16](0x101, time.Unix(3113, 0))`, }, { Value: uint16 (0x102), - Until: 3223, - Expected: `tmp.Temporary[uint16](0x102, 3223)`, + Until: time.Unix(3223, 0), + Expected: `tmp.Temporary[uint16](0x102, time.Unix(3223, 0))`, }, { Value: uint16 (0xfffe), - Until: 3883, - Expected: `tmp.Temporary[uint16](0xfffe, 3883)`, + Until: time.Unix(3883, 0), + Expected: `tmp.Temporary[uint16](0xfffe, time.Unix(3883, 0))`, }, { Value: uint16 (0xffff), - Until: 3993, - Expected: `tmp.Temporary[uint16](0xffff, 3993)`, + Until: time.Unix(3993, 0), + Expected: `tmp.Temporary[uint16](0xffff, time.Unix(3993, 0))`, }, { Value: struct { A string; B int }{A:"joeblow",B:7}, - Until: 9876543210, - Expected: `tmp.Temporary[struct { A string; B int }](struct { A string; B int }{A:"joeblow", B:7}, 9876543210)`, + Until: time.Unix(9876543210, 0), + Expected: `tmp.Temporary[struct { A string; B int }](struct { A string; B int }{A:"joeblow", B:7}, time.Unix(9876543210, 0))`, }, } diff --git a/temporal_is_defunct_test.go b/temporal_is_defunct_test.go index ccf37e1..f9cb61a 100644 --- a/temporal_is_defunct_test.go +++ b/temporal_is_defunct_test.go @@ -23,11 +23,11 @@ func TestTemporal_IsDefunct(t *testing.T) { Expected: false, }, { - Temporal: tmp.Temporary[string]("expired", time.Now().Unix()-999), // supposed to be in the pastasx + Temporal: tmp.Temporary[string]("expired", time.Unix(time.Now().Unix()-999,0)), // supposed to be in the pastasx Expected: true, }, { - Temporal: tmp.Temporary[string]("not-expired", time.Now().Unix()+99999), + Temporal: tmp.Temporary[string]("not-expired", time.Unix(time.Now().Unix()+99999,0)), Expected: false, }, } diff --git a/temporal_is_nothing_test.go b/temporal_is_nothing_test.go index 31ceda3..3ac5f60 100644 --- a/temporal_is_nothing_test.go +++ b/temporal_is_nothing_test.go @@ -21,11 +21,11 @@ func TestTemporal_IsNothing(t *testing.T) { Expected: false, }, { - Temporal: Temporary[string]("expired", time.Now().Unix()-999), + Temporal: Temporary[string]("expired", time.Unix(time.Now().Unix()-999,0)), Expected: false, }, { - Temporal: Temporary[string]("not-expired", time.Now().Unix()+99999), + Temporal: Temporary[string]("not-expired", time.Unix(time.Now().Unix()+99999,0)), Expected: false, }, } diff --git a/temporal_marshaljson_bool_test.go b/temporal_marshaljson_bool_test.go index 209e2e0..94667c0 100644 --- a/temporal_marshaljson_bool_test.go +++ b/temporal_marshaljson_bool_test.go @@ -3,6 +3,8 @@ package tmp_test import ( "testing" + "time" + "sourcecode.social/reiver/go-tmp" ) @@ -24,11 +26,11 @@ func TestTemporal_MarshalJSON_bool(t *testing.T) { { - Value: tmp.Temporary(false, 9_223_372_036_854_775_807), // supposed to be a time far in the future + Value: tmp.Temporary(false, time.Unix(9_223_372_036_854_775_807,0)), // supposed to be a time far in the future Expected: "false", }, { - Value: tmp.Temporary(true, 9_223_372_036_854_775_806), // supposed to be a time far in the future + Value: tmp.Temporary(true, time.Unix(9_223_372_036_854_775_806,0)), // supposed to be a time far in the future Expected: "true", }, } @@ -68,10 +70,10 @@ func TestTemporal_MarshalJSON_bool_fail(t *testing.T) { { - Value: tmp.Temporary(false, 1234), // supposed to be a time that is already expired + Value: tmp.Temporary(false, time.Unix(1234,0)), // supposed to be a time that is already expired }, { - Value: tmp.Temporary(true, 5678), // supposed to be a time that is already expired + Value: tmp.Temporary(true, time.Unix(5678,0)), // supposed to be a time that is already expired }, }