diff --git a/packages/server/utils/pkg/datasource/local/local_test.go b/packages/server/utils/pkg/datasource/local/local_test.go index e8192b8..830401c 100644 --- a/packages/server/utils/pkg/datasource/local/local_test.go +++ b/packages/server/utils/pkg/datasource/local/local_test.go @@ -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" diff --git a/packages/server/utils/pkg/map/map_test.go b/packages/server/utils/pkg/map/map_test.go index 1644c2c..8fb7494 100644 --- a/packages/server/utils/pkg/map/map_test.go +++ b/packages/server/utils/pkg/map/map_test.go @@ -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) - }) }