diff --git a/pkg/document/crdt/counter_test.go b/pkg/document/crdt/counter_test.go index de3345dfb..bc05d717a 100644 --- a/pkg/document/crdt/counter_test.go +++ b/pkg/document/crdt/counter_test.go @@ -30,44 +30,55 @@ import ( func TestCounter(t *testing.T) { t.Run("new counter test", func(t *testing.T) { - intCntWithInt32Value := crdt.NewCounter(crdt.IntegerCnt, int32(math.MaxInt32), time.InitialTicket) + intCntWithInt32Value, err := crdt.NewCounter(crdt.IntegerCnt, int32(math.MaxInt32), time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.IntegerCnt, intCntWithInt32Value.ValueType()) - intCntWithInt64Value := crdt.NewCounter(crdt.IntegerCnt, int64(math.MaxInt32+1), time.InitialTicket) + intCntWithInt64Value, err := crdt.NewCounter(crdt.IntegerCnt, int64(math.MaxInt32+1), time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.IntegerCnt, intCntWithInt64Value.ValueType()) - intCntWithIntValue := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket) + intCntWithIntValue, err := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.IntegerCnt, intCntWithIntValue.ValueType()) - intCntWithDoubleValue := crdt.NewCounter(crdt.IntegerCnt, 0.5, time.InitialTicket) + intCntWithDoubleValue, err := crdt.NewCounter(crdt.IntegerCnt, 0.5, time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.IntegerCnt, intCntWithDoubleValue.ValueType()) - intCntWithUnsupportedValue := func() { crdt.NewCounter(crdt.IntegerCnt, "", time.InitialTicket) } - assert.Panics(t, intCntWithUnsupportedValue) + _, err = crdt.NewCounter(crdt.IntegerCnt, "", time.InitialTicket) + assert.EqualError(t, err, "unsupported type") - longCntWithInt32Value := crdt.NewCounter(crdt.LongCnt, int32(math.MaxInt32), time.InitialTicket) + longCntWithInt32Value, err := crdt.NewCounter(crdt.LongCnt, int32(math.MaxInt32), time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.LongCnt, longCntWithInt32Value.ValueType()) - longCntWithInt64Value := crdt.NewCounter(crdt.LongCnt, int64(math.MaxInt32+1), time.InitialTicket) + longCntWithInt64Value, err := crdt.NewCounter(crdt.LongCnt, int64(math.MaxInt32+1), time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.LongCnt, longCntWithInt64Value.ValueType()) - longCntWithIntValue := crdt.NewCounter(crdt.LongCnt, math.MaxInt32+1, time.InitialTicket) + longCntWithIntValue, err := crdt.NewCounter(crdt.LongCnt, math.MaxInt32+1, time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.LongCnt, longCntWithIntValue.ValueType()) - longCntWithDoubleValue := crdt.NewCounter(crdt.LongCnt, 0.5, time.InitialTicket) + longCntWithDoubleValue, err := crdt.NewCounter(crdt.LongCnt, 0.5, time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, crdt.LongCnt, longCntWithDoubleValue.ValueType()) - longCntWithUnsupportedValue := func() { crdt.NewCounter(crdt.LongCnt, "", time.InitialTicket) } - assert.Panics(t, longCntWithUnsupportedValue) + _, err = crdt.NewCounter(crdt.LongCnt, "", time.InitialTicket) + assert.EqualError(t, err, "unsupported type") }) t.Run("increase test", func(t *testing.T) { var x = 5 var y int64 = 10 var z = 3.14 - integer := crdt.NewCounter(crdt.IntegerCnt, x, time.InitialTicket) - long := crdt.NewCounter(crdt.LongCnt, y, time.InitialTicket) - double := crdt.NewCounter(crdt.IntegerCnt, z, time.InitialTicket) + integer, err := crdt.NewCounter(crdt.IntegerCnt, x, time.InitialTicket) + assert.NoError(t, err) + long, err := crdt.NewCounter(crdt.LongCnt, y, time.InitialTicket) + assert.NoError(t, err) + double, err := crdt.NewCounter(crdt.IntegerCnt, z, time.InitialTicket) + assert.NoError(t, err) integerOperand := crdt.NewPrimitive(x, time.InitialTicket) longOperand := crdt.NewPrimitive(y, time.InitialTicket) @@ -90,21 +101,14 @@ func TestCounter(t *testing.T) { assert.Equal(t, double.Marshal(), "21") // error process test - // TODO: it should be modified to error check - // when 'Remove panic from server code (#50)' is completed. - unsupportedTypePanicTest := func() { - r := recover() - assert.NotNil(t, r) - assert.Equal(t, r, "unsupported type") + unsupportedTypeErrorTest := func(v interface{}) { + _, err = crdt.NewCounter(crdt.IntegerCnt, v, time.InitialTicket) + assert.EqualError(t, err, "unsupported type") } - unsupportedTest := func(v interface{}) { - defer unsupportedTypePanicTest() - crdt.NewCounter(crdt.IntegerCnt, v, time.InitialTicket) - } - unsupportedTest("str") - unsupportedTest(true) - unsupportedTest([]byte{2}) - unsupportedTest(gotime.Now()) + unsupportedTypeErrorTest("str") + unsupportedTypeErrorTest(true) + unsupportedTypeErrorTest([]byte{2}) + unsupportedTypeErrorTest(gotime.Now()) assert.Equal(t, integer.Marshal(), "23") assert.Equal(t, long.Marshal(), "28") @@ -112,7 +116,8 @@ func TestCounter(t *testing.T) { }) t.Run("Counter value overflow test", func(t *testing.T) { - integer := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket) + integer, err := crdt.NewCounter(crdt.IntegerCnt, math.MaxInt32, time.InitialTicket) + assert.NoError(t, err) assert.Equal(t, integer.ValueType(), crdt.IntegerCnt) operand := crdt.NewPrimitive(1, time.InitialTicket)