Skip to content

Commit c362dbf

Browse files
committed
feat: stop self.when of control blocks
1 parent 9b31bcb commit c362dbf

File tree

5 files changed

+268
-1039
lines changed

5 files changed

+268
-1039
lines changed

src/lib/ruby-generator/control.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default function (Generator) {
5151

5252
Generator.control_start_as_clone = function (block) {
5353
block.isStatement = true;
54-
return `${Generator.spriteName()}.when(:start_as_a_clone) do\n`;
54+
return `when_start_as_a_clone do\n`;
5555
};
5656

5757
Generator.control_create_clone_of = function (block) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ const ControlConverter = {
122122
}
123123
this._addSubstack(block, statement);
124124
return block;
125+
},
126+
127+
register: function (converter) {
128+
converter.registerCallMethodWithBlock('self', 'when_start_as_a_clone', 0, 0, params => {
129+
const {rubyBlock} = params;
130+
131+
const block = converter.createBlock('control_start_as_clone', 'hat');
132+
converter.setParent(rubyBlock, block);
133+
return block;
134+
});
125135
}
126136
};
127137

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ class RubyToBlocksConverter {
107107
this.reset();
108108

109109
[
110-
EventConverter
110+
EventConverter,
111+
ControlConverter
111112
].forEach(x => x.register(this));
112113
}
113114

test/integration/ruby-tab/control.test.js

