Skip to content

Commit a100674

Browse files
committed
check argument type in My Blocks.
1 parent 87cf9da commit a100674

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

src/lib/ruby-to-blocks-converter/index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,14 @@ class RubyToBlocksConverter {
407407
);
408408
}
409409
procedure = {
410-
id: Blockly.utils.genUid()
410+
id: Blockly.utils.genUid(),
411+
name: name,
412+
procCode: [name],
413+
argumentNames: [],
414+
argumentDefaults: [],
415+
argumentIds: [],
416+
argumentVariables: [],
417+
argumentBlocks: []
411418
};
412419
this._context.procedures[name] = procedure;
413420
return procedure;
@@ -859,12 +866,16 @@ class RubyToBlocksConverter {
859866
args.forEach((arg, i) => {
860867
const argumentId = procedure.argumentIds[i];
861868
if (this._isFalseOrBooleanBlock(arg)) {
862-
if (arg !== false) {
863-
this._addInput(block, argumentId, arg, null);
869+
if (procedure.argumentVariables[i].isBoolean ||
870+
this._changeToBooleanArgument(procedure.argumentNames[i])) {
871+
if (arg !== false) {
872+
this._addInput(block, argumentId, arg, null);
873+
}
874+
return;
864875
}
865-
this._changeToBooleanArgument(procedure.argumentNames[i]);
866-
return;
867-
} else if (this._isNumberOrBlock(arg) || this._isStringOrBlock(arg)) {
876+
}
877+
if (!procedure.argumentVariables[i].isBoolean &&
878+
(this._isNumberOrBlock(arg) || this._isStringOrBlock(arg))) {
868879
this._addTextInput(block, argumentId, _.isNumber(arg) ? arg.toString() : arg, '');
869880
return;
870881
}
@@ -1463,20 +1474,16 @@ class RubyToBlocksConverter {
14631474
topLevel: true
14641475
});
14651476
const procedure = this._createProcedure(procedureName);
1466-
procedure.procCode = [procedureName];
14671477

14681478
const customBlock = this._createBlock('procedures_prototype', 'statement', {
14691479
shadow: true
14701480
});
14711481
this._addInput(block, 'custom_block', customBlock);
14721482

1473-
procedure.argumentNames = this._process(node.children[2]);
1474-
procedure.argumentDefaults = [];
1475-
procedure.argumentIds = [];
1476-
procedure.argumentVariables = [];
1477-
procedure.argumentBlocks = [];
14781483
this._context.localVariables = {};
1479-
procedure.argumentNames.forEach(n => {
1484+
this._process(node.children[2]).forEach(n => {
1485+
n = n.toString();
1486+
procedure.argumentNames.push(n);
14801487
procedure.argumentVariables.push(this._findOrCreateVariable(n));
14811488
procedure.procCode.push('%s');
14821489
procedure.argumentDefaults.push('');

test/unit/lib/ruby-to-blocks-converter/my-blocks.test.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,32 @@ describe('RubyToBlocksConverter/My Blocks', () => {
1414
target = null;
1515
});
1616

17+
test('procedures_definition,procedures_prototype no arguments', () => {
18+
const code = `
19+
def self.made_block
20+
end
21+
`;
22+
const expected = [
23+
{
24+
opcode: 'procedures_definition',
25+
inputs: [
26+
{
27+
name: 'custom_block',
28+
block: {
29+
opcode: 'procedures_prototype',
30+
mutation: {
31+
proccode: 'made_block',
32+
arguments: []
33+
},
34+
shadow: true
35+
}
36+
}
37+
]
38+
}
39+
];
40+
convertAndExpectToEqualBlocks(converter, target, code, expected);
41+
});
42+
1743
test('procedures_definition,procedures_prototype', () => {
1844
const code = `
1945
def self.made_block(arg1, arg2)
@@ -577,17 +603,33 @@ describe('RubyToBlocksConverter/My Blocks', () => {
577603
convertAndExpectToEqualBlocks(converter, target, code, expected);
578604
});
579605

580-
test.only('error if argument type miss match', () => {
581-
const code = `
582-
def self.made_block(arg1)
583-
if arg1
584-
end
585-
end
606+
describe('error if argument type miss match', () => {
607+
test('defined string_number, call boolean', () => {
608+
const code = `
609+
def self.made_block(arg1, arg2)
610+
if arg2
611+
end
612+
end
586613
587-
made_block(12)
588-
`;
589-
const res = converter.targetCodeToBlocks(target, code);
590-
expect(converter.errors).toHaveLength(1);
591-
expect(res).toBeFalsy();
614+
made_block(12, 34)
615+
`;
616+
const res = converter.targetCodeToBlocks(target, code);
617+
expect(converter.errors).toHaveLength(1);
618+
expect(converter.errors[0].text).toMatch(/invalid type of My Block "made_block" argument #2/);
619+
expect(res).toBeFalsy();
620+
});
621+
622+
test('defined boolean, call string_number', () => {
623+
const code = `
624+
def self.made_block(arg1, arg2)
625+
end
626+
627+
made_block(false, 1)
628+
`;
629+
const res = converter.targetCodeToBlocks(target, code);
630+
expect(converter.errors).toHaveLength(1);
631+
expect(converter.errors[0].text).toMatch(/invalid type of My Block "made_block" argument #1/);
632+
expect(res).toBeFalsy();
633+
});
592634
});
593635
});

0 commit comments

Comments
 (0)