Skip to content

Commit

Permalink
Merge pull request #70 from rfronczyk/DPR-429-handle-list-and-variabl…
Browse files Browse the repository at this point in the history
…e-blocks

DPR-429: Fix handling of primitive blocks: list and variable.
  • Loading branch information
colbygk authored Oct 10, 2024
2 parents 7570cb7 + 99b29f8 commit c4a89d9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,22 @@ const blocks = function (targets) {
for (let t in targets) {
for (let a in targets[t].blocks) {
const block = targets[t].blocks[a];
let opcode = block.opcode;

// Check for primitive blocks which don't have the opcode field
if (typeof opcode === 'undefined') {
switch (block[0]) {
case (12):
opcode = 'data_variable';
break;
case (13):
opcode = 'data_listcontents';
break;
}
}

// Get opcode and check variable manipulation for the presence of
// cloud variables
let opcode = block.opcode;
if (opcode === 'data_setvariableto' || opcode === 'data_changevariableby') {
if (isArgCloudVar(block.fields.VARIABLE[1])) {
opcode += '_cloud';
Expand Down
38 changes: 38 additions & 0 deletions test/unit/sb3.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const badExtensions = fs.readFileSync(
path.resolve(__dirname, '../fixtures/sb3/badExtensions.json')
);

const primitiveVariableAndListBlocks = fs.readFileSync(
path.resolve(__dirname, '../fixtures/sb3/primitiveVariableAndListBlocks.json')
);

test('default (object)', t => {
analysis(defaultObject, (err, result) => {
t.ok(typeof err === 'undefined' || err === null);
Expand Down Expand Up @@ -355,3 +359,37 @@ test('regression test IBE-198, a bad list does not break library', t => {
t.end();
});
});

test('correctly handling primitve reporter blocks: list and variable', t => {
analysis(primitiveVariableAndListBlocks, (err, result) => {
t.ok(typeof err === 'undefined' || err === null);
t.type(result, 'object');

t.type(result.variables, 'object');
t.equal(result.variables.count, 1);
t.same(result.variables.id, [
'my_variable'
]);

t.type(result.lists, 'object');
t.equal(result.lists.count, 1);
t.same(result.lists.id, [
'my_list'
]);

t.type(result.blocks, 'object');
t.equal(result.blocks.count, 3);
t.equal(result.blocks.unique, 3);
t.same(result.blocks.id, [
'data_listcontents',
'motion_changexby',
'data_variable'
]);
t.same(result.blocks.frequency, {
data_listcontents: 1,
motion_changexby: 1,
data_variable: 1
});
t.end();
});
});

0 comments on commit c4a89d9

Please sign in to comment.