forked from watplugin/wat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AwaitTest.cs
129 lines (110 loc) · 4.54 KB
/
AwaitTest.cs
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
using Godot;
using System;
using System.Threading.Tasks;
public class AwaitTest : WAT.Test
{
[Signal] public delegate void MySignal();
public event EventHandler MyEventHandlerEvent;
public event EventHandler MyEventHandlerEventWithArgs;
public event Action MyActionEvent;
public event Action<string, int> MyActionEventWithArgs;
// In Addition to the Tests below, Developers may also await..
// ..any method that is targeted by the the [Start], [Pre]..
// ..[Post], [End] attributes provided they change the..
// ..targeted method's signature to async task.
[Test]
public async Task AwaitUntilTimeout()
{
// Developers may await in tests for a set of amount of..
// ..time by changing the Test Method signature to..
// ..async Task while calling await UntilTimeout.
await UntilTimeout(0.2f);
Assert.AutoPass("Await Until Timeout");
}
[Test]
public async Task AwaitUntilSignal()
{
// Developers may await in tests for a signal by..
// ..changing the Test Method signature to async task..
// ..and awaiting UntilSignal passing in the emitter..
// ..object, the string signal and the time limit.
CallDeferred("emit_signal", nameof(MySignal));
await UntilSignal(this, nameof(MySignal), 0.2f);
Assert.AutoPass("Await Until Signal");
}
[Test]
public async Task AwaitUntilEvent()
{
// Developers may await in tests for an Event with no EventArgs..
// ..by changing the Test Method signature to async task and..
// ..awaiting UntilEvent passing in the Event sender, the event..
// ..handle and a time limit.
// Note: Currently WAT only handles Event handles that take the..
// ..the sender and an optional EventArg object.
CallDeferred(nameof(InvokeEventHandlerEventWithNoArgs));
await UntilEvent(this, nameof(MyEventHandlerEvent), 0.2f);
Assert.AutoPass("Await Until Event");
}
[Test]
public async Task AwaitUntilEventHandlerEventWithArgs()
{
// Developers may await in tests for an Event with EventArgs..
// ..by changing the Test Method signature to async task and..
// ..awaiting UntilEvent passing in the Event sender, the event..
// ..handle and a time limit.
// Note: Currently WAT only handles Event handles that take the..
// ..the sender and an optional EventArg Object.
CallDeferred(nameof(InvokeEventHandlerEventWithEventArgs), "Hello", "Alex");
// Developers may investigate TestEventData object returned by UntilEvent..
// ..which containers the property Sender and the property Arguments which..
// ..stores the EventArg object that may have been sent.
TestEventData data =
await UntilEventHandlerEvent(this, nameof(MyEventHandlerEventWithArgs), 0.2f);
GreetingArgs args = (GreetingArgs)data.Arguments;
Assert.IsEqual(args.Greeting, "Hello");
Assert.IsEqual(args.Name, "Alex");
}
[Test]
public async Task AwaitUntilEventWithArgs()
{
// Developers may await in tests for an Event with any parameters.
CallDeferred(nameof(InvokeActionEventWithArgs), "heyo", 20);
// Developers may investigate the parameters invoked by the event.
// These parameters are returned from UntilEvent.
var args = await UntilEvent(this, nameof(MyActionEventWithArgs), 10, (string text, int number) =>
{
// Developers may also use a callback method to
// directly link themselves up to the event.
Assert.IsEqual(text, "heyo");
Assert.IsEqual(number, 20);
});
Assert.IsEqual(args[0], "heyo");
Assert.IsEqual(args[1], 20);
}
private void InvokeActionEventWithNoArgs()
{
MyActionEvent?.Invoke();
}
private void InvokeActionEventWithArgs(string text, int number)
{
MyActionEventWithArgs?.Invoke(text, number);
}
private void InvokeEventHandlerEventWithNoArgs()
{
MyEventHandlerEvent?.Invoke(this, null);
}
private void InvokeEventHandlerEventWithEventArgs(string greeting, string name)
{
MyEventHandlerEventWithArgs?.Invoke(this, new GreetingArgs(greeting, name));
}
public class GreetingArgs : EventArgs
{
public string Greeting { get; set; }
public string Name { get; set; }
public GreetingArgs(string greeting, string name)
{
Greeting = greeting;
Name = name;
}
}
}