mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2025-07-05 10:41:03 -04:00
Merge pull request #2161 from LLK/e16n
Supporting VM changes for extensionification
This commit is contained in:
commit
9af2e4c086
10 changed files with 209 additions and 57 deletions
|
@ -1,6 +1,8 @@
|
|||
const test = require('tap').test;
|
||||
const Worker = require('tiny-worker');
|
||||
|
||||
const BlockType = require('../../src/extension-support/block-type');
|
||||
|
||||
const dispatch = require('../../src/dispatch/central-dispatch');
|
||||
const VirtualMachine = require('../../src/virtual-machine');
|
||||
|
||||
|
@ -33,8 +35,9 @@ class TestInternalExtension {
|
|||
};
|
||||
}
|
||||
|
||||
go () {
|
||||
go (args, util, blockInfo) {
|
||||
this.status.goCalled = true;
|
||||
return blockInfo;
|
||||
}
|
||||
|
||||
_buildAMenu () {
|
||||
|
@ -62,9 +65,22 @@ test('internal extension', t => {
|
|||
t.type(func, 'function');
|
||||
|
||||
t.notOk(extension.status.goCalled);
|
||||
func();
|
||||
const goBlockInfo = func();
|
||||
t.ok(extension.status.goCalled);
|
||||
|
||||
// The 'go' block returns its own blockInfo. Make sure it matches the expected info.
|
||||
// Note that the extension parser fills in missing fields so there are more fields here than in `getInfo`.
|
||||
const expectedBlockInfo = {
|
||||
arguments: {},
|
||||
blockAllThreads: false,
|
||||
blockType: BlockType.COMMAND,
|
||||
func: goBlockInfo.func, // Cheat since we don't have a good way to ensure we generate the same function
|
||||
opcode: 'go',
|
||||
terminal: false,
|
||||
text: 'go'
|
||||
};
|
||||
t.deepEqual(goBlockInfo, expectedBlockInfo);
|
||||
|
||||
// There should be 2 menus - one is an array, one is the function to call.
|
||||
t.equal(vm.runtime._blockInfo[0].menus.length, 2);
|
||||
// First menu has 3 items.
|
||||
|
|
|
@ -25,7 +25,7 @@ test('spec', t => {
|
|||
t.type(b.getNextBlock, 'function');
|
||||
t.type(b.getBranch, 'function');
|
||||
t.type(b.getOpcode, 'function');
|
||||
|
||||
t.type(b.mutationToXML, 'function');
|
||||
|
||||
t.end();
|
||||
});
|
||||
|
@ -239,6 +239,25 @@ test('getOpcode', t => {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('mutationToXML', t => {
|
||||
const b = new Blocks(new Runtime());
|
||||
const testStringRaw = '"arbitrary" & \'complicated\' test string';
|
||||
const testStringEscaped = '\\"arbitrary\\" & 'complicated' test string';
|
||||
const mutation = {
|
||||
tagName: 'mutation',
|
||||
children: [],
|
||||
blockInfo: {
|
||||
text: testStringRaw
|
||||
}
|
||||
};
|
||||
const xml = b.mutationToXML(mutation);
|
||||
t.equals(
|
||||
xml,
|
||||
`<mutation blockInfo="{"text":"${testStringEscaped}"}"></mutation>`
|
||||
);
|
||||
t.end();
|
||||
});
|
||||
|
||||
// Block events tests
|
||||
test('create', t => {
|
||||
const b = new Blocks(new Runtime());
|
||||
|
|
26
test/unit/engine_mutation-adapter.js
Normal file
26
test/unit/engine_mutation-adapter.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
const test = require('tap').test;
|
||||
|
||||
const mutationAdapter = require('../../src/engine/mutation-adapter');
|
||||
|
||||
test('spec', t => {
|
||||
t.type(mutationAdapter, 'function');
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('convert DOM to Scratch object', t => {
|
||||
const testStringRaw = '"arbitrary" & \'complicated\' test string';
|
||||
const testStringEscaped = '\\"arbitrary\\" & 'complicated' test string';
|
||||
const xml = `<mutation blockInfo="{"text":"${testStringEscaped}"}"></mutation>`;
|
||||
const expectedMutation = {
|
||||
tagName: 'mutation',
|
||||
children: [],
|
||||
blockInfo: {
|
||||
text: testStringRaw
|
||||
}
|
||||
};
|
||||
|
||||
// TODO: do we want to test passing a DOM node to `mutationAdapter`? Node.js doesn't have built-in DOM support...
|
||||
const mutationFromString = mutationAdapter(xml);
|
||||
t.deepEqual(mutationFromString, expectedMutation);
|
||||
t.end();
|
||||
});
|
|
@ -11,6 +11,9 @@ const ScratchBlocksConstants = require('../../src/engine/scratch-blocks-constant
|
|||
const testExtensionInfo = {
|
||||
id: 'test',
|
||||
name: 'fake test extension',
|
||||
color1: '#111111',
|
||||
color2: '#222222',
|
||||
color3: '#333333',
|
||||
blocks: [
|
||||
{
|
||||
func: 'MAKE_A_VARIABLE',
|
||||
|
@ -20,7 +23,8 @@ const testExtensionInfo = {
|
|||
{
|
||||
opcode: 'reporter',
|
||||
blockType: BlockType.REPORTER,
|
||||
text: 'simple text'
|
||||
text: 'simple text',
|
||||
blockIconURI: 'invalid icon URI' // trigger the 'scratch_extension' path
|
||||
},
|
||||
'---', // separator between groups of blocks in an extension
|
||||
{
|
||||
|
@ -63,6 +67,14 @@ const testExtensionInfo = {
|
|||
]
|
||||
};
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
const testButton = function (t, button) {
|
||||
t.same(button.json, null); // should be null or undefined
|
||||
t.equal(button.xml, '<button text="this is a button" callbackKey="MAKE_A_VARIABLE"></button>');
|
||||
|
@ -70,13 +82,28 @@ const testButton = function (t, button) {
|
|||
|
||||
const testReporter = function (t, reporter) {
|
||||
t.equal(reporter.json.type, 'test_reporter');
|
||||
testCategoryInfo(t, reporter);
|
||||
t.equal(reporter.json.checkboxInFlyout, true);
|
||||
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'));
|
||||
t.equal(reporter.json.message0, 'simple text');
|
||||
t.same(reporter.json.extensions, ['scratch_extension']);
|
||||
t.equal(reporter.json.message0, '%1 %2simple text'); // "%1 %2" from the block icon
|
||||
t.notOk(reporter.json.hasOwnProperty('message1'));
|
||||
t.notOk(reporter.json.hasOwnProperty('args0'));
|
||||
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'
|
||||
}
|
||||
]);
|
||||
t.notOk(reporter.json.hasOwnProperty('args1'));
|
||||
t.equal(reporter.xml, '<block type="test_reporter"></block>');
|
||||
};
|
||||
|
@ -88,9 +115,11 @@ const testSeparator = function (t, separator) {
|
|||
|
||||
const testCommand = function (t, command) {
|
||||
t.equal(command.json.type, 'test_command');
|
||||
testCategoryInfo(t, command);
|
||||
t.equal(command.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE);
|
||||
t.assert(command.json.hasOwnProperty('previousStatement'));
|
||||
t.assert(command.json.hasOwnProperty('nextStatement'));
|
||||
t.notOk(command.json.extensions && command.json.extensions.length); // OK if it's absent or empty
|
||||
t.equal(command.json.message0, 'text with %1');
|
||||
t.notOk(command.json.hasOwnProperty('message1'));
|
||||
t.strictSame(command.json.args0[0], {
|
||||
|
@ -105,9 +134,11 @@ const testCommand = function (t, command) {
|
|||
|
||||
const testConditional = function (t, conditional) {
|
||||
t.equal(conditional.json.type, 'test_ifElse');
|
||||
testCategoryInfo(t, conditional);
|
||||
t.equal(conditional.json.outputShape, ScratchBlocksConstants.OUTPUT_SHAPE_SQUARE);
|
||||
t.ok(conditional.json.hasOwnProperty('previousStatement'));
|
||||
t.ok(conditional.json.hasOwnProperty('nextStatement'));
|
||||
t.notOk(conditional.json.extensions && conditional.json.extensions.length); // OK if it's absent or empty
|
||||
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');
|
||||
|
@ -133,9 +164,11 @@ const testConditional = function (t, conditional) {
|
|||
|
||||
const testLoop = function (t, loop) {
|
||||
t.equal(loop.json.type, 'test_loop');
|
||||
testCategoryInfo(t, loop);
|
||||
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
|
||||
t.notOk(loop.json.extensions && loop.json.extensions.length); // OK if it's absent or empty
|
||||
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
|
||||
|
@ -160,7 +193,8 @@ const testLoop = function (t, loop) {
|
|||
test('registerExtensionPrimitives', t => {
|
||||
const runtime = new Runtime();
|
||||
|
||||
runtime.on(Runtime.EXTENSION_ADDED, blocksInfo => {
|
||||
runtime.on(Runtime.EXTENSION_ADDED, categoryInfo => {
|
||||
const blocksInfo = categoryInfo.blocks;
|
||||
t.equal(blocksInfo.length, testExtensionInfo.blocks.length);
|
||||
|
||||
blocksInfo.forEach(blockInfo => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue