Skip to content

Commit bf145f2

Browse files
authored
test suite for js/Blocks/BooleanBlocks.js (#4517)
* Adding test cases for BooleanBlocks.js * Exporting modules for test
1 parent bbc7918 commit bf145f2

File tree

2 files changed

+312
-0
lines changed

2 files changed

+312
-0
lines changed

js/blocks/BooleanBlocks.js

+3
Original file line numberDiff line numberDiff line change
@@ -1050,3 +1050,6 @@ function setupBooleanBlocks(activity) {
10501050
new EqualBlock().setup(activity);
10511051
new StaticBooleanBlock().setup(activity);
10521052
}
1053+
if (typeof module !== "undefined" && module.exports) {
1054+
module.exports = { setupBooleanBlocks };
1055+
}
+309
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
const { setupBooleanBlocks } = require('../BooleanBlocks');
2+
global._ = jest.fn((str) => str);
3+
global.BooleanBlock = jest.fn().mockImplementation((type) => ({
4+
type,
5+
setPalette: jest.fn(),
6+
setHelpString: jest.fn(),
7+
formBlock: jest.fn(),
8+
updateParameter: jest.fn(),
9+
arg: jest.fn(function(logo, turtle, blk, receivedArg) {
10+
const connections = mockActivity.blocks.blockList[blk].connections;
11+
12+
if (this.type === 'not') {
13+
const cblk = connections[1];
14+
if (cblk === null) {
15+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
16+
return false;
17+
}
18+
const a = logo.parseArg(logo, turtle, cblk, blk, receivedArg);
19+
return !a;
20+
}
21+
22+
if (this.type === 'and') {
23+
const cblk1 = connections[1];
24+
const cblk2 = connections[2];
25+
if (cblk1 === null || cblk2 === null) {
26+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
27+
return false;
28+
}
29+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
30+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
31+
return a && b;
32+
}
33+
34+
if (this.type === 'or') {
35+
const cblk1 = connections[1];
36+
const cblk2 = connections[2];
37+
if (cblk1 === null || cblk2 === null) {
38+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
39+
return false;
40+
}
41+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
42+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
43+
return a || b;
44+
}
45+
46+
if (this.type === 'xor') {
47+
const cblk1 = connections[1];
48+
const cblk2 = connections[2];
49+
if (cblk1 === null || cblk2 === null) {
50+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
51+
return false;
52+
}
53+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
54+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
55+
return (a && !b) || (!a && b);
56+
}
57+
58+
if (this.type === 'greater') {
59+
const cblk1 = connections[1];
60+
const cblk2 = connections[2];
61+
if (cblk1 === null || cblk2 === null) {
62+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
63+
return false;
64+
}
65+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
66+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
67+
return Number(a) > Number(b);
68+
}
69+
70+
if (this.type === 'less') {
71+
const cblk1 = connections[1];
72+
const cblk2 = connections[2];
73+
if (cblk1 === null || cblk2 === null) {
74+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
75+
return false;
76+
}
77+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
78+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
79+
return Number(a) < Number(b);
80+
}
81+
82+
if (this.type === 'less_than_or_equal_to') {
83+
const cblk1 = connections[1];
84+
const cblk2 = connections[2];
85+
if (cblk1 === null || cblk2 === null) {
86+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
87+
return false;
88+
}
89+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
90+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
91+
return Number(a) <= Number(b);
92+
}
93+
94+
if (this.type === 'greater_than_or_equal_to') {
95+
const cblk1 = connections[1];
96+
const cblk2 = connections[2];
97+
if (cblk1 === null || cblk2 === null) {
98+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
99+
return false;
100+
}
101+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
102+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
103+
return Number(a) >= Number(b);
104+
}
105+
106+
if (this.type === 'equal') {
107+
const cblk1 = connections[1];
108+
const cblk2 = connections[2];
109+
if (cblk1 === null || cblk2 === null) {
110+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
111+
return false;
112+
}
113+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
114+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
115+
return a === b;
116+
}
117+
118+
if (this.type === 'not_equal_to') {
119+
const cblk1 = connections[1];
120+
const cblk2 = connections[2];
121+
if (cblk1 === null || cblk2 === null) {
122+
mockActivity.errorMsg(global.NOINPUTERRORMSG, blk);
123+
return false;
124+
}
125+
const a = logo.parseArg(logo, turtle, cblk1, blk, receivedArg);
126+
const b = logo.parseArg(logo, turtle, cblk2, blk, receivedArg);
127+
return a !== b;
128+
}
129+
130+
return undefined;
131+
}),
132+
setup: jest.fn(),
133+
beginnerBlock: jest.fn(),
134+
}));
135+
global.NOINPUTERRORMSG = 'No input error message';
136+
137+
const mockActivity = {
138+
blocks: {
139+
blockList: {
140+
'blk1': { value: true, connections: [null, null, null] },
141+
'blk2': { value: false, connections: [null, null, null] },
142+
'blk3': { value: true, connections: [null, 'blk4', 'blk5'] },
143+
'blk4': { value: false, connections: [null, null, null] },
144+
'blk5': { value: true, connections: [null, null, null] },
145+
'blk6': { value: false, connections: [null, 'blk7', 'blk8'] },
146+
'blk7': { value: true, connections: [null, null, null] },
147+
'blk8': { value: false, connections: [null, null, null] },
148+
'blk9': { value: true, connections: [null, 'blk10', 'blk11'] },
149+
'blk10': { value: false, connections: [null, null, null] },
150+
'blk11': { value: true, connections: [null, null, null] },
151+
},
152+
},
153+
errorMsg: jest.fn(),
154+
};
155+
156+
const mockLogo = {
157+
parseArg: jest.fn((logo, turtle, cblk, blk, receivedArg) => {
158+
if (!mockActivity.blocks.blockList[cblk]) {
159+
throw new Error(`Block ${cblk} not found in blockList`);
160+
}
161+
return mockActivity.blocks.blockList[cblk].value;
162+
}),
163+
};
164+
165+
describe('setupBooleanBlocks - Additional Tests', () => {
166+
beforeEach(() => {
167+
jest.clearAllMocks();
168+
});
169+
170+
it('should handle NotBlock arg with valid connections', () => {
171+
setupBooleanBlocks(mockActivity);
172+
const notBlock = new global.BooleanBlock('not');
173+
const result = notBlock.arg(mockLogo, 'turtle1', 'blk3', true);
174+
expect(result).toBe(true);
175+
});
176+
177+
it('should handle AndBlock arg with valid connections', () => {
178+
setupBooleanBlocks(mockActivity);
179+
const andBlock = new global.BooleanBlock('and');
180+
const result = andBlock.arg(mockLogo, 'turtle1', 'blk3', true);
181+
expect(result).toBe(false);
182+
});
183+
184+
it('should handle OrBlock arg with valid connections', () => {
185+
setupBooleanBlocks(mockActivity);
186+
const orBlock = new global.BooleanBlock('or');
187+
const result = orBlock.arg(mockLogo, 'turtle1', 'blk3', true);
188+
expect(result).toBe(true);
189+
});
190+
191+
it('should handle XorBlock arg with valid connections', () => {
192+
setupBooleanBlocks(mockActivity);
193+
const xorBlock = new global.BooleanBlock('xor');
194+
const result = xorBlock.arg(mockLogo, 'turtle1', 'blk3', true);
195+
expect(result).toBe(true);
196+
});
197+
198+
it('should handle GreaterBlock arg with valid connections', () => {
199+
setupBooleanBlocks(mockActivity);
200+
const greaterBlock = new global.BooleanBlock('greater');
201+
const result = greaterBlock.arg(mockLogo, 'turtle1', 'blk6', true);
202+
expect(result).toBe(true);
203+
});
204+
205+
it('should handle LessBlock arg with valid connections', () => {
206+
setupBooleanBlocks(mockActivity);
207+
const lessBlock = new global.BooleanBlock('less');
208+
const result = lessBlock.arg(mockLogo, 'turtle1', 'blk6', true);
209+
expect(result).toBe(false);
210+
});
211+
212+
it('should handle LessThanOrEqualToBlock arg with valid connections', () => {
213+
setupBooleanBlocks(mockActivity);
214+
const lessThanOrEqualToBlock = new global.BooleanBlock('less_than_or_equal_to');
215+
const result = lessThanOrEqualToBlock.arg(mockLogo, 'turtle1', 'blk6', true);
216+
expect(result).toBe(false);
217+
});
218+
219+
it('should handle GreaterThanOrEqualToBlock arg with valid connections', () => {
220+
setupBooleanBlocks(mockActivity);
221+
const greaterThanOrEqualToBlock = new global.BooleanBlock('greater_than_or_equal_to');
222+
const result = greaterThanOrEqualToBlock.arg(mockLogo, 'turtle1', 'blk6', true);
223+
expect(result).toBe(true);
224+
});
225+
226+
it('should handle EqualBlock arg with valid connections', () => {
227+
setupBooleanBlocks(mockActivity);
228+
const equalBlock = new global.BooleanBlock('equal');
229+
const result = equalBlock.arg(mockLogo, 'turtle1', 'blk9', true);
230+
expect(result).toBe(false);
231+
});
232+
233+
it('should handle NotEqualToBlock arg with valid connections', () => {
234+
setupBooleanBlocks(mockActivity);
235+
const notEqualToBlock = new global.BooleanBlock('not_equal_to');
236+
const result = notEqualToBlock.arg(mockLogo, 'turtle1', 'blk9', true);
237+
expect(result).toBe(true);
238+
});
239+
240+
it('should handle NotBlock arg with null connections', () => {
241+
setupBooleanBlocks(mockActivity);
242+
const notBlock = new global.BooleanBlock('not');
243+
notBlock.arg(mockLogo, 'turtle1', 'blk1', true);
244+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk1');
245+
});
246+
247+
it('should handle AndBlock arg with null connections', () => {
248+
setupBooleanBlocks(mockActivity);
249+
const andBlock = new global.BooleanBlock('and');
250+
andBlock.arg(mockLogo, 'turtle1', 'blk2', true);
251+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk2');
252+
});
253+
254+
it('should handle OrBlock arg with null connections', () => {
255+
setupBooleanBlocks(mockActivity);
256+
const orBlock = new global.BooleanBlock('or');
257+
orBlock.arg(mockLogo, 'turtle1', 'blk1', true);
258+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk1');
259+
});
260+
261+
it('should handle XorBlock arg with null connections', () => {
262+
setupBooleanBlocks(mockActivity);
263+
const xorBlock = new global.BooleanBlock('xor');
264+
xorBlock.arg(mockLogo, 'turtle1', 'blk2', true);
265+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk2');
266+
});
267+
268+
it('should handle GreaterBlock arg with null connections', () => {
269+
setupBooleanBlocks(mockActivity);
270+
const greaterBlock = new global.BooleanBlock('greater');
271+
greaterBlock.arg(mockLogo, 'turtle1', 'blk1', true);
272+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk1');
273+
});
274+
275+
it('should handle LessBlock arg with null connections', () => {
276+
setupBooleanBlocks(mockActivity);
277+
const lessBlock = new global.BooleanBlock('less');
278+
lessBlock.arg(mockLogo, 'turtle1', 'blk2', true);
279+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk2');
280+
});
281+
282+
it('should handle LessThanOrEqualToBlock arg with null connections', () => {
283+
setupBooleanBlocks(mockActivity);
284+
const lessThanOrEqualToBlock = new global.BooleanBlock('less_than_or_equal_to');
285+
lessThanOrEqualToBlock.arg(mockLogo, 'turtle1', 'blk1', true);
286+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk1');
287+
});
288+
289+
it('should handle GreaterThanOrEqualToBlock arg with null connections', () => {
290+
setupBooleanBlocks(mockActivity);
291+
const greaterThanOrEqualToBlock = new global.BooleanBlock('greater_than_or_equal_to');
292+
greaterThanOrEqualToBlock.arg(mockLogo, 'turtle1', 'blk2', true);
293+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk2');
294+
});
295+
296+
it('should handle EqualBlock arg with null connections', () => {
297+
setupBooleanBlocks(mockActivity);
298+
const equalBlock = new global.BooleanBlock('equal');
299+
equalBlock.arg(mockLogo, 'turtle1', 'blk1', true);
300+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk1');
301+
});
302+
303+
it('should handle NotEqualToBlock arg with null connections', () => {
304+
setupBooleanBlocks(mockActivity);
305+
const notEqualToBlock = new global.BooleanBlock('not_equal_to');
306+
notEqualToBlock.arg(mockLogo, 'turtle1', 'blk2', true);
307+
expect(mockActivity.errorMsg).toHaveBeenCalledWith(global.NOINPUTERRORMSG, 'blk2');
308+
});
309+
});

0 commit comments

Comments
 (0)