mirror of
https://github.com/scratchfoundation/scratch-analysis.git
synced 2025-05-15 15:20:27 -04:00
Clean up tests
This commit is contained in:
parent
bc9c3e0ab3
commit
b164232b3f
5 changed files with 115 additions and 130 deletions
21
lib/sb2.js
21
lib/sb2.js
|
@ -1,3 +1,5 @@
|
||||||
|
const utility = require('./utility');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of items matching the specified attribute.
|
* Returns an array of items matching the specified attribute.
|
||||||
* @param {object} project Project object (SB2 format)
|
* @param {object} project Project object (SB2 format)
|
||||||
|
@ -81,23 +83,6 @@ const sprites = function (input) {
|
||||||
return {count: result};
|
return {count: result};
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Tallys term frequency from an array of strings.
|
|
||||||
* @param {array} input Array of strings
|
|
||||||
* @return {object} Frequency information
|
|
||||||
*/
|
|
||||||
const frequency = function (input) {
|
|
||||||
const result = Object.create(null);
|
|
||||||
|
|
||||||
for (let i in input) {
|
|
||||||
var term = input[i];
|
|
||||||
if (typeof result[term] === 'undefined') result[term] = 0;
|
|
||||||
result[term]++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts all blocks and generates a frequency count.
|
* Extracts all blocks and generates a frequency count.
|
||||||
* @param {object} project Project object (SB2 format)
|
* @param {object} project Project object (SB2 format)
|
||||||
|
@ -136,7 +121,7 @@ const blocks = function (project) {
|
||||||
walk(flatten(project, 'scripts'));
|
walk(flatten(project, 'scripts'));
|
||||||
|
|
||||||
// Generate frequency count
|
// Generate frequency count
|
||||||
const freq = frequency(result);
|
const freq = utility.frequency(result);
|
||||||
|
|
||||||
// Build result and return
|
// Build result and return
|
||||||
return {
|
return {
|
||||||
|
|
14
lib/sb3.js
14
lib/sb3.js
|
@ -9,7 +9,7 @@ const scripts = function (targets) {
|
||||||
for (let b in targets[t].blocks) {
|
for (let b in targets[t].blocks) {
|
||||||
if (targets[t].blocks[b].topLevel) occurances++;
|
if (targets[t].blocks[b].topLevel) occurances++;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
count: occurances
|
count: occurances
|
||||||
|
@ -27,7 +27,7 @@ const variables = function (targets, attribute) {
|
||||||
occurances++;
|
occurances++;
|
||||||
idList.push(variable[0]);
|
idList.push(variable[0]);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
count: occurances,
|
count: occurances,
|
||||||
|
@ -49,7 +49,7 @@ const extract = function (targets, attribute, id, hash) {
|
||||||
if (typeof id !== 'undefined') idList.push(asset[id]);
|
if (typeof id !== 'undefined') idList.push(asset[id]);
|
||||||
if (typeof hash !== 'undefined') hashList.push(asset[hash]);
|
if (typeof hash !== 'undefined') hashList.push(asset[hash]);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
const result = {count: occurances};
|
const result = {count: occurances};
|
||||||
if (typeof id !== 'undefined') result.id = idList;
|
if (typeof id !== 'undefined') result.id = idList;
|
||||||
|
@ -61,7 +61,7 @@ const sprites = function (targets) {
|
||||||
return {
|
return {
|
||||||
count: targets.length - 1
|
count: targets.length - 1
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
const blocks = function (targets) {
|
const blocks = function (targets) {
|
||||||
// Storage object
|
// Storage object
|
||||||
|
@ -86,10 +86,10 @@ const blocks = function (targets) {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
const extensions = function (extensions) {
|
const extensions = function (list) {
|
||||||
return {
|
return {
|
||||||
count: extensions.length,
|
count: list.length,
|
||||||
id: extensions
|
id: list
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,7 @@ class Utility {
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Utility;
|
module.exports = Utility;
|
||||||
|
|
104
test/unit/sb2.js
104
test/unit/sb2.js
|
@ -189,60 +189,60 @@ test('complex (binary)', t => {
|
||||||
t.equal(result.blocks.count, 34);
|
t.equal(result.blocks.count, 34);
|
||||||
t.equal(result.blocks.unique, 18);
|
t.equal(result.blocks.unique, 18);
|
||||||
t.deepEqual(result.blocks.id, [
|
t.deepEqual(result.blocks.id, [
|
||||||
"whenGreenFlag",
|
'whenGreenFlag',
|
||||||
"doForever",
|
'doForever',
|
||||||
"changeGraphicEffect:by:",
|
'changeGraphicEffect:by:',
|
||||||
"whenGreenFlag",
|
'whenGreenFlag',
|
||||||
"deleteLine:ofList:",
|
'deleteLine:ofList:',
|
||||||
"deleteLine:ofList:",
|
'deleteLine:ofList:',
|
||||||
"doForever",
|
'doForever',
|
||||||
"forward:",
|
'forward:',
|
||||||
"turnRight:",
|
'turnRight:',
|
||||||
"randomFrom:to:",
|
'randomFrom:to:',
|
||||||
"bounceOffEdge",
|
'bounceOffEdge',
|
||||||
"whenGreenFlag",
|
'whenGreenFlag',
|
||||||
"doForever",
|
'doForever',
|
||||||
"setGraphicEffect:to:",
|
'setGraphicEffect:to:',
|
||||||
"xpos",
|
'xpos',
|
||||||
"whenGreenFlag",
|
'whenGreenFlag',
|
||||||
"doForever",
|
'doForever',
|
||||||
"call",
|
'call',
|
||||||
"randomFrom:to:",
|
'randomFrom:to:',
|
||||||
"heading",
|
'heading',
|
||||||
"randomFrom:to:",
|
'randomFrom:to:',
|
||||||
"heading",
|
'heading',
|
||||||
"procDef",
|
'procDef',
|
||||||
"setVar:to:",
|
'setVar:to:',
|
||||||
"getParam",
|
'getParam',
|
||||||
"setVar:to:",
|
'setVar:to:',
|
||||||
"getParam",
|
'getParam',
|
||||||
"append:toList:",
|
'append:toList:',
|
||||||
"getParam",
|
'getParam',
|
||||||
"append:toList:",
|
'append:toList:',
|
||||||
"getParam",
|
'getParam',
|
||||||
"LEGO WeDo 2.0\u001FwhenTilted",
|
'LEGO WeDo 2.0\u001FwhenTilted',
|
||||||
"LEGO WeDo 2.0\u001FsetLED",
|
'LEGO WeDo 2.0\u001FsetLED',
|
||||||
"randomFrom:to:"
|
'randomFrom:to:'
|
||||||
]);
|
]);
|
||||||
t.deepEqual(result.blocks.frequency, {
|
t.deepEqual(result.blocks.frequency, {
|
||||||
"LEGO WeDo 2.0\u001FsetLED": 1,
|
'LEGO WeDo 2.0\u001FsetLED': 1,
|
||||||
"LEGO WeDo 2.0\u001FwhenTilted": 1,
|
'LEGO WeDo 2.0\u001FwhenTilted': 1,
|
||||||
"bounceOffEdge": 1,
|
'bounceOffEdge': 1,
|
||||||
"call": 1,
|
'call': 1,
|
||||||
"changeGraphicEffect:by:": 1,
|
'changeGraphicEffect:by:': 1,
|
||||||
"doForever": 4,
|
'doForever': 4,
|
||||||
"deleteLine:ofList:": 2,
|
'deleteLine:ofList:': 2,
|
||||||
"forward:": 1,
|
'forward:': 1,
|
||||||
"getParam": 4,
|
'getParam': 4,
|
||||||
"heading": 2,
|
'heading': 2,
|
||||||
"procDef": 1,
|
'procDef': 1,
|
||||||
"append:toList:": 2,
|
'append:toList:': 2,
|
||||||
"randomFrom:to:": 4,
|
'randomFrom:to:': 4,
|
||||||
"setGraphicEffect:to:": 1,
|
'setGraphicEffect:to:': 1,
|
||||||
"setVar:to:": 2,
|
'setVar:to:': 2,
|
||||||
"turnRight:": 1,
|
'turnRight:': 1,
|
||||||
"whenGreenFlag": 4,
|
'whenGreenFlag': 4,
|
||||||
"xpos": 1
|
'xpos': 1
|
||||||
});
|
});
|
||||||
|
|
||||||
t.type(result.extensions, 'object');
|
t.type(result.extensions, 'object');
|
||||||
|
|
104
test/unit/sb3.js
104
test/unit/sb3.js
|
@ -193,60 +193,60 @@ test('complex (binary)', t => {
|
||||||
t.equal(result.blocks.count, 34);
|
t.equal(result.blocks.count, 34);
|
||||||
t.equal(result.blocks.unique, 18);
|
t.equal(result.blocks.unique, 18);
|
||||||
t.deepEqual(result.blocks.id, [
|
t.deepEqual(result.blocks.id, [
|
||||||
"event_whenflagclicked",
|
'event_whenflagclicked',
|
||||||
"control_forever",
|
'control_forever',
|
||||||
"looks_changeeffectby",
|
'looks_changeeffectby',
|
||||||
"event_whenflagclicked",
|
'event_whenflagclicked',
|
||||||
"data_deleteoflist",
|
'data_deleteoflist',
|
||||||
"data_deleteoflist",
|
'data_deleteoflist',
|
||||||
"control_forever",
|
'control_forever',
|
||||||
"motion_movesteps",
|
'motion_movesteps',
|
||||||
"motion_turnright",
|
'motion_turnright',
|
||||||
"operator_random",
|
'operator_random',
|
||||||
"motion_ifonedgebounce",
|
'motion_ifonedgebounce',
|
||||||
"event_whenflagclicked",
|
'event_whenflagclicked',
|
||||||
"control_forever",
|
'control_forever',
|
||||||
"looks_seteffectto",
|
'looks_seteffectto',
|
||||||
"motion_xposition",
|
'motion_xposition',
|
||||||
"event_whenflagclicked",
|
'event_whenflagclicked',
|
||||||
"control_forever",
|
'control_forever',
|
||||||
"procedures_call",
|
'procedures_call',
|
||||||
"operator_random",
|
'operator_random',
|
||||||
"motion_direction",
|
'motion_direction',
|
||||||
"operator_random",
|
'operator_random',
|
||||||
"motion_direction",
|
'motion_direction',
|
||||||
"procedures_definition",
|
'procedures_definition',
|
||||||
"data_setvariableto",
|
'data_setvariableto',
|
||||||
"argument_reporter_string_number",
|
'argument_reporter_string_number',
|
||||||
"data_setvariableto",
|
'data_setvariableto',
|
||||||
"argument_reporter_string_number",
|
'argument_reporter_string_number',
|
||||||
"data_addtolist",
|
'data_addtolist',
|
||||||
"argument_reporter_string_number",
|
'argument_reporter_string_number',
|
||||||
"data_addtolist",
|
'data_addtolist',
|
||||||
"argument_reporter_string_number",
|
'argument_reporter_string_number',
|
||||||
"wedo2_whenTilted",
|
'wedo2_whenTilted',
|
||||||
"wedo2_setLightHue",
|
'wedo2_setLightHue',
|
||||||
"operator_random"
|
'operator_random'
|
||||||
]);
|
]);
|
||||||
t.deepEqual(result.blocks.frequency, {
|
t.deepEqual(result.blocks.frequency, {
|
||||||
"argument_reporter_string_number": 4,
|
argument_reporter_string_number: 4,
|
||||||
"control_forever": 4,
|
control_forever: 4,
|
||||||
"data_addtolist": 2,
|
data_addtolist: 2,
|
||||||
"data_deleteoflist": 2,
|
data_deleteoflist: 2,
|
||||||
"data_setvariableto": 2,
|
data_setvariableto: 2,
|
||||||
"event_whenflagclicked": 4,
|
event_whenflagclicked: 4,
|
||||||
"looks_changeeffectby": 1,
|
looks_changeeffectby: 1,
|
||||||
"looks_seteffectto": 1,
|
looks_seteffectto: 1,
|
||||||
"motion_direction": 2,
|
motion_direction: 2,
|
||||||
"motion_ifonedgebounce": 1,
|
motion_ifonedgebounce: 1,
|
||||||
"motion_movesteps": 1,
|
motion_movesteps: 1,
|
||||||
"motion_turnright": 1,
|
motion_turnright: 1,
|
||||||
"motion_xposition": 1,
|
motion_xposition: 1,
|
||||||
"operator_random": 4,
|
operator_random: 4,
|
||||||
"procedures_call": 1,
|
procedures_call: 1,
|
||||||
"procedures_definition": 1,
|
procedures_definition: 1,
|
||||||
"wedo2_setLightHue": 1,
|
wedo2_setLightHue: 1,
|
||||||
"wedo2_whenTilted": 1
|
wedo2_whenTilted: 1
|
||||||
});
|
});
|
||||||
|
|
||||||
t.type(result.extensions, 'object');
|
t.type(result.extensions, 'object');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue