-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4aca06c
commit ee13ae2
Showing
1 changed file
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
//go:build test | ||
// +build test | ||
|
||
package miniredis | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestStringData_Type(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data StringData | ||
want MiniRedisDataType | ||
}{ | ||
{ | ||
name: "empty string", | ||
data: StringData{data: []byte("")}, | ||
want: Scalar, | ||
}, | ||
{ | ||
name: "non-empty string", | ||
data: StringData{data: []byte("hello")}, | ||
want: Scalar, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.data.Type(); got != tt.want { | ||
t.Errorf("StringData.Type() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIntegerData_Type(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data IntegerData | ||
want MiniRedisDataType | ||
}{ | ||
{ | ||
name: "zero", | ||
data: IntegerData{data: 0}, | ||
want: Scalar, | ||
}, | ||
{ | ||
name: "positive integer", | ||
data: IntegerData{data: 42}, | ||
want: Scalar, | ||
}, | ||
{ | ||
name: "negative integer", | ||
data: IntegerData{data: -42}, | ||
want: Scalar, | ||
}, | ||
{ | ||
name: "max int64", | ||
data: IntegerData{data: 9223372036854775807}, | ||
want: Scalar, | ||
}, | ||
{ | ||
name: "min int64", | ||
data: IntegerData{data: -9223372036854775808}, | ||
want: Scalar, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.data.Type(); got != tt.want { | ||
t.Errorf("IntegerData.Type() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStringData_Serialize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data StringData | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "empty string", | ||
data: StringData{data: []byte("")}, | ||
want: []byte("$0\r\n\r\n"), | ||
}, | ||
{ | ||
name: "simple string", | ||
data: StringData{data: []byte("hello")}, | ||
want: []byte("$5\r\nhello\r\n"), | ||
}, | ||
{ | ||
name: "string with spaces", | ||
data: StringData{data: []byte("hello world")}, | ||
want: []byte("$11\r\nhello world\r\n"), | ||
}, | ||
{ | ||
name: "string with special characters", | ||
data: StringData{data: []byte("hello\nworld")}, | ||
want: []byte("$11\r\nhello\nworld\r\n"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.data.Serialize() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("StringData.Serialize() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !bytes.Equal(got, tt.want) { | ||
t.Errorf("StringData.Serialize() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestIntegerData_Serialize(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
data IntegerData | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "zero", | ||
data: IntegerData{data: 0}, | ||
want: []byte(":0\r\n"), | ||
}, | ||
{ | ||
name: "positive integer", | ||
data: IntegerData{data: 42}, | ||
want: []byte(":42\r\n"), | ||
}, | ||
{ | ||
name: "negative integer", | ||
data: IntegerData{data: -42}, | ||
want: []byte(":-42\r\n"), | ||
}, | ||
{ | ||
name: "max int64", | ||
data: IntegerData{data: 9223372036854775807}, | ||
want: []byte(":9223372036854775807\r\n"), | ||
}, | ||
{ | ||
name: "min int64", | ||
data: IntegerData{data: -9223372036854775808}, | ||
want: []byte(":-9223372036854775808\r\n"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.data.Serialize() | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("IntegerData.Serialize() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !bytes.Equal(got, tt.want) { | ||
t.Errorf("IntegerData.Serialize() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |