Skip to content

Commit 825ad7b

Browse files
committed
WIP Filter and pipe tests
1 parent 90f648d commit 825ad7b

File tree

9 files changed

+582
-0
lines changed

9 files changed

+582
-0
lines changed

bin/cjs/test/Filter.spec.js

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const plumbing_1 = require("../plumbing/");
4+
const types_1 = require("../types");
5+
let messagesReceived;
6+
let callBackMethod;
7+
/**
8+
* Test the Pipe class.
9+
*/
10+
describe("Filter Test", () => {
11+
describe("Filter plumbing tests", () => {
12+
test("Should implement IPipeFitting", () => {
13+
// create a pipe, casting to IPipeFitting
14+
const filter = new plumbing_1.Filter({ name: "Test Filter" });
15+
// instance created
16+
expect(filter).toBeInstanceOf(plumbing_1.Filter);
17+
});
18+
test("Should connect an IPipeFitting as its output", () => {
19+
// create two pipes
20+
const pipe = new plumbing_1.Pipe();
21+
// Create a filter
22+
const filter = new plumbing_1.Filter({ name: "Test Filter" });
23+
// Connect filter to pipe
24+
const connected = filter.connect(pipe);
25+
// they should be connected
26+
expect(connected).toBe(true);
27+
});
28+
test("Should be connectable to other IPipeFittings", () => {
29+
// create two pipes
30+
const pipe = new plumbing_1.Pipe();
31+
// Create a filter
32+
const filter = new plumbing_1.Filter({ name: "Test Filter" });
33+
// Connect the filter to pipe 1
34+
const connectedInput = pipe.connect(filter);
35+
// they should be connected
36+
expect(connectedInput).toBe(true);
37+
});
38+
test("Should disconnect its output fitting", () => {
39+
// create a pipe
40+
const pipe = new plumbing_1.Pipe();
41+
// Create a filter
42+
const filter = new plumbing_1.Filter({ name: "Test Filter" });
43+
// Connect the filter output to the pipe
44+
const connected = filter.connect(pipe);
45+
// they should be connected
46+
expect(connected).toBe(true);
47+
// disconnect pipe from the filter
48+
const disconnectedPipe = filter.disconnect();
49+
expect(disconnectedPipe).toBe(pipe);
50+
// no filter output remains connected
51+
const noOutput = filter.disconnect();
52+
expect(noOutput).toBe(undefined);
53+
});
54+
});
55+
describe("Filter message tests", () => {
56+
beforeEach(() => {
57+
messagesReceived = new Array();
58+
callBackMethod = (message) => {
59+
messagesReceived.push(message);
60+
};
61+
});
62+
test("Should apply a filter that modifies a normal message header", () => {
63+
var _a, _b;
64+
// create a message
65+
const message = {
66+
type: types_1.PipeMessageType.NORMAL,
67+
header: { width: 10, height: 2 },
68+
};
69+
const scale = (message, params) => {
70+
var _a, _b;
71+
let width = ((_a = message === null || message === void 0 ? void 0 : message.header) === null || _a === void 0 ? void 0 : _a.width) || 0;
72+
let height = ((_b = message === null || message === void 0 ? void 0 : message.header) === null || _b === void 0 ? void 0 : _b.height) || 0;
73+
const factor = (params === null || params === void 0 ? void 0 : params.factor) || 0;
74+
width = width * factor;
75+
height = height * factor;
76+
message.header = { width, height };
77+
return true;
78+
};
79+
const filter = new plumbing_1.Filter({
80+
name: "Scale",
81+
output: new plumbing_1.PipeListener(callBackMethod),
82+
params: { factor: 10 },
83+
filter: scale
84+
});
85+
const written = filter.write(message);
86+
expect(written).toBe(true);
87+
const received = messagesReceived.shift();
88+
expect(received).toBe(message);
89+
expect((_a = received.header) === null || _a === void 0 ? void 0 : _a.width).toBe(100);
90+
expect((_b = received.header) === null || _b === void 0 ? void 0 : _b.height).toBe(20);
91+
});
92+
test("Should apply a filter that modifies a normal message body", () => {
93+
var _a, _b;
94+
// create a message
95+
const message = {
96+
type: types_1.PipeMessageType.NORMAL,
97+
body: { width: 10, height: 2 },
98+
};
99+
const scale = (message, params) => {
100+
var _a, _b;
101+
let width = ((_a = message === null || message === void 0 ? void 0 : message.body) === null || _a === void 0 ? void 0 : _a.width) || 0;
102+
let height = ((_b = message === null || message === void 0 ? void 0 : message.body) === null || _b === void 0 ? void 0 : _b.height) || 0;
103+
const factor = (params === null || params === void 0 ? void 0 : params.factor) || 0;
104+
width = width * factor;
105+
height = height * factor;
106+
message.body = { width, height };
107+
return true;
108+
};
109+
const filter = new plumbing_1.Filter({
110+
name: "Scale",
111+
output: new plumbing_1.PipeListener(callBackMethod),
112+
params: { factor: 10 },
113+
filter: scale
114+
});
115+
const written = filter.write(message);
116+
expect(written).toBe(true);
117+
const received = messagesReceived.shift();
118+
expect(received).toBe(message);
119+
expect((_a = received.body) === null || _a === void 0 ? void 0 : _a.width).toBe(100);
120+
expect((_b = received.body) === null || _b === void 0 ? void 0 : _b.height).toBe(20);
121+
});
122+
test("Should receive and respect FilterControlMessageType.BYPASS", () => {
123+
var _a, _b;
124+
// create a message
125+
const message = {
126+
type: types_1.PipeMessageType.NORMAL,
127+
body: { width: 10, height: 2 },
128+
};
129+
// Create the filter control function
130+
const scale = (message, params) => {
131+
var _a, _b;
132+
let width = ((_a = message === null || message === void 0 ? void 0 : message.body) === null || _a === void 0 ? void 0 : _a.width) || 0;
133+
let height = ((_b = message === null || message === void 0 ? void 0 : message.body) === null || _b === void 0 ? void 0 : _b.height) || 0;
134+
const factor = (params === null || params === void 0 ? void 0 : params.factor) || 0;
135+
width = width * factor;
136+
height = height * factor;
137+
message.body = { width, height };
138+
return true;
139+
};
140+
// Create the filter
141+
const filter = new plumbing_1.Filter({
142+
name: 'Scale',
143+
output: new plumbing_1.PipeListener(callBackMethod),
144+
params: { factor: 10 },
145+
filter: scale
146+
});
147+
const listener = new plumbing_1.PipeListener(callBackMethod);
148+
filter.connect(listener);
149+
// create bypass control message
150+
let bypassMessage = {
151+
type: types_1.FilterControlMessageType.BYPASS,
152+
name: 'Scale'
153+
};
154+
// Send the bypass control message to the filter
155+
const bypassWritten = filter.write(bypassMessage);
156+
expect(bypassWritten).toBe(true);
157+
// Send the normal message to the filter
158+
const messageWritten = filter.write(message);
159+
expect(messageWritten).toBe(true);
160+
// Message should not be modified
161+
const received = messagesReceived.pop();
162+
expect((_a = received.body) === null || _a === void 0 ? void 0 : _a.width).toBe(10);
163+
expect((_b = received.body) === null || _b === void 0 ? void 0 : _b.height).toBe(2);
164+
});
165+
test("Should exit BYPASS mode when FilterControlMessageType.FILTER is set", () => {
166+
var _a, _b;
167+
// create a message
168+
const message = {
169+
type: types_1.PipeMessageType.NORMAL,
170+
body: { width: 10, height: 2 },
171+
};
172+
// Create the filter control function
173+
const scale = (message, params) => {
174+
var _a, _b;
175+
let width = ((_a = message === null || message === void 0 ? void 0 : message.body) === null || _a === void 0 ? void 0 : _a.width) || 0;
176+
let height = ((_b = message === null || message === void 0 ? void 0 : message.body) === null || _b === void 0 ? void 0 : _b.height) || 0;
177+
const factor = (params === null || params === void 0 ? void 0 : params.factor) || 0;
178+
width = width * factor;
179+
height = height * factor;
180+
message.body = { width, height };
181+
return true;
182+
};
183+
// Create the filter
184+
const filter = new plumbing_1.Filter({
185+
name: 'Scale',
186+
output: new plumbing_1.PipeListener(callBackMethod),
187+
params: { factor: 10 },
188+
filter: scale
189+
});
190+
const listener = new plumbing_1.PipeListener(callBackMethod);
191+
filter.connect(listener);
192+
// create bypass control message
193+
let bypassMessage = {
194+
type: types_1.FilterControlMessageType.BYPASS,
195+
name: 'Scale'
196+
};
197+
// Send the bypass control message to the filter, putting it into bypass mode
198+
const bypassWritten = filter.write(bypassMessage);
199+
expect(bypassWritten).toBe(true);
200+
// create filter control message
201+
let filterMessage = {
202+
type: types_1.FilterControlMessageType.FILTER,
203+
name: 'Scale'
204+
};
205+
// Send the control message to the filter, putting back into filter mode
206+
const filterMessageWritten = filter.write(filterMessage);
207+
expect(filterMessageWritten).toBe(true);
208+
// Send the normal message to the filter
209+
const messageWritten = filter.write(message);
210+
expect(messageWritten).toBe(true);
211+
// Message should be modified
212+
const received = messagesReceived.pop();
213+
expect((_a = received.body) === null || _a === void 0 ? void 0 : _a.width).toBe(100);
214+
expect((_b = received.body) === null || _b === void 0 ? void 0 : _b.height).toBe(20);
215+
});
216+
});
217+
});

bin/cjs/test/Pipe.spec.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
3+
const plumbing_1 = require("../plumbing");
4+
/**
5+
* Test the Pipe class.
6+
*/
7+
describe("Pipe Test", () => {
8+
test("Should implement IPipeFitting and be constructed with no arguments", () => {
9+
// create a pipe, casting to IPipeFitting
10+
const pipe = new plumbing_1.Pipe();
11+
// instance created
12+
expect(pipe).toBeInstanceOf(plumbing_1.Pipe);
13+
});
14+
test("Should connect two pipes", () => {
15+
// create two pipes
16+
const pipe1 = new plumbing_1.Pipe();
17+
const pipe2 = new plumbing_1.Pipe();
18+
// connect pipe 2 to pipe 1
19+
const connected = pipe1.connect(pipe2);
20+
// they should be connected
21+
expect(connected).toBe(true);
22+
});
23+
test("Should disconnect two connected pipes", () => {
24+
// create two pipes
25+
const pipe1 = new plumbing_1.Pipe();
26+
const pipe2 = new plumbing_1.Pipe();
27+
// connect pipe 2 to pipe 1
28+
const connected = pipe1.connect(pipe2);
29+
// they should be connected
30+
expect(connected).toBe(true);
31+
// disconnect pipe 2 from pipe 1
32+
const disconnectedPipe = pipe1.disconnect();
33+
expect(disconnectedPipe).toBe(pipe2);
34+
// nothing remains connected to pipe 1
35+
const noOutput = pipe1.disconnect();
36+
expect(noOutput).toBe(undefined);
37+
});
38+
test("Pipe can be constructed with another pipe as its output", () => {
39+
// create a pipe
40+
const pipe1 = new plumbing_1.Pipe();
41+
// create second pipe with first pipe as it's output
42+
const pipe2 = new plumbing_1.Pipe(pipe1);
43+
// disconnect pipe 1 from pipe 2
44+
const disconnectedPipe = pipe2.disconnect();
45+
expect(disconnectedPipe).toBe(pipe1);
46+
});
47+
test("Should fail to connect a pipe to a pipe with an output already connected", () => {
48+
// create three pipes
49+
const pipe1 = new plumbing_1.Pipe();
50+
const pipe2 = new plumbing_1.Pipe();
51+
const pipe3 = new plumbing_1.Pipe();
52+
// connect pipe 2 to pipe 1
53+
const connected = pipe1.connect(pipe2);
54+
// they should be connected
55+
expect(connected).toBe(true);
56+
// attempt to connect pipe 3 to pipe 1
57+
const extraConnected = pipe1.connect(pipe3);
58+
// new pipe should not be connected
59+
expect(extraConnected).toBe(false);
60+
});
61+
});

bin/cjs/types/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
var desc = Object.getOwnPropertyDescriptor(m, k);
5+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6+
desc = { enumerable: true, get: function() { return m[k]; } };
7+
}
8+
Object.defineProperty(o, k2, desc);
9+
}) : (function(o, m, k, k2) {
10+
if (k2 === undefined) k2 = k;
11+
o[k2] = m[k];
12+
}));
13+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
14+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15+
};
16+
Object.defineProperty(exports, "__esModule", { value: true });
17+
__exportStar(require("./enum"), exports);
18+
__exportStar(require("./pipe"), exports);
19+
__exportStar(require("./message"), exports);
20+
__exportStar(require("../plumbing"), exports);

0 commit comments

Comments
 (0)