Merge pull request from towerofnix/itemnum-vm

Implement "item # of (thing) in (list)" block
This commit is contained in:
Eric Rosenbaum 2018-05-03 18:08:31 -04:00 committed by GitHub
commit 4cee734cde
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 0 deletions

49
test/unit/blocks_data.js Normal file
View file

@ -0,0 +1,49 @@
const test = require('tap').test;
const Data = require('../../src/blocks/scratch3_data');
const blocks = new Data();
const lists = {};
const util = {
target: {
lookupOrCreateList (id, name) {
if (!(name in lists)) {
lists[name] = {value: []};
}
return lists[name];
}
}
};
test('getItemNumOfList returns the index of an item (basic)', t => {
lists.list = {value: ['apple', 'taco', 'burrito', 'extravaganza']};
const args = {ITEM: 'burrito', LIST: {name: 'list'}};
const index = blocks.getItemNumOfList(args, util);
t.strictEqual(index, 3);
t.end();
});
test('getItemNumOfList returns 0 when an item is not found', t => {
lists.list = {value: ['aaaaapple', 'burrito']};
const args = {ITEM: 'jump', LIST: {name: 'list'}};
const index = blocks.getItemNumOfList(args, util);
t.strictEqual(index, 0);
t.end();
});
test('getItemNumOfList uses Scratch comparison', t => {
lists.list = {value: ['jump', 'Jump', '123', 123, 800]};
const args = {LIST: {name: 'list'}};
// Be case-insensitive:
args.ITEM = 'Jump';
t.strictEqual(blocks.getItemNumOfList(args, util), 1);
// Be type-insensitive:
args.ITEM = 123;
t.strictEqual(blocks.getItemNumOfList(args, util), 3);
args.ITEM = '800';
t.strictEqual(blocks.getItemNumOfList(args, util), 5);
t.end();
});