Skip to content

Commit

Permalink
[Feat] Add test unit for converting between map strings and maps
Browse files Browse the repository at this point in the history
  • Loading branch information
CornWorld committed Jan 27, 2024
1 parent 9a6da5b commit 3cad58b
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 39 deletions.
1 change: 1 addition & 0 deletions packages/server/utils/pkg/datasource/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"server/utils/pkg/game"
"server/utils/pkg/instruction"
"server/utils/pkg/map"
_ "server/utils/pkg/map/blockManager/block"
"server/utils/pkg/map/type"
"strconv"
"testing"
Expand Down
164 changes: 125 additions & 39 deletions packages/server/utils/pkg/map/map_test.go
Original file line number Diff line number Diff line change
@@ -1,49 +1,135 @@
package _map

import (
"reflect"
"server/utils/pkg/map/blockManager"
"server/utils/pkg/map/blockManager/block"
_ "server/utils/pkg/map/blockManager/block"
"server/utils/pkg/map/type"
"testing"
)

func TestMapToJsonStr(t *testing.T) {
t.Run("t1", func(t *testing.T) {
str := `
{
"mappings": {
"block":[
"blank",
"castle",
"king",
"mountain",
"soldier"
],
"owner": [
1,
2
]
func TestConvJsonStrMap(t *testing.T) {
type args struct {
jsonStr string
}
tests := []struct {
name string
args args
want *Map
}{
{
name: "basic map string",
args: args{
jsonStr: `{"mappings":{"block":["blank"]},"type":[[0,0,0]]}`,
},
want: &Map{
[][]_type.Block{
{
&block.Blank{},
&block.Blank{},
&block.Blank{},
},
},
mapInfo{
size: MapSize{3, 1},
id: 0,
},
},
},
{
name: "with not expected owner field 1",
args: args{
jsonStr: `{"mappings":{"block":["blank"],"owner":[1,2]},"type":[[0,0,0]]}`,
},
want: &Map{
[][]_type.Block{
{
&block.Blank{},
&block.Blank{},
&block.Blank{},
},
},
mapInfo{
size: MapSize{3, 1},
id: 0,
},
},
},
{
name: "with not expected owner field 2",
args: args{
jsonStr: `{"mappings":{"block":["blank"]},"type":[[0,0,0]],"owner":[[1,1,1]]}`,
},
want: &Map{
[][]_type.Block{
{
&block.Blank{},
&block.Blank{},
&block.Blank{},
},
},
mapInfo{
size: MapSize{3, 1},
id: 0,
},
},
},
{
name: "with owner field",
args: args{
jsonStr: `{"mappings":{"block":["soldier"],"owner":[1]},"type":[[0,0,0]],"owner":[[1,1,1]]}`,
},
want: &Map{
[][]_type.Block{
{
blockManager.NewBlock(1, 0, 1),
blockManager.NewBlock(1, 0, 1),
blockManager.NewBlock(1, 0, 1),
},
},
mapInfo{
size: MapSize{3, 1},
id: 0,
},
},
},
"type": [
[1, 0, 0, 0],
[0, 2, 3, 0],
[0, 3, 2, 0],
[0, 0, 0, 1]
],
"owner": [
[0, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 0]
],
"number": [
[43, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 43]
]
{
name: "with owner field and number field",
args: args{
jsonStr: `{"mappings":{"block":["soldier"],"owner":[1]},"type":[[0,0,0]],"owner":[[1,1,1]],"number":[[1,2,255]]}`,
},
want: &Map{
[][]_type.Block{
{
blockManager.NewBlock(1, 1, 1),
blockManager.NewBlock(1, 2, 1),
blockManager.NewBlock(1, 255, 1),
},
},
mapInfo{
size: MapSize{3, 1},
id: 0,
},
},
},
}
for _, tt := range tests {
t.Run("JsonStrToMap: "+tt.name, func(t *testing.T) {
if got := JsonStrToMap(tt.args.jsonStr); !reflect.DeepEqual(got, tt.want) {
t.Errorf("JsonStrToMap() = %v, want %v", got, tt.want)
}
})
}
for _, tt := range tests {
t.Run("MapToJsonStr: "+tt.name, func(t *testing.T) {
got := MapToJsonStr(tt.want)
var j1, j2 *Map
j1 = JsonStrToMap(got)
j2 = JsonStrToMap(tt.args.jsonStr)
if !reflect.DeepEqual(j1, j2) {
t.Errorf("MapToJsonStr() = %v, want %v", j1, j2)
}
})
}
`
m := JsonStrToMap(str)
got := MapToJsonStr(m)
t.Log(got)
})
}

0 comments on commit 3cad58b

Please sign in to comment.