2018-04-09 18:34:46 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
|
|
|
|
const ArgumentType = require('../../src/extension-support/argument-type');
|
|
|
|
const BlockType = require('../../src/extension-support/block-type');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
|
|
|
const ScratchBlocksConstants = require('../../src/engine/scratch-blocks-constants');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {ExtensionMetadata}
|
|
|
|
*/
|
|
|
|
const testExtensionInfo = {
|
|
|
|
id: 'test',
|
|
|
|
name: 'fake test extension',
|
2019-06-11 18:31:34 -04:00
|
|
|
color1: '#111111',
|
|
|
|
color2: '#222222',
|
|
|
|
color3: '#333333',
|
2018-04-09 18:34:46 -04:00
|
|
|
blocks: [
|
2019-04-01 20:55:07 -04:00
|
|
|
{
|
2019-04-03 01:44:23 -04:00
|
|
|
func: 'MAKE_A_VARIABLE',
|
2019-04-01 20:55:07 -04:00
|
|
|
blockType: BlockType.BUTTON,
|
|
|
|
text: 'this is a button'
|
|
|
|
},
|
2018-04-09 18:34:46 -04:00
|
|
|
{
|
|
|
|
opcode: 'reporter',
|
|
|
|
blockType: BlockType.REPORTER,
|
2019-06-11 18:31:34 -04:00
|
|
|
text: 'simple text',
|
|
|
|
blockIconURI: 'invalid icon URI' // trigger the 'scratch_extension' path
|
2018-04-09 18:34:46 -04:00
|
|
|
},
|
2019-09-21 00:49:49 -04:00
|
|
|
{
|
|
|
|
opcode: 'inlineImage',
|
|
|
|
blockType: BlockType.REPORTER,
|
|
|
|
text: 'text and [IMAGE]',
|
|
|
|
arguments: {
|
|
|
|
IMAGE: {
|
|
|
|
type: ArgumentType.IMAGE,
|
|
|
|
dataURI: 'invalid image URI'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2018-04-09 18:34:46 -04:00
|
|
|
'---', // separator between groups of blocks in an extension
|
|
|
|
{
|
|
|
|
opcode: 'command',
|
|
|
|
blockType: BlockType.COMMAND,
|
2019-09-27 12:39:15 -04:00
|
|
|
text: 'text with [ARG] [ARG_WITH_DEFAULT]',
|
2018-04-09 18:34:46 -04:00
|
|
|
arguments: {
|
|
|
|
ARG: {
|
|
|
|
type: ArgumentType.STRING
|
2019-09-27 12:39:15 -04:00
|
|
|
},
|
|
|
|
ARG_WITH_DEFAULT: {
|
|
|
|
type: ArgumentType.STRING,
|
|
|
|
defaultValue: 'default text'
|
2018-04-09 18:34:46 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'ifElse',
|
|
|
|
blockType: BlockType.CONDITIONAL,
|
|
|
|
branchCount: 2,
|
|
|
|
text: [
|
|
|
|
'test if [THING] is spiffy and if so then',
|
|
|
|
'or elsewise'
|
|
|
|
],
|
|
|
|
arguments: {
|
|
|
|
THING: {
|
|
|
|
type: ArgumentType.BOOLEAN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'loop',
|
|
|
|
blockType: BlockType.LOOP, // implied branchCount of 1 unless otherwise stated
|
|
|
|
isTerminal: true,
|
|
|
|
text: [
|
|
|
|
'loopty [MANY] loops'
|
|
|
|
],
|
|
|
|
arguments: {
|
|
|
|
MANY: {
|
|
|
|
type: ArgumentType.NUMBER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2019-06-11 18:31:34 -04:00
|
|
|
const testCategoryInfo = function (t, block) {
|
|
|
|
t.equal(block.json.category, 'fake test extension');
|
|
|
|
t.equal(block.json.colour, '#111111');
|
|
|
|
t.equal(block.json.colourSecondary, '#222222');
|
|
|
|
t.equal(block.json.colourTertiary, '#333333');
|
|
|
|
t.equal(block.json.inputsInline, true);
|
|
|
|
};
|
|
|
|
|
2019-04-01 20:55:07 -04:00
|
|
|
const testButton = function (t, button) {
|
|
|
|
t.same(button.json, null); // should be null or undefined
|
2019-04-03 01:44:23 -04:00
|
|
|
t.equal(button.xml, '<button text="this is a button" callbackKey="MAKE_A_VARIABLE"></button>');
|
2019-04-01 20:55:07 -04:00
|
|
|
};
|
|
|
|
|
2018-04-09 18:34:46 -04:00
|
|
|
const testReporter = function (t, reporter) {
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(reporter.json.type, 'test_reporter');
|
2019-06-11 18:31:34 -04:00
|
|
|
testCategoryInfo(t, reporter);
|
|
|
|
t.equal(reporter.json.checkboxInFlyout, true);
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(reporter.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_ROUND);
|
|
|
|
t.equal(reporter.json.output, 'String');
|
|
|
|
t.notOk(reporter.json.hasOwnProperty('previousStatement'));
|
|
|
|
t.notOk(reporter.json.hasOwnProperty('nextStatement'));
|
2019-06-11 18:31:34 -04:00
|
|
|
t.same(reporter.json.extensions, ['scratch_extension']);
|
|
|
|
t.equal(reporter.json.message0, '%1 %2simple text'); // "%1 %2" from the block icon
|
2018-04-09 18:34:46 -04:00
|
|
|
t.notOk(reporter.json.hasOwnProperty('message1'));
|
2019-06-11 18:31:34 -04:00
|
|
|
t.same(reporter.json.args0, [
|
|
|
|
// %1 in message0: the block icon
|
|
|
|
{
|
|
|
|
type: 'field_image',
|
|
|
|
src: 'invalid icon URI',
|
|
|
|
width: 40,
|
|
|
|
height: 40
|
|
|
|
},
|
|
|
|
// %2 in message0: separator between icon and text (only added when there's also an icon)
|
|
|
|
{
|
|
|
|
type: 'field_vertical_separator'
|
|
|
|
}
|
|
|
|
]);
|
2018-04-09 18:34:46 -04:00
|
|
|
t.notOk(reporter.json.hasOwnProperty('args1'));
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(reporter.xml, '<block type="test_reporter"></block>');
|
2018-04-09 18:34:46 -04:00
|
|
|
};
|
|
|
|
|
2019-09-21 00:49:49 -04:00
|
|
|
const testInlineImage = function (t, inlineImage) {
|
|
|
|
t.equal(inlineImage.json.type, 'test_inlineImage');
|
|
|
|
testCategoryInfo(t, inlineImage);
|
|
|
|
t.equal(inlineImage.json.checkboxInFlyout, true);
|
|
|
|
t.equal(inlineImage.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_ROUND);
|
|
|
|
t.equal(inlineImage.json.output, 'String');
|
|
|
|
t.notOk(inlineImage.json.hasOwnProperty('previousStatement'));
|
|
|
|
t.notOk(inlineImage.json.hasOwnProperty('nextStatement'));
|
|
|
|
t.notOk(inlineImage.json.extensions && inlineImage.json.extensions.length); // OK if it's absent or empty
|
|
|
|
t.equal(inlineImage.json.message0, 'text and %1'); // block text followed by inline image
|
|
|
|
t.notOk(inlineImage.json.hasOwnProperty('message1'));
|
|
|
|
t.same(inlineImage.json.args0, [
|
|
|
|
// %1 in message0: the block icon
|
|
|
|
{
|
|
|
|
type: 'field_image',
|
|
|
|
src: 'invalid image URI',
|
|
|
|
width: 24,
|
|
|
|
height: 24,
|
|
|
|
flip_rtl: false // False by default
|
|
|
|
}
|
|
|
|
]);
|
|
|
|
t.notOk(inlineImage.json.hasOwnProperty('args1'));
|
|
|
|
t.equal(inlineImage.xml, '<block type="test_inlineImage"></block>');
|
|
|
|
};
|
|
|
|
|
2018-04-09 18:34:46 -04:00
|
|
|
const testSeparator = function (t, separator) {
|
2019-04-01 20:55:07 -04:00
|
|
|
t.same(separator.json, null); // should be null or undefined
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(separator.xml, '<sep gap="36"/>');
|
|
|
|
};
|
|
|
|
|
|
|
|
const testCommand = function (t, command) {
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(command.json.type, 'test_command');
|
2019-06-11 18:31:34 -04:00
|
|
|
testCategoryInfo(t, command);
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(command.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE);
|
|
|
|
t.assert(command.json.hasOwnProperty('previousStatement'));
|
|
|
|
t.assert(command.json.hasOwnProperty('nextStatement'));
|
2019-06-11 18:31:34 -04:00
|
|
|
t.notOk(command.json.extensions && command.json.extensions.length); // OK if it's absent or empty
|
2019-09-27 12:39:15 -04:00
|
|
|
t.equal(command.json.message0, 'text with %1 %2');
|
2018-04-09 18:34:46 -04:00
|
|
|
t.notOk(command.json.hasOwnProperty('message1'));
|
|
|
|
t.strictSame(command.json.args0[0], {
|
|
|
|
type: 'input_value',
|
|
|
|
name: 'ARG'
|
|
|
|
});
|
|
|
|
t.notOk(command.json.hasOwnProperty('args1'));
|
|
|
|
t.equal(command.xml,
|
2019-09-27 12:39:15 -04:00
|
|
|
'<block type="test_command"><value name="ARG"><shadow type="text"></shadow></value>' +
|
|
|
|
'<value name="ARG_WITH_DEFAULT"><shadow type="text"><field name="TEXT">' +
|
|
|
|
'default text</field></shadow></value></block>');
|
2018-04-09 18:34:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const testConditional = function (t, conditional) {
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(conditional.json.type, 'test_ifElse');
|
2019-06-11 18:31:34 -04:00
|
|
|
testCategoryInfo(t, conditional);
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(conditional.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE);
|
|
|
|
t.ok(conditional.json.hasOwnProperty('previousStatement'));
|
|
|
|
t.ok(conditional.json.hasOwnProperty('nextStatement'));
|
2019-06-11 18:31:34 -04:00
|
|
|
t.notOk(conditional.json.extensions && conditional.json.extensions.length); // OK if it's absent or empty
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(conditional.json.message0, 'test if %1 is spiffy and if so then');
|
|
|
|
t.equal(conditional.json.message1, '%1'); // placeholder for substack #1
|
|
|
|
t.equal(conditional.json.message2, 'or elsewise');
|
|
|
|
t.equal(conditional.json.message3, '%1'); // placeholder for substack #2
|
|
|
|
t.notOk(conditional.json.hasOwnProperty('message4'));
|
|
|
|
t.strictSame(conditional.json.args0[0], {
|
|
|
|
type: 'input_value',
|
|
|
|
name: 'THING',
|
|
|
|
check: 'Boolean'
|
|
|
|
});
|
|
|
|
t.strictSame(conditional.json.args1[0], {
|
|
|
|
type: 'input_statement',
|
|
|
|
name: 'SUBSTACK'
|
|
|
|
});
|
|
|
|
t.notOk(conditional.json.hasOwnProperty(conditional.json.args2));
|
|
|
|
t.strictSame(conditional.json.args3[0], {
|
|
|
|
type: 'input_statement',
|
|
|
|
name: 'SUBSTACK2'
|
|
|
|
});
|
|
|
|
t.notOk(conditional.json.hasOwnProperty('args4'));
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(conditional.xml, '<block type="test_ifElse"><value name="THING"></value></block>');
|
2018-04-09 18:34:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const testLoop = function (t, loop) {
|
2018-04-20 10:29:29 -04:00
|
|
|
t.equal(loop.json.type, 'test_loop');
|
2019-06-11 18:31:34 -04:00
|
|
|
testCategoryInfo(t, loop);
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(loop.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE);
|
|
|
|
t.ok(loop.json.hasOwnProperty('previousStatement'));
|
|
|
|
t.notOk(loop.json.hasOwnProperty('nextStatement')); // isTerminal is set on this block
|
2019-06-11 18:31:34 -04:00
|
|
|
t.notOk(loop.json.extensions && loop.json.extensions.length); // OK if it's absent or empty
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(loop.json.message0, 'loopty %1 loops');
|
|
|
|
t.equal(loop.json.message1, '%1'); // placeholder for substack
|
|
|
|
t.equal(loop.json.message2, '%1'); // placeholder for loop arrow
|
|
|
|
t.notOk(loop.json.hasOwnProperty('message3'));
|
|
|
|
t.strictSame(loop.json.args0[0], {
|
|
|
|
type: 'input_value',
|
|
|
|
name: 'MANY'
|
|
|
|
});
|
|
|
|
t.strictSame(loop.json.args1[0], {
|
|
|
|
type: 'input_statement',
|
|
|
|
name: 'SUBSTACK'
|
|
|
|
});
|
|
|
|
t.equal(loop.json.lastDummyAlign2, 'RIGHT'); // move loop arrow to right side
|
|
|
|
t.equal(loop.json.args2[0].type, 'field_image');
|
|
|
|
t.equal(loop.json.args2[0].flip_rtl, true);
|
|
|
|
t.notOk(loop.json.hasOwnProperty('args3'));
|
|
|
|
t.equal(loop.xml,
|
2019-09-27 12:39:15 -04:00
|
|
|
'<block type="test_loop"><value name="MANY"><shadow type="math_number"></shadow></value></block>');
|
2018-04-09 18:34:46 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
test('registerExtensionPrimitives', t => {
|
|
|
|
const runtime = new Runtime();
|
|
|
|
|
2019-04-18 12:45:22 -04:00
|
|
|
runtime.on(Runtime.EXTENSION_ADDED, categoryInfo => {
|
|
|
|
const blocksInfo = categoryInfo.blocks;
|
2018-04-09 18:34:46 -04:00
|
|
|
t.equal(blocksInfo.length, testExtensionInfo.blocks.length);
|
|
|
|
|
2019-04-03 14:02:54 -04:00
|
|
|
blocksInfo.forEach(blockInfo => {
|
|
|
|
// `true` here means "either an object or a non-empty string but definitely not null or undefined"
|
|
|
|
t.true(blockInfo.info, 'Every block and pseudo-block must have a non-empty "info" field');
|
|
|
|
});
|
|
|
|
|
2018-04-09 18:34:46 -04:00
|
|
|
// Note that this also implicitly tests that block order is preserved
|
2019-09-21 00:49:49 -04:00
|
|
|
const [button, reporter, inlineImage, separator, command, conditional, loop] = blocksInfo;
|
2018-04-09 18:34:46 -04:00
|
|
|
|
2019-04-01 20:55:07 -04:00
|
|
|
testButton(t, button);
|
2018-04-09 18:34:46 -04:00
|
|
|
testReporter(t, reporter);
|
2019-09-21 00:49:49 -04:00
|
|
|
testInlineImage(t, inlineImage);
|
2018-04-09 18:34:46 -04:00
|
|
|
testSeparator(t, separator);
|
|
|
|
testCommand(t, command);
|
|
|
|
testConditional(t, conditional);
|
|
|
|
testLoop(t, loop);
|
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
runtime._registerExtensionPrimitives(testExtensionInfo);
|
|
|
|
});
|