-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStateMachineSystemTests.cs
More file actions
377 lines (321 loc) · 10.7 KB
/
StateMachineSystemTests.cs
File metadata and controls
377 lines (321 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using System.Reflection;
using GFramework.Core.Abstractions.Enums;
using GFramework.Core.Abstractions.Logging;
using GFramework.Core.Abstractions.State;
using GFramework.Core.Abstractions.Systems;
using GFramework.Core.Architectures;
using GFramework.Core.Command;
using GFramework.Core.Environment;
using GFramework.Core.Events;
using GFramework.Core.Ioc;
using GFramework.Core.Logging;
using GFramework.Core.Query;
using GFramework.Core.State;
namespace GFramework.Core.Tests.State;
/// <summary>
/// ContextAwareStateMachine类的单元测试
/// 测试内容包括:
/// - 作为ISystem的集成测试
/// - Init方法 - 初始化上下文感知状态
/// - Init方法 - 设置Context属性
/// - Destroy方法 - 清理状态
/// - OnArchitecturePhase方法 - 接收架构阶段
/// - 上下文感知状态初始化
/// - 状态变更事件发送
/// - SetContext方法
/// - GetContext方法
/// - ISystem接口实现验证
/// - 与EventBus的集成测试
/// - 多状态注册和切换
/// - 状态机生命周期完整性
/// </summary>
[TestFixture]
public class StateMachineSystemTests
{
[SetUp]
public void SetUp()
{
// 初始化 LoggerFactoryResolver 以支持 MicrosoftDiContainer
LoggerFactoryResolver.Provider = new ConsoleLoggerFactoryProvider();
_eventBus = new EventBus();
var container = new MicrosoftDiContainer();
// 直接初始化 logger 字段
var loggerField = typeof(MicrosoftDiContainer).GetField("_logger",
BindingFlags.NonPublic | BindingFlags.Instance);
loggerField?.SetValue(container,
LoggerFactoryResolver.Provider.CreateLogger(nameof(StateMachineSystemTests)));
container.RegisterPlurality(_eventBus);
container.RegisterPlurality(new CommandExecutor());
container.RegisterPlurality(new QueryExecutor());
container.RegisterPlurality(new DefaultEnvironment());
container.RegisterPlurality(new AsyncQueryExecutor());
_context = new ArchitectureContext(container);
_stateMachine = new TestStateMachineSystemV5();
_stateMachine.SetContext(_context);
}
private TestStateMachineSystemV5? _stateMachine;
private ArchitectureContext? _context;
private EventBus? _eventBus;
/// <summary>
/// 测试ContextAwareStateMachine实现ISystem接口
/// </summary>
[Test]
public void ContextAwareStateMachine_Should_Implement_ISystem_Interface()
{
Assert.That(_stateMachine, Is.InstanceOf<ISystem>());
}
/// <summary>
/// 测试SetContext设置Context属性
/// </summary>
[Test]
public void SetContext_Should_Set_Context_Property()
{
_stateMachine!.SetContext(_context!);
var context = _stateMachine.GetContext();
Assert.That(context, Is.SameAs(_context));
}
/// <summary>
/// 测试GetContext返回Context属性
/// </summary>
[Test]
public void GetContext_Should_Return_Context_Property()
{
_stateMachine!.SetContext(_context!);
var context = _stateMachine.GetContext();
Assert.That(context, Is.Not.Null);
Assert.That(context, Is.SameAs(_context));
}
/// <summary>
/// 测试Init方法为所有ContextAware状态设置Context
/// </summary>
[Test]
public void Init_Should_SetContext_On_All_ContextAware_States()
{
var state1 = new TestContextAwareStateV5();
var state2 = new TestContextAwareStateV5_2();
_stateMachine!.Register(state1);
_stateMachine.Register(state2);
Assert.Throws<InvalidOperationException>(() => state1.GetContext());
Assert.Throws<InvalidOperationException>(() => state2.GetContext());
_stateMachine.Initialize();
Assert.That(state1.GetContext(), Is.SameAs(_context));
Assert.That(state2.GetContext(), Is.SameAs(_context));
}
/// <summary>
/// 测试Init方法不为非ContextAware状态设置Context
/// </summary>
[Test]
public void Init_Should_Not_SetContext_On_NonContextAware_States()
{
var state = new TestStateV5();
_stateMachine!.Register(state);
_stateMachine.Initialize();
}
/// <summary>
/// 测试DestroyAsync方法不抛出异常
/// </summary>
[Test]
public async Task DestroyAsync_Should_Not_Throw_Exception()
{
Assert.That(async () => await _stateMachine!.DestroyAsync(), Throws.Nothing);
}
/// <summary>
/// 测试OnArchitecturePhase方法不抛出异常
/// </summary>
[Test]
public void OnArchitecturePhase_Should_Not_Throw_Exception()
{
Assert.That(() => _stateMachine!.OnArchitecturePhase(ArchitecturePhase.Ready),
Throws.Nothing);
}
/// <summary>
/// 测试异步ChangeToAsync发送StateChangedEvent事件
/// 验证当状态机使用异步方法切换到新状态时,会正确触发StateChangedEvent事件
/// </summary>
[Test]
public async Task ChangeToAsync_Should_Send_StateChangedEvent()
{
var eventReceived = false;
StateChangedEvent? receivedEvent = null;
_eventBus!.Register<StateChangedEvent>(e =>
{
eventReceived = true;
receivedEvent = e;
});
var state1 = new TestStateV5();
var state2 = new TestStateV5();
_stateMachine!.Register(state1);
_stateMachine.Register(state2);
_stateMachine.Initialize();
await _stateMachine.ChangeToAsync<TestStateV5>();
Assert.That(eventReceived, Is.True);
Assert.That(receivedEvent!.OldState, Is.Null);
Assert.That(receivedEvent.NewState, Is.InstanceOf<TestStateV5>());
}
/// <summary>
/// 测试异步ChangeToAsync发送StateChangedEvent事件(包含旧状态)
/// 验证当状态机使用异步方法从一个状态切换到另一个状态时,会正确触发StateChangedEvent事件
/// </summary>
[Test]
public async Task ChangeToAsync_Should_Send_StateChangedEvent_With_OldState()
{
var eventReceived = false;
StateChangedEvent? receivedEvent = null;
_eventBus!.Register<StateChangedEvent>(e =>
{
eventReceived = true;
receivedEvent = e;
});
var state1 = new TestStateV5();
var state2 = new TestStateV5_2();
_stateMachine!.Register(state1);
_stateMachine.Register(state2);
_stateMachine.Initialize();
await _stateMachine.ChangeToAsync<TestStateV5>();
eventReceived = false;
await _stateMachine.ChangeToAsync<TestStateV5_2>();
Assert.That(eventReceived, Is.True);
Assert.That(receivedEvent!.OldState, Is.InstanceOf<TestStateV5>());
Assert.That(receivedEvent.NewState, Is.InstanceOf<TestStateV5_2>());
}
/// <summary>
/// 测试异步切换时多次状态变更都能正确触发事件
/// </summary>
[Test]
public async Task ChangeToAsync_MultipleChanges_Should_Send_Events_Correctly()
{
var eventCount = 0;
_eventBus!.Register<StateChangedEvent>(_ => { eventCount++; });
var state1 = new TestStateV5();
var state2 = new TestStateV5_2();
_stateMachine!.Register(state1);
_stateMachine.Register(state2);
_stateMachine.Initialize();
await _stateMachine.ChangeToAsync<TestStateV5>();
await _stateMachine.ChangeToAsync<TestStateV5_2>();
await _stateMachine.ChangeToAsync<TestStateV5>();
Assert.That(eventCount, Is.EqualTo(3));
}
}
#region Test Classes
/// <summary>
/// 测试用的ContextAwareStateMachine派生类,用于访问内部状态字典
/// </summary>
public class TestStateMachineSystemV5 : StateMachineSystem
{
/// <summary>
/// 获取状态机内部的状态字典
/// </summary>
/// <returns>类型到状态实例的映射字典</returns>
public IDictionary<Type, IState> GetStates()
{
return States;
}
}
/// <summary>
/// 测试用的上下文感知状态基类实现
/// </summary>
public class TestContextAwareStateV5 : ContextAwareStateBase
{
/// <summary>
/// 进入状态时调用
/// </summary>
/// <param name="previous">前一个状态</param>
public override void OnEnter(IState? previous)
{
}
/// <summary>
/// 退出状态时调用
/// </summary>
/// <param name="next">下一个状态</param>
public override void OnExit(IState? next)
{
}
}
/// <summary>
/// 第二个测试用的上下文感知状态基类实现
/// </summary>
public class TestContextAwareStateV5_2 : ContextAwareStateBase
{
/// <summary>
/// 进入状态时调用
/// </summary>
/// <param name="previous">前一个状态</param>
public override void OnEnter(IState? previous)
{
}
/// <summary>
/// 退出状态时调用
/// </summary>
/// <param name="next">下一个状态</param>
public override void OnExit(IState? next)
{
}
}
/// <summary>
/// 测试用的普通状态实现
/// </summary>
public class TestStateV5 : IState
{
/// <summary>
/// 状态标识符
/// </summary>
public int Id { get; set; }
/// <summary>
/// 检查是否可以转换到指定状态
/// </summary>
/// <param name="next">目标状态</param>
/// <returns>始终返回true表示允许转换</returns>
public bool CanTransitionTo(IState next)
{
return true;
}
/// <summary>
/// 进入状态时调用
/// </summary>
/// <param name="previous">前一个状态</param>
public void OnEnter(IState? previous)
{
}
/// <summary>
/// 退出状态时调用
/// </summary>
/// <param name="next">下一个状态</param>
public void OnExit(IState? next)
{
}
}
/// <summary>
/// 第二个测试用的普通状态实现,用于区分不同状态类型
/// </summary>
public class TestStateV5_2 : IState
{
/// <summary>
/// 状态标识符
/// </summary>
public int Id { get; set; }
/// <summary>
/// 检查是否可以转换到指定状态
/// </summary>
/// <param name="next">目标状态</param>
/// <returns>始终返回true表示允许转换</returns>
public bool CanTransitionTo(IState next)
{
return true;
}
/// <summary>
/// 进入状态时调用
/// </summary>
/// <param name="previous">前一个状态</param>
public void OnEnter(IState? previous)
{
}
/// <summary>
/// 退出状态时调用
/// </summary>
/// <param name="next">下一个状态</param>
public void OnExit(IState? next)
{
}
}
#endregion