Skip to content

Commit a850d13

Browse files
committed
add unit tests for Slot class
1 parent c1bce68 commit a850d13

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import "../../../entry";
2+
3+
import { Signal } from "../../../../src/org/osflash/signals/Signal";
4+
import { Slot } from "../../../../src/org/osflash/signals/Slot";
5+
import { ISlot } from "../../../../src/org/osflash/signals/ISlot";
6+
7+
import { assert } from "chai";
8+
9+
describe("SlotTest", () => {
10+
11+
let signal: Signal;
12+
let slot: ISlot;
13+
let expected: any[];
14+
let received: any[];
15+
16+
beforeEach(() => {
17+
signal = new Signal();
18+
expected = null;
19+
received = null;
20+
});
21+
22+
afterEach(() => {
23+
signal = null;
24+
slot = null;
25+
expected = null;
26+
received = null;
27+
});
28+
29+
it("create_slot_with_null_listener_should_throw_a_error", () => {
30+
assert.throws(() => {
31+
slot = new Slot(null, signal);
32+
}, Error);
33+
});
34+
35+
it("create_slot_with_null_signal_should_throw_a_error", () => {
36+
assert.throws(() => {
37+
slot = new Slot(listener0, null);
38+
}, Error);
39+
});
40+
41+
it("execute0_should_not_call_listener_when_disabled", () => {
42+
slot = new Slot(listener0, signal);
43+
slot.enabled = false;
44+
slot.execute0();
45+
assert.isFalse(slot.enabled);
46+
assert.isNull(received);
47+
});
48+
49+
it("execute0_should_call_listener_without_passing_any_parameter", () => {
50+
expected = [];
51+
slot = new Slot(listener0, signal);
52+
slot.execute0();
53+
assert.deepEqual(received, expected);
54+
});
55+
56+
it("execute0_with_one_parameter_should_call_listener_passing_one_parameter", () => {
57+
expected = [42];
58+
slot = new Slot(listener1, signal);
59+
slot.params = expected;
60+
slot.execute0();
61+
assert.deepEqual(received, expected);
62+
});
63+
64+
it("execute0_with_two_parameters_should_call_listener_passing_two_parameters", () => {
65+
expected = ["The answer is", 42];
66+
slot = new Slot(listener2, signal);
67+
slot.params = expected;
68+
slot.execute0();
69+
assert.deepEqual(received, expected);
70+
});
71+
72+
it("execute1_should_not_call_listener_when_disabled", () => {
73+
slot = new Slot(listener1, signal);
74+
slot.enabled = false;
75+
slot.execute1(42);
76+
assert.isFalse(slot.enabled);
77+
assert.isNull(received);
78+
});
79+
80+
it("execute1_should_call_listener_passing_exactly_one_parameter", () => {
81+
expected = [42];
82+
slot = new Slot(listener1, signal);
83+
slot.execute1(expected[0]);
84+
assert.deepEqual(received, expected);
85+
});
86+
87+
it("execute1_with_one_parameter_should_call_listener_passing_value_and_parameter", () => {
88+
expected = ["The answer is", 42];
89+
slot = new Slot(listener2, signal);
90+
slot.params = [expected[1]];
91+
slot.execute1(expected[0]);
92+
assert.deepEqual(received, expected);
93+
});
94+
95+
it("execute1_with_two_parameters_should_call_listener_passing_value_and_two_parameters", () => {
96+
expected = ["The answer is", 42, "not anymore?"];
97+
slot = new Slot(listener3, signal);
98+
slot.params = [expected[1], expected[2]];
99+
slot.execute1(expected[0]);
100+
assert.deepEqual(received, expected);
101+
});
102+
103+
it("execute_should_not_call_listener_when_disabled", () => {
104+
slot = new Slot(listener0, signal);
105+
slot.enabled = false;
106+
slot.execute(["to be ignored"]);
107+
assert.isFalse(slot.enabled);
108+
assert.isNull(received);
109+
});
110+
111+
it("execute_without_value_should_call_listener_passing_no_value", () => {
112+
expected = [];
113+
slot = new Slot(listener0, signal);
114+
slot.execute(expected);
115+
assert.deepEqual(received, expected);
116+
});
117+
118+
it("execute_with_one_value_should_call_listener_passing_exactly_one_value", () => {
119+
expected = [1];
120+
slot = new Slot(listener1, signal);
121+
slot.execute(expected);
122+
assert.deepEqual(received, expected);
123+
});
124+
125+
it("execute_with_two_values_should_call_listener_passing_exactly_two_values", () => {
126+
expected = [1, 2];
127+
slot = new Slot(listener2, signal);
128+
slot.execute(expected);
129+
assert.deepEqual(received, expected);
130+
});
131+
132+
it("execute_with_three_values_should_call_listener_passing_exactly_three_values", () => {
133+
expected = [1, 2, 3];
134+
slot = new Slot(listener3, signal);
135+
slot.execute(expected);
136+
assert.deepEqual(received, expected);
137+
});
138+
139+
it("execute_with_four_values_should_call_listener_passing_exactly_four_values", () => {
140+
expected = [1, 2, 3, 4];
141+
slot = new Slot(listener4, signal);
142+
slot.execute(expected);
143+
assert.deepEqual(received, expected);
144+
});
145+
146+
it("execute_with_four_values_and_one_parameter_should_call_listener_passing_exactly_five_values", () => {
147+
expected = [1, 2, 3, 4, 5];
148+
slot = new Slot(listener5, signal);
149+
slot.params = expected.slice(4);
150+
slot.execute(expected.slice(0, 4));
151+
assert.deepEqual(received, expected);
152+
});
153+
154+
it("get_listener_should_return_listener", () => {
155+
slot = new Slot(listener0, signal);
156+
assert.equal(slot.listener, listener0);
157+
});
158+
159+
it("set_null_listener_should_throw_a_error", () => {
160+
assert.throws(() => {
161+
slot = new Slot(listener0, signal);
162+
slot.listener = null;
163+
}, Error);
164+
});
165+
166+
it("set_listener_should_change_listener", () => {
167+
slot = new Slot(listener0, signal);
168+
slot.listener = listener1;
169+
assert.equal(slot.listener, listener1);
170+
});
171+
172+
it("toString_should_return_string", () => {
173+
slot = new Slot(listener0, signal);
174+
assert.isString(slot.toString());
175+
console.log(slot.toString());
176+
});
177+
178+
function listener0(): void {
179+
received = [];
180+
}
181+
182+
function listener1(value: any): void {
183+
received = [value];
184+
}
185+
186+
function listener2(value1: any, value2: any): void {
187+
received = [value1, value2];
188+
}
189+
190+
function listener3(value1: any, value2: any, value3: any): void {
191+
received = [value1, value2, value3];
192+
}
193+
194+
function listener4(value1: any, value2: any, value3: any, value4: any): void {
195+
received = [value1, value2, value3, value4];
196+
}
197+
198+
function listener5(value1: any, value2: any, value3: any, value4: any, value5: any): void {
199+
received = [value1, value2, value3, value4, value5];
200+
}
201+
});

0 commit comments

Comments
 (0)