query_set_service_load_calculate_set_test.go 6.5 KB
package domainService

import (
	"fmt"
	"github.com/stretchr/testify/assert"
	"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
	"strings"
	"testing"
)

func TestDataLayout(t *testing.T) {
	inputs := []struct {
		cells []*domain.LayoutCell
		flag  Location
		title string
	}{
		{
			title: "配置组多组混合",
			cells: []*domain.LayoutCell{
				// 分组一
				{
					X:         0,
					Y:         0,
					Length:    2,
					ImageData: "2",
					Direction: domain.DirectionRight,
				},
				{
					X:         0,
					Y:         1,
					Length:    3,
					ImageData: "3",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         0,
					Length:    2,
					ImageData: "a",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         1,
					Length:    2,
					ImageData: "f",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         2,
					Length:    2,
					ImageData: "b",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         3,
					Length:    2,
					ImageData: "e",
					Direction: domain.DirectionDown,
				},
				{
					X:         2,
					Y:         0,
					Length:    1,
					ImageData: "c",
					Direction: domain.DirectionNone,
				},
				{
					X:         2,
					Y:         1,
					Length:    1,
					ImageData: "d",
					Direction: domain.DirectionNone,
				},

				// 分组二 右平移10
				{
					X:         0,
					Y:         10,
					Length:    2,
					ImageData: "2",
					Direction: domain.DirectionRight,
				},
				{
					X:         0,
					Y:         11,
					Length:    3,
					ImageData: "3",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         10,
					Length:    2,
					ImageData: "a",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         11,
					Length:    2,
					ImageData: "f",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         12,
					Length:    2,
					ImageData: "b",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         13,
					Length:    2,
					ImageData: "e",
					Direction: domain.DirectionDown,
				},
				{
					X:         2,
					Y:         10,
					Length:    1,
					ImageData: "c",
					Direction: domain.DirectionNone,
				},
				{
					X:         2,
					Y:         11,
					Length:    1,
					ImageData: "d",
					Direction: domain.DirectionNone,
				},
			},
			flag: Location{X: 3, Y: 2},
		},
		{
			title: "正常多字段横排",
			cells: []*domain.LayoutCell{
				// 分组一
				{
					X:         0,
					Y:         0,
					Length:    5,
					ImageData: "a",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         0,
					Length:    5,
					ImageData: "b",
					Direction: domain.DirectionRight,
				},
				{
					X:         2,
					Y:         0,
					Length:    5,
					ImageData: "c",
					Direction: domain.DirectionRight,
				},
				{
					X:         3,
					Y:         0,
					Length:    5,
					ImageData: "d",
					Direction: domain.DirectionRight,
				},

				// 分组二 平移10
				{
					X:         0,
					Y:         10,
					Length:    5,
					ImageData: "a",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         10,
					Length:    5,
					ImageData: "b",
					Direction: domain.DirectionRight,
				},
				{
					X:         2,
					Y:         10,
					Length:    5,
					ImageData: "c",
					Direction: domain.DirectionRight,
				},
				{
					X:         3,
					Y:         10,
					Length:    5,
					ImageData: "d",
					Direction: domain.DirectionRight,
				},
			},
			flag: Location{X: 3, Y: 4},
		},

		{
			title: "正常多字段横排+计算项目",
			cells: []*domain.LayoutCell{
				// 分组一
				{
					X:         0,
					Y:         0,
					Length:    5,
					ImageData: "a",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         0,
					Length:    5,
					ImageData: "b",
					Direction: domain.DirectionRight,
				},
				{
					X:         2,
					Y:         0,
					Length:    1,
					ImageData: "c",
					Direction: domain.DirectionNone,
				},
				{
					X:         3,
					Y:         0,
					Length:    1,
					ImageData: "d",
					Direction: domain.DirectionNone,
				},

				// 分组二 平移10
				{
					X:         0,
					Y:         10,
					Length:    1,
					ImageData: "a",
					Direction: domain.DirectionRight,
				},
				{
					X:         1,
					Y:         10,
					Length:    5,
					ImageData: "b",
					Direction: domain.DirectionRight,
				},
				{
					X:         2,
					Y:         10,
					Length:    1,
					ImageData: "c",
					Direction: domain.DirectionNone,
				},
				{
					X:         2,
					Y:         11,
					Length:    1,
					ImageData: "e",
					Direction: domain.DirectionNone,
				},
				{
					X:         3,
					Y:         10,
					Length:    1,
					ImageData: "d",
					Direction: domain.DirectionNone,
				},
			},
			flag: Location{X: 2, Y: 1},
		},

		{
			title: "正常多字段横排+竖排",
			cells: []*domain.LayoutCell{
				// 分组一
				{
					X:         0,
					Y:         0,
					Length:    5,
					ImageData: "a",
					Direction: domain.DirectionRight,
				},
				{
					X:         0,
					Y:         1,
					Length:    5,
					ImageData: "b",
					Direction: domain.DirectionDown,
				},
				{
					X:         1,
					Y:         0,
					Length:    5,
					ImageData: "c",
					Direction: domain.DirectionRight,
				},
				{
					X:         2,
					Y:         0,
					Length:    5,
					ImageData: "d",
					Direction: domain.DirectionRight,
				},
			},
			flag: Location{X: 2, Y: 1},
		},
	}
	padding := func(cells []*domain.LayoutCell) {
		for _, cell := range cells {
			for i := 0; i < cell.Length; i++ {
				cell.BlockData = append(cell.BlockData, cell.ImageData)
			}
		}
	}
	for _, input := range inputs {
		padding(input.cells)
		// 根据数据修改位移
		CellsLocationAdjust(input.cells)
		// 数据布局
		res := &domain.DataTable{}
		res, err := DataLayout(res, nil, input.cells)
		if err != nil {
			assert.NoError(t, err)
		}
		printRes(res)
		assert.NotEmptyf(t, res.Data[input.flag.X][input.flag.Y], "单元格有值")
	}
}

func printRes(res *domain.DataTable) {
	strBuilder := strings.Builder{}
	for i := range res.Data {
		values := res.Data[i]
		for i := 0; i < len(values)-1; i++ {
			if values[i] == "" {
				values[i] = " "
			}
		}
		strBuilder.WriteString(strings.Join(values, " | "))
		strBuilder.WriteString("\n")
	}
	fmt.Println(strBuilder.String())
}