Skip to content

Commit

Permalink
Add boolean argument support to ScratchX compatibility layer (#208)
Browse files Browse the repository at this point in the history
Co-authored-by: Muffin <[email protected]>
  • Loading branch information
KyleKart and GarboMuffin authored May 9, 2024
1 parent 6d94ef1 commit ce04eaf
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/extension-support/tw-scratchx-compatibility-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ const isScratchCompatibleValue = v => typeof v === 'string' || typeof v === 'num
const parseScratchXArgument = (argument, defaultValue) => {
const result = {};
const hasDefaultValue = isScratchCompatibleValue(defaultValue);
if (hasDefaultValue) {

// defaultValue is ignored for booleans in Scratch 3
if (hasDefaultValue && argument !== 'b') {
result.defaultValue = defaultValue;
}
// TODO: ScratchX docs don't mention support for boolean arguments?

if (argument === 's') {
result.type = ArgumentType.STRING;
if (!hasDefaultValue) {
Expand All @@ -79,6 +81,8 @@ const parseScratchXArgument = (argument, defaultValue) => {
const split = argument.split(/\.|:/);
const menuName = split[1];
result.menu = menuName;
} else if (argument === 'b') {
result.type = ArgumentType.BOOLEAN;
} else {
throw new Error(`Unknown ScratchX argument type: ${argument}`);
}
Expand Down
19 changes: 14 additions & 5 deletions test/unit/tw_scratchx.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ test('complex extension', async t => {
return 'This value should be ignored.';
};

const touching = sprite => sprite === 'Sprite9';
const touching = (sprite, bool) => sprite === 'Sprite9' && bool === true;

const converted = convert(
'My Extension',
Expand All @@ -87,7 +87,7 @@ test('complex extension', async t => {
['r', 'multiply %n by %n and append %s', 'multiplyAndAppend'],
['R', 'repeat %m.myMenu %n', 'repeat', ''],
['-'],
['b', 'touching %s', 'touching', 'Sprite1']
['b', 'touching %s %b', 'touching', 'Sprite1', 'ignored']
],
menus: {
myMenu: ['abc', 'def', 123, true, false],
Expand Down Expand Up @@ -178,12 +178,15 @@ test('complex extension', async t => {
'---',
{
opcode: 'touching',
text: 'touching [0]',
text: 'touching [0] [1]',
blockType: 'Boolean',
arguments: [
{
type: 'string',
defaultValue: 'Sprite1'
},
{
type: 'Boolean'
}
]
}
Expand Down Expand Up @@ -232,11 +235,17 @@ test('complex extension', async t => {
}), 'scratchxscratchxscratchx');

t.equal(converted.touching({
0: 'Sprite1'
0: 'Sprite1',
1: true
}), false);
t.equal(converted.touching({
0: 'Sprite9'
0: 'Sprite9',
1: true
}), true);
t.equal(converted.touching({
0: 'Sprite9',
1: false
}), false);

t.end();
});
Expand Down

0 comments on commit ce04eaf

Please sign in to comment.