Skip to content

Commit 25ec89f

Browse files
committed
control_start_as_clone
1 parent e58f3e1 commit 25ec89f

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,21 @@ const ControlConverter = {
6262
block = this._createBlock('control_delete_this_clone', 'statement');
6363
}
6464
break;
65+
case 'when':
66+
if (args.length === 1 &&
67+
_.isString(args[0]) && args[0] === 'start_as_a_clone' &&
68+
rubyBlockArgs && rubyBlockArgs.length === 0 &&
69+
rubyBlock) {
70+
block = this._createBlock('control_start_as_clone', 'hat', {
71+
topLevel: true
72+
});
73+
74+
if (this._isBlock(rubyBlock[0])) {
75+
rubyBlock[0].parent = block.id;
76+
block.next = rubyBlock[0].id;
77+
}
78+
}
79+
break;
6580
}
6681
} else if (this._isNumberOrBlock(receiver)) {
6782
switch (name) {

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,6 @@ describe('RubyToBlocksConverter/Control', () => {
907907
}
908908
];
909909
convertAndExpectToEqualBlocks(converter, target, code, expected);
910-
911910
});
912911

913912
test('invalid', () => {
@@ -919,4 +918,63 @@ describe('RubyToBlocksConverter/Control', () => {
919918
});
920919
});
921920
});
921+
922+
describe('control_start_as_clone', () => {
923+
test('normal', () => {
924+
code = `
925+
self.when(:start_as_a_clone) do
926+
end
927+
`;
928+
expected = [
929+
{
930+
opcode: 'control_start_as_clone'
931+
}
932+
];
933+
convertAndExpectToEqualBlocks(converter, target, code, expected);
934+
935+
code = `
936+
self.when(:start_as_a_clone) do
937+
move(10)
938+
end
939+
`;
940+
expected = [
941+
{
942+
opcode: 'control_start_as_clone',
943+
next: rubyToExpected(converter, target, 'move(10)')[0]
944+
}
945+
];
946+
convertAndExpectToEqualBlocks(converter, target, code, expected);
947+
948+
code = `
949+
self.when(:start_as_a_clone) do
950+
move(10)
951+
bounce_if_on_edge
952+
end
953+
`;
954+
expected = [
955+
{
956+
opcode: 'control_start_as_clone',
957+
next: rubyToExpected(converter, target, 'move(10); bounce_if_on_edge')[0]
958+
}
959+
];
960+
convertAndExpectToEqualBlocks(converter, target, code, expected);
961+
});
962+
963+
test('invalid', () => {
964+
[
965+
'self.when(:start_as_a_clone)',
966+
'self.when(:start_as_a_clone, 1)'
967+
].forEach(s => {
968+
convertAndExpectToEqualRubyStatement(converter, target, s, s);
969+
});
970+
971+
[
972+
'12.when(:start_as_a_clone) {}'
973+
].forEach(s => {
974+
expect(converter.targetCodeToBlocks(target, s)).toBeTruthy();
975+
const blockId = Object.keys(converter.blocks).filter(id => converter.blocks[id].topLevel)[0];
976+
expect(converter.blocks[blockId].opcode).toEqual('ruby_statement_with_block');
977+
});
978+
});
979+
});
922980
});

0 commit comments

Comments
 (0)