Line data Source code
1 : // Package testutil はテスト用のユーティリティ関数を提供する
2 : package testutil
3 :
4 : import (
5 : "math/rand/v2"
6 : "sync"
7 : "testing"
8 :
9 : gc "github.com/kijimaD/ruins/internal/components"
10 : "github.com/kijimaD/ruins/internal/config"
11 : "github.com/kijimaD/ruins/internal/loader"
12 : "github.com/kijimaD/ruins/internal/raw"
13 : "github.com/kijimaD/ruins/internal/rawschema"
14 : "github.com/kijimaD/ruins/internal/resources"
15 : w "github.com/kijimaD/ruins/internal/world"
16 : "github.com/stretchr/testify/require"
17 : )
18 :
19 : // 共有リソースをキャッシュ(一度だけ読み込む)
20 : var (
21 : rawMasterOnce sync.Once
22 : rawMaster rawschema.Raws
23 : conditionDefs gc.ConditionDefs
24 : skillDefs gc.SkillDefs
25 : errRawMaster error
26 : )
27 :
28 : // initConfig は InitTestWorld の初期化オプションを集約する。
29 : type initConfig struct {
30 : stageKey gc.StageKey
31 : stageLevel gc.Level
32 : ui bool
33 : }
34 :
35 : // Option は InitTestWorld の初期化オプション。
36 : type Option func(*initConfig)
37 :
38 : // WithCurrentStage は初期化時の現ステージキーを指定する。省略時はオーバーワールド。
39 : // ステージ跨ぎのテストで、最初から特定のステージ上で始めたいときに使う。
40 : func WithCurrentStage(key gc.StageKey) Option {
41 2 : return func(c *initConfig) { c.stageKey = key }
42 : }
43 :
44 : // WithStageLevel は現ステージのフィールド寸法を指定する。省略時は 50x50。
45 : // 実ゲームではフィールド寸法はステージ生成時に一度決まるため、テストも生成相当の初期化時に与える。
46 : func WithStageLevel(level gc.Level) Option {
47 2 : return func(c *initConfig) { c.stageLevel = level }
48 : }
49 :
50 : // WithUI はフォントフェイス込みの UIResources を積む。UI を描くテストはこれを付ける。
51 : // フェイスはテストが排他所有する。text/v2 は GoTextFaceSource 内に可変キャッシュを持ち、
52 : // 共有フェイスを並行描画すると競合するが、排他所有ならロック無しで並列に実描画できる。
53 : func WithUI() Option {
54 2 : return func(c *initConfig) { c.ui = true }
55 : }
56 :
57 : // InitTestWorld はテスト用 world の唯一の入り口。選び方はこの2行で尽きる。
58 : //
59 : // - 描かないテスト: InitTestWorld(t)。ECS と RawMaster だけの軽量 world
60 : // - UI を描くテスト: InitTestWorld(t, WithUI())。独立フォントフェイス込み
61 : //
62 : // 付け忘れて描くとフェイスが nil で即座に落ちるので、静かに壊れることはない。
63 : // 例外は states の golden_replay だけで、実プレイ再現のため vrt.InitReplayWorld を使う。
64 : func InitTestWorld(tb testing.TB, opts ...Option) w.World {
65 1 : tb.Helper()
66 1 :
67 1 : cfg := initConfig{stageKey: gc.NewOverworldStage(), stageLevel: gc.Level{TileWidth: 50, TileHeight: 50}}
68 1 : for _, opt := range opts {
69 1 : opt(&cfg)
70 1 : }
71 :
72 : // テスト用configを構築してからWorldを初期化する。シングルトンが config を読むため先に渡す。
73 1 : testCfg := &config.Config{Profile: config.ProfileDevelopment}
74 1 : testCfg.ApplyProfileDefaults()
75 1 : world, err := w.InitWorld(&gc.Components{}, testCfg)
76 1 : require.NoError(tb, err)
77 1 :
78 1 : world.Resources.Config.LogLevel = "ignore"
79 1 : world.Resources.Config.Seed = rand.Uint64()
80 1 : world.Resources.Config.RNG = rand.New(rand.NewPCG(world.Resources.Config.Seed, 0))
81 1 : // テスト world は永続化の副作用を持たない。新規開始や入眠のオートセーブでセーブファイルを書かない
82 1 : world.Resources.Config.DisableAutoSave = true
83 1 : world.Resources.SetScreenDimensions(960, 720)
84 1 :
85 : // RawMaster を共有リソースから一度だけ読み込む。
86 : // require は Once の外で行う。Once 内で FailNow すると Goexit により Once が
87 : // 完了扱いになり、rawMaster が空のまま以後の全呼び出しへ漏れるため。
88 1 : rawMasterOnce.Do(func() {
89 1 : rawMaster, errRawMaster = loader.LoadRaws()
90 1 : if errRawMaster == nil {
91 1 : conditionDefs, errRawMaster = raw.NewConditionDefs(rawMaster)
92 1 : }
93 1 : if errRawMaster == nil {
94 1 : skillDefs, errRawMaster = raw.NewSkillDefs(rawMaster)
95 1 : }
96 : })
97 1 : require.NoError(tb, errRawMaster, "failed to load RawMaster")
98 1 : world.Resources.RawMaster = rawMaster
99 1 : world.Resources.ConditionDefs = conditionDefs
100 1 : world.Resources.SkillDefs = skillDefs
101 1 : world.Resources.WeatherDefs = raw.NewWeatherDefs(rawMaster)
102 1 : world.Resources.TileEnvDefs = raw.NewTileEnvDefs(rawMaster)
103 1 : world.Resources.NeedDefs = raw.NewNeedDefs(rawMaster)
104 1 : materialDefs, err := raw.NewMaterialDefs(rawMaster)
105 1 : require.NoError(tb, err)
106 1 : world.Resources.MaterialDefs = materialDefs
107 1 : world.Resources.CalendarDefs = raw.NewCalendarDefs(rawMaster)
108 1 :
109 : // テスト用スプライトシートを初期化
110 1 : spriteSheets := map[string]gc.SpriteSheet{
111 1 : "field": {
112 1 : Sprites: map[string]gc.Sprite{
113 1 : "void": {Width: 32, Height: 32},
114 1 : "wall_generic": {Width: 32, Height: 32},
115 1 : "floor": {Width: 32, Height: 32},
116 1 : "player": {Width: 32, Height: 32},
117 1 : "player_0": {Width: 32, Height: 32},
118 1 : "player_1": {Width: 32, Height: 32},
119 1 : "warp_next": {Width: 32, Height: 32},
120 1 : "red_ball": {Width: 32, Height: 32},
121 1 : },
122 1 : },
123 1 : }
124 1 : world.Resources.SetSpriteSheets(spriteSheets)
125 1 :
126 : // テスト用の現ステージを用意する。フィールド寸法は現ステージの StageField が持つため、
127 : // 現ステージを cfg.stageKey に確定し、そのキーに束縛した StageField を Level 付きで作る。
128 : // 実ゲームでも寸法はステージ生成時に一度決まるので、ここで与える。既定は overworld・50x50、
129 : // WithCurrentStage/WithStageLevel で上書き。
130 : // オーバーワールド判定は帯データ SeamlessBand の有無で行うので、帯を付けない既定では
131 : // IsOnOverworld は偽のまま。帯を要するテストは EnsureSeamlessBand で付ける。
132 : // query の循環 import を避けるため world.Components を直接使う
133 1 : d := world.Components.Dungeon.Get(world.Resources.SingletonEntity)
134 1 : d.CurrentStage = cfg.stageKey
135 1 : fieldEntity := world.ECS.NewEntity()
136 1 : world.Components.StageBound.Add(fieldEntity, &gc.StageBound{Key: cfg.stageKey})
137 1 : field := gc.NewStageField()
138 1 : field.Level = cfg.stageLevel
139 1 : world.Components.StageField.Add(fieldEntity, field)
140 1 :
141 1 : if cfg.ui {
142 1 : world.Resources.UIResources = borrowUIResources(tb)
143 1 : }
144 :
145 1 : return world
146 : }
147 :
148 : // uiResPool は WithUI が積む UIResources の置き場。フェイスの構築は重く、描画は
149 : // フェイス内部のキャッシュへ書き込むため同時共有はできない。そこで排他所有で貸し出す。
150 : // 並行するテストは必ず別インスタンスを掴んで独立し、順に走るテストは温まった
151 : // キャッシュごと再利用して構築を省く。ポインタを入れるのは Put のボクシングを避けるため
152 : var uiResPool sync.Pool
153 :
154 : // borrowUIResources はプールから UIResources を借り、テスト終了時に返す。
155 : // 空なら独立したフェイス一式を構築する
156 : func borrowUIResources(tb testing.TB) resources.UIResources {
157 1 : tb.Helper()
158 1 : uir, ok := uiResPool.Get().(*resources.UIResources)
159 1 : if !ok {
160 1 : fresh, err := loader.LoadUIResources()
161 1 : require.NoError(tb, err)
162 1 : uir = &fresh
163 1 : }
164 2 : tb.Cleanup(func() { uiResPool.Put(uir) })
165 1 : return *uir
166 : }
|