Lines changed: 254 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const seleniumHelper = new SeleniumHelper();
66
const {
77
clickText,
88
clickXpath,
9+
findByXpath,
910
scope,
1011
getDriver,
1112
loadUri,
@@ -14,6 +15,8 @@ const {
1415

1516
const rubyHelper = new RubyHelper(seleniumHelper);
1617
const {
18+
fillInRubyProgram,
19+
currentRubyProgram,
1720
expectInterconvertBetweenCodeAndRuby
1821
} = rubyHelper;
1922

@@ -32,37 +35,285 @@ describe('Ruby Tab: Control category blocks', () => {
3235
await loadUri(urlFor('/'));
3336
await clickXpath('//button[@aria-label="Choose a Sprite"]');
3437
await clickText('Abby', scope.modal);
38+
await findByXpath(
39+
'//div[contains(@class, "sprite-selector-item_is-selected")]' +
40+
'/*/div[contains(@class, "sprite-selector-item_sprite-name_1PXjh") and text()="Abby"]'
41+
);
3542
await clickText('Sprite1', scope.spriteItems);
3643

37-
const code = dedent`
44+
const ruby = dedent`
3845
sleep(1)
46+
sleep(x)
47+
sleep(touching?("_edge_"))
3948
10.times do
49+
move(10)
50+
end
51+
x.times do
52+
move(10)
53+
end
54+
(touching?("_edge_")).times do
55+
move(10)
4056
end
4157
loop do
58+
move(10)
4259
end
4360
4461
if false
4562
end
63+
if touching?("_edge_")
64+
turn_right(180)
65+
end
4666
if false
4767
else
4868
end
69+
if touching?("_edge_")
70+
turn_right(180)
71+
else
72+
move(10)
73+
end
74+
if touching?("_edge_")
75+
else
76+
move(10)
77+
end
78+
if touching?("_edge_")
79+
turn_right(180)
80+
else
81+
end
4982
wait until false
83+
wait until touching?("_edge_")
5084
until false
5185
move(10)
5286
end
87+
until touching?("_edge_")
88+
move(10)
89+
end
5390
stop("all")
5491
5592
stop("this script")
5693
5794
stop("other scripts in sprite")
5895
59-
self.when(:start_as_a_clone) do
96+
when_start_as_a_clone do
6097
end
6198
6299
create_clone("_myself_")
63100
create_clone("Abby")
64101
delete_this_clone
65102
`;
66-
await expectInterconvertBetweenCodeAndRuby(code);
103+
await expectInterconvertBetweenCodeAndRuby(ruby);
104+
});
105+
106+
test('Ruby -> Code -> Ruby (backward compatibility) ', async () => {
107+
await loadUri(urlFor('/'));
108+
109+
const oldRuby = dedent`
110+
self.when(:start_as_a_clone) do
111+
end
112+
`;
113+
114+
const newRuby = dedent`
115+
when_start_as_a_clone do
116+
end
117+
`;
118+
119+
await clickText('Ruby', '*[@role="tab"]');
120+
await fillInRubyProgram(oldRuby);
121+
await clickText('Code', '*[@role="tab"]');
122+
await clickXpath(
123+
'//div[contains(@class, "menu-bar_menu-bar-item") and contains(@class, "menu-bar_hoverable")]' +
124+
'/*/span[text()="Edit"]'
125+
);
126+
await clickText('Generate Ruby from Code');
127+
await clickText('Ruby', '*[@role="tab"]');
128+
expect(await currentRubyProgram()).toEqual(`${newRuby}\n`);
129+
});
130+
131+
test('Ruby -> Code -> Ruby (remove wait) ', async () => {
132+
await loadUri(urlFor('/'));
133+
134+
const beforeRuby = dedent`
135+
10.times { wait; move(10) }
136+
10.times { wait; wait; move(10) }
137+
10.times { move(10); wait }
138+
10.times { move(10); wait; wait }
139+
10.times { wait; move(10); wait }
140+
10.times { wait; wait; move(10); wait; wait }
141+
repeat(10) { move(10); wait }
142+
loop { wait }
143+
loop { wait; wait }
144+
loop { wait; wait; wait }
145+
until touching?("_edge_")
146+
wait; move(10)
147+
end
148+
until touching?("_edge_")
149+
wait; wait; move(10)
150+
end
151+
until touching?("_edge_")
152+
wait; move(10); wait
153+
end
154+
until touching?("_edge_")
155+
move(10); wait
156+
end
157+
until touching?("_edge_")
158+
move(10); wait; wait
159+
end
160+
`;
161+
162+
const afterRuby = dedent`
163+
10.times do
164+
move(10)
165+
end
166+
10.times do
167+
move(10)
168+
end
169+
10.times do
170+
move(10)
171+
end
172+
10.times do
173+
move(10)
174+
end
175+
10.times do
176+
move(10)
177+
end
178+
10.times do
179+
move(10)
180+
end
181+
10.times do
182+
move(10)
183+
end
184+
loop do
185+
end
186+
187+
loop do
188+
end
189+
190+
loop do
191+
end
192+
193+
until touching?("_edge_")
194+
move(10)
195+
end
196+
until touching?("_edge_")
197+
move(10)
198+
end
199+
until touching?("_edge_")
200+
move(10)
201+
end
202+
until touching?("_edge_")
203+
move(10)
204+
end
205+
until touching?("_edge_")
206+
move(10)
207+
end
208+
`;
209+
210+
await clickText('Ruby', '*[@role="tab"]');
211+
await fillInRubyProgram(beforeRuby);
212+
await clickText('Code', '*[@role="tab"]');
213+
await clickXpath(
214+
'//div[contains(@class, "menu-bar_menu-bar-item") and contains(@class, "menu-bar_hoverable")]' +
215+
'/*/span[text()="Edit"]'
216+
);
217+
await clickText('Generate Ruby from Code');
218+
await clickText('Ruby', '*[@role="tab"]');
219+
expect(await currentRubyProgram()).toEqual(`${afterRuby}\n`);
220+
});
221+
222+
test('Ruby -> Code -> Ruby (alias) ', async () => {
223+
await loadUri(urlFor('/'));
224+
225+
const beforeRuby = dedent`
226+
repeat(10) { move(10); wait }
227+
repeat(x) { move(10) }
228+
repeat(touching?("_edge_")) { move(10) }
229+
forever { move(10); wait }
230+
`;
231+
232+
const afterRuby = dedent`
233+
10.times do
234+
move(10)
235+
end
236+
x.times do
237+
move(10)
238+
end
239+
(touching?("_edge_")).times do
240+
move(10)
241+
end
242+
loop do
243+
move(10)
244+
end
245+
`;
246+
247+
await clickText('Ruby', '*[@role="tab"]');
248+
await fillInRubyProgram(beforeRuby);
249+
await clickText('Code', '*[@role="tab"]');
250+
await clickXpath(
251+
'//div[contains(@class, "menu-bar_menu-bar-item") and contains(@class, "menu-bar_hoverable")]' +
252+
'/*/span[text()="Edit"]'
253+
);
254+
await clickText('Generate Ruby from Code');
255+
await clickText('Ruby', '*[@role="tab"]');
256+
expect(await currentRubyProgram()).toEqual(`${afterRuby}\n`);
257+
});
258+
259+
test('Ruby -> Code -> Ruby (etc) ', async () => {
260+
await loadUri(urlFor('/'));
261+
262+
const beforeRuby = dedent`
263+
if (touching?("_edge_"))
264+
turn_right(180)
265+
end
266+
unless touching?("_edge_")
267+
turn_right(180)
268+
else
269+
move(10)
270+
end
271+
unless touching?("_edge_")
272+
else
273+
move(10)
274+
end
275+
unless touching?("_edge_")
276+
turn_right(180)
277+
else
278+
end
279+
unless touching?("_edge_")
280+
else
281+
end
282+
wait until (touching?("_edge_"))
283+
`;
284+
285+
const afterRuby = dedent`
286+
if touching?("_edge_")
287+
turn_right(180)
288+
end
289+
if touching?("_edge_")
290+
move(10)
291+
else
292+
turn_right(180)
293+
end
294+
if touching?("_edge_")
295+
move(10)
296+
else
297+
end
298+
if touching?("_edge_")
299+
else
300+
turn_right(180)
301+
end
302+
if touching?("_edge_")
303+
else
304+
end
305+
wait until touching?("_edge_")
306+
`;
307+
308+
await clickText('Ruby', '*[@role="tab"]');
309+
await fillInRubyProgram(beforeRuby);
310+
await clickText('Code', '*[@role="tab"]');
311+
await clickXpath(
312+
'//div[contains(@class, "menu-bar_menu-bar-item") and contains(@class, "menu-bar_hoverable")]' +
313+
'/*/span[text()="Edit"]'
314+
);
315+
await clickText('Generate Ruby from Code');
316+
await clickText('Ruby', '*[@role="tab"]');
317+
expect(await currentRubyProgram()).toEqual(`${afterRuby}\n`);
67318
});
68319
});

0 commit comments

Comments
 (0)