Skip to content

Commit 2958c58

Browse files
committed
added test for global variable.
1 parent d321faf commit 2958c58

File tree

2 files changed

+151
-134
lines changed

2 files changed

+151
-134
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,11 @@ class RubyToBlocksConverter {
999999
}
10001000

10011001
block = this._createBlock(`motion_change${xy}by`, 'statement');
1002-
this._addInput(block, `D${_.toUpper(xy)}`, this._createNumberBlock('math_number', rh, block.id));
1002+
this._addInput(
1003+
block,
1004+
`D${_.toUpper(xy)}`,
1005+
this._createNumberBlock('math_number', rh, block.id)
1006+
);
10031007
}
10041008
break;
10051009
}
Lines changed: 146 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import RubyToBlocksConverter from '../../../../src/lib/ruby-to-blocks-converter';
22
import {
33
convertAndExpectToEqualBlocks,
4-
expectToEqualRubyStatement,
4+
convertAndExpectToEqualRubyStatement,
55
expectedInfo
66
} from '../../../helpers/expect-to-equal-blocks';
77

@@ -11,162 +11,175 @@ describe('RubyToBlocksConverter/Variables', () => {
1111

1212
beforeEach(() => {
1313
converter = new RubyToBlocksConverter(null);
14-
target = null; // TODO: 正しいtargetを用意して、定義済みの各種変数はそれを参照しているかチェックする。無駄に新しい変数を作らせないこと。
15-
});
16-
17-
test('data_variable', () => {
18-
const code = '@a';
19-
const expected = [
20-
{
21-
opcode: 'data_variable',
22-
fields: [
14+
// TODO: 正しいtargetを用意して、定義済みの各種変数はそれを参照しているかチェックする。
15+
// 無駄に新しい変数を作らせないこと。
16+
target = null;
17+
});
18+
19+
[
20+
{
21+
scope: '@',
22+
name: 'a'
23+
},
24+
{
25+
scope: '$',
26+
name: 'a'
27+
},
28+
].forEach(variable => {
29+
let varName = `${variable.scope}${variable.name}`;
30+
describe(varName, () => {
31+
test('data_variable', () => {
32+
const code = varName;
33+
const expected = [
2334
{
24-
name: 'VARIABLE',
25-
variable: '@a'
35+
opcode: 'data_variable',
36+
fields: [
37+
{
38+
name: 'VARIABLE',
39+
variable: varName
40+
}
41+
]
2642
}
27-
]
28-
}
29-
];
30-
convertAndExpectToEqualBlocks(converter, target, code, expected);
31-
});
43+
];
44+
convertAndExpectToEqualBlocks(converter, target, code, expected);
45+
});
3246

33-
test('data_setvariableto', () => {
34-
const code = '@a = 0';
35-
const expected = [
36-
{
37-
opcode: 'data_setvariableto',
38-
fields: [
47+
test('data_setvariableto', () => {
48+
const code = `${varName} = 0`;
49+
const expected = [
3950
{
40-
name: 'VARIABLE',
41-
variable: '@a'
51+
opcode: 'data_setvariableto',
52+
fields: [
53+
{
54+
name: 'VARIABLE',
55+
variable: varName
56+
}
57+
],
58+
inputs: [
59+
{
60+
name: 'VALUE',
61+
block: expectedInfo.makeText('0')
62+
}
63+
]
4264
}
43-
],
44-
inputs: [
45-
{
46-
name: 'VALUE',
47-
block: expectedInfo.makeText('0')
48-
}
49-
]
50-
}
51-
];
52-
convertAndExpectToEqualBlocks(converter, target, code, expected);
53-
});
65+
];
66+
convertAndExpectToEqualBlocks(converter, target, code, expected);
67+
});
5468

55-
test('data_changevariableby', () => {
56-
const code = '@a += 1';
57-
const expected = [
58-
{
59-
opcode: 'data_changevariableby',
60-
fields: [
69+
test('data_changevariableby', () => {
70+
const code = `${varName} += 1`;
71+
const expected = [
6172
{
62-
name: 'VARIABLE',
63-
variable: '@a'
73+
opcode: 'data_changevariableby',
74+
fields: [
75+
{
76+
name: 'VARIABLE',
77+
variable: varName
78+
}
79+
],
80+
inputs: [
81+
{
82+
name: 'VALUE',
83+
block: expectedInfo.makeText('1')
84+
}
85+
]
6486
}
65-
],
66-
inputs: [
87+
];
88+
convertAndExpectToEqualBlocks(converter, target, code, expected);
89+
90+
[
91+
`${varName} -= 1`,
92+
`${varName} *= 1`,
93+
`${varName} /= 1`,
94+
`${varName} %= 1`
95+
].forEach(s => {
96+
convertAndExpectToEqualRubyStatement(converter, target, s, s);
97+
});
98+
});
99+
100+
test('data_showvariable', () => {
101+
const code = `show_variable("${varName}")`;
102+
const expected = [
67103
{
68-
name: 'VALUE',
69-
block: expectedInfo.makeText('1')
104+
opcode: 'data_showvariable',
105+
fields: [
106+
{
107+
name: 'VARIABLE',
108+
variable: varName
109+
}
110+
]
70111
}
71-
]
72-
}
73-
];
74-
convertAndExpectToEqualBlocks(converter, target, code, expected);
75-
76-
[
77-
'@a -= 1',
78-
'@a *= 1',
79-
'@a /= 1',
80-
'@a %= 1'
81-
].forEach(s => {
82-
expect(converter.targetCodeToBlocks(target, s)).toBeTruthy();
83-
expectToEqualRubyStatement(converter, s);
84-
});
85-
});
86-
87-
test('data_showvariable', () => {
88-
const code = 'show_variable("@a")';
89-
const expected = [
90-
{
91-
opcode: 'data_showvariable',
92-
fields: [
112+
];
113+
convertAndExpectToEqualBlocks(converter, target, code, expected);
114+
115+
[
116+
`show_variable("${variable.name}")`,
117+
'show_variable(1)',
118+
`show_variable("&${variable.name}")`
119+
].forEach(s => {
120+
convertAndExpectToEqualRubyStatement(converter, target, s, s);
121+
});
122+
});
123+
124+
test('data_hidevariable', () => {
125+
const code = `hide_variable("${varName}")`;
126+
const expected = [
93127
{
94-
name: 'VARIABLE',
95-
variable: '@a'
128+
opcode: 'data_hidevariable',
129+
fields: [
130+
{
131+
name: 'VARIABLE',
132+
variable: varName
133+
}
134+
]
96135
}
97-
]
98-
}
99-
];
100-
convertAndExpectToEqualBlocks(converter, target, code, expected);
101-
102-
[
103-
'show_variable("a")',
104-
'show_variable(1)',
105-
'show_variable("&a")'
106-
].forEach(s => {
107-
converter.targetCodeToBlocks(target, s);
108-
expectToEqualRubyStatement(converter, s);
109-
});
110-
});
136+
];
137+
convertAndExpectToEqualBlocks(converter, target, code, expected);
111138

112-
test('data_hidevariable', () => {
113-
const code = 'hide_variable("@a")';
114-
const expected = [
115-
{
116-
opcode: 'data_hidevariable',
117-
fields: [
118-
{
119-
name: 'VARIABLE',
120-
variable: '@a'
121-
}
122-
]
123-
}
124-
];
125-
convertAndExpectToEqualBlocks(converter, target, code, expected);
126-
127-
[
128-
'hide_variable("a")',
129-
'hide_variable(1)',
130-
'hide_variable("&a")'
131-
].forEach(s => {
132-
converter.targetCodeToBlocks(target, s);
133-
expectToEqualRubyStatement(converter, s);
134-
});
135-
});
139+
[
140+
`hide_variable("${variable.name}")`,
141+
'hide_variable(1)',
142+
`hide_variable("&${variable.name}")`
143+
].forEach(s => {
144+
convertAndExpectToEqualRubyStatement(converter, target, s, s);
145+
});
146+
});
136147

137-
test.skip('data_listcontents', () => {
138-
});
148+
test.skip('data_listcontents', () => {
149+
});
139150

140-
test.skip('data_addtolist', () => {
141-
});
151+
test.skip('data_addtolist', () => {
152+
});
142153

143-
test.skip('data_deleteoflist', () => {
144-
});
154+
test.skip('data_deleteoflist', () => {
155+
});
145156

146-
test.skip('data_deletealloflist', () => {
147-
});
157+
test.skip('data_deletealloflist', () => {
158+
});
148159

149-
test.skip('data_insertatlist', () => {
150-
});
160+
test.skip('data_insertatlist', () => {
161+
});
151162

152-
test.skip('data_replaceitemoflist', () => {
153-
});
163+
test.skip('data_replaceitemoflist', () => {
164+
});
154165

155-
test.skip('data_itemoflist', () => {
156-
});
166+
test.skip('data_itemoflist', () => {
167+
});
157168

158-
test.skip('data_itemnumoflist', () => {
159-
});
169+
test.skip('data_itemnumoflist', () => {
170+
});
160171

161-
test.skip('data_lengthoflist', () => {
162-
});
172+
test.skip('data_lengthoflist', () => {
173+
});
163174

164-
test.skip('data_listcontainsitem', () => {
165-
});
175+
test.skip('data_listcontainsitem', () => {
176+
});
166177

167-
test.skip('data_showlist', () => {
168-
});
178+
test.skip('data_showlist', () => {
179+
});
169180

170-
test.skip('data_hidelist', () => {
181+
test.skip('data_hidelist', () => {
182+
});
183+
});
171184
});
172185
});

0 commit comments

Comments
 (0)