2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const Operators = require('../../src/blocks/scratch3_operators');
|
2016-10-03 21:43:31 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
const blocks = new Operators(null);
|
2016-10-03 21:43:31 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('getPrimitives', t => {
|
2016-10-03 21:43:31 -04:00
|
|
|
t.type(blocks.getPrimitives(), 'object');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('add', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.add({NUM1: '1', NUM2: '1'}), 2);
|
|
|
|
t.strictEqual(blocks.add({NUM1: 'foo', NUM2: 'bar'}), 0);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('subtract', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.subtract({NUM1: '1', NUM2: '1'}), 0);
|
|
|
|
t.strictEqual(blocks.subtract({NUM1: 'foo', NUM2: 'bar'}), 0);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('multiply', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.multiply({NUM1: '2', NUM2: '2'}), 4);
|
|
|
|
t.strictEqual(blocks.multiply({NUM1: 'foo', NUM2: 'bar'}), 0);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('divide', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.divide({NUM1: '2', NUM2: '2'}), 1);
|
2017-08-26 13:07:47 -04:00
|
|
|
t.strictEqual(blocks.divide({NUM1: '1', NUM2: '0'}), Infinity); // @todo
|
|
|
|
t.ok(isNaN(blocks.divide({NUM1: 'foo', NUM2: 'bar'}))); // @todo
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('lt', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.lt({OPERAND1: '1', OPERAND2: '2'}), true);
|
|
|
|
t.strictEqual(blocks.lt({OPERAND1: '2', OPERAND2: '1'}), false);
|
|
|
|
t.strictEqual(blocks.lt({OPERAND1: '1', OPERAND2: '1'}), false);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('equals', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.equals({OPERAND1: '1', OPERAND2: '2'}), false);
|
|
|
|
t.strictEqual(blocks.equals({OPERAND1: '2', OPERAND2: '1'}), false);
|
|
|
|
t.strictEqual(blocks.equals({OPERAND1: '1', OPERAND2: '1'}), true);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('gt', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.gt({OPERAND1: '1', OPERAND2: '2'}), false);
|
|
|
|
t.strictEqual(blocks.gt({OPERAND1: '2', OPERAND2: '1'}), true);
|
|
|
|
t.strictEqual(blocks.gt({OPERAND1: '1', OPERAND2: '1'}), false);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('and', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.and({OPERAND1: true, OPERAND2: true}), true);
|
|
|
|
t.strictEqual(blocks.and({OPERAND1: true, OPERAND2: false}), false);
|
|
|
|
t.strictEqual(blocks.and({OPERAND1: false, OPERAND2: false}), false);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('or', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.or({OPERAND1: true, OPERAND2: true}), true);
|
|
|
|
t.strictEqual(blocks.or({OPERAND1: true, OPERAND2: false}), true);
|
|
|
|
t.strictEqual(blocks.or({OPERAND1: false, OPERAND2: false}), false);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('not', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.not({OPERAND: true}), false);
|
|
|
|
t.strictEqual(blocks.not({OPERAND: false}), true);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('random', t => {
|
|
|
|
const min = 0;
|
|
|
|
const max = 100;
|
|
|
|
const result = blocks.random({FROM: min, TO: max});
|
2016-10-03 21:43:31 -04:00
|
|
|
t.ok(result >= min);
|
|
|
|
t.ok(result <= max);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('random - equal', t => {
|
|
|
|
const min = 1;
|
|
|
|
const max = 1;
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.random({FROM: min, TO: max}), min);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('random - decimal', t => {
|
|
|
|
const min = 0.1;
|
|
|
|
const max = 10;
|
|
|
|
const result = blocks.random({FROM: min, TO: max});
|
2016-10-03 21:43:31 -04:00
|
|
|
t.ok(result >= min);
|
|
|
|
t.ok(result <= max);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('random - int', t => {
|
|
|
|
const min = 0;
|
|
|
|
const max = 10;
|
|
|
|
const result = blocks.random({FROM: min, TO: max});
|
2016-10-03 21:43:31 -04:00
|
|
|
t.ok(result >= min);
|
|
|
|
t.ok(result <= max);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('random - reverse', t => {
|
|
|
|
const min = 0;
|
|
|
|
const max = 10;
|
|
|
|
const result = blocks.random({FROM: max, TO: min});
|
2016-10-03 21:43:31 -04:00
|
|
|
t.ok(result >= min);
|
|
|
|
t.ok(result <= max);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('join', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.join({STRING1: 'foo', STRING2: 'bar'}), 'foobar');
|
|
|
|
t.strictEqual(blocks.join({STRING1: '1', STRING2: '2'}), '12');
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('letterOf', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 0}), '');
|
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 1}), 'f');
|
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 2}), 'o');
|
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 3}), 'o');
|
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 4}), '');
|
|
|
|
t.strictEqual(blocks.letterOf({STRING: 'foo', LETTER: 'bar'}), '');
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('length', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.length({STRING: ''}), 0);
|
|
|
|
t.strictEqual(blocks.length({STRING: 'foo'}), 3);
|
|
|
|
t.strictEqual(blocks.length({STRING: '1'}), 1);
|
|
|
|
t.strictEqual(blocks.length({STRING: '100'}), 3);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-08-10 09:37:10 -04:00
|
|
|
test('contains', t => {
|
|
|
|
t.strictEqual(blocks.contains({STRING1: 'hello world', STRING2: 'hello'}), true);
|
|
|
|
t.strictEqual(blocks.contains({STRING1: 'foo', STRING2: 'bar'}), false);
|
|
|
|
t.strictEqual(blocks.contains({STRING1: 'HeLLo world', STRING2: 'hello'}), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('mod', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.mod({NUM1: 1, NUM2: 1}), 0);
|
|
|
|
t.strictEqual(blocks.mod({NUM1: 3, NUM2: 6}), 3);
|
|
|
|
t.strictEqual(blocks.mod({NUM1: -3, NUM2: 6}), 3);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('round', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.round({NUM: 1}), 1);
|
|
|
|
t.strictEqual(blocks.round({NUM: 1.1}), 1);
|
|
|
|
t.strictEqual(blocks.round({NUM: 1.5}), 2);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('mathop', t => {
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'abs', NUM: -1}), 1);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'floor', NUM: 1.5}), 1);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'ceiling', NUM: 0.1}), 1);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'sqrt', NUM: 1}), 1);
|
2017-01-31 19:05:54 -05:00
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'sin', NUM: 1}), 0.0174524064);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'sin', NUM: 90}), 1);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'cos', NUM: 1}), 0.9998476952);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'cos', NUM: 180}), -1);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'tan', NUM: 1}), 0.0174550649);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'tan', NUM: 90}), Infinity);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'tan', NUM: 180}), 0);
|
2016-10-23 12:41:45 -04:00
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'asin', NUM: 1}), 90);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'acos', NUM: 1}), 0);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'atan', NUM: 1}), 45);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'ln', NUM: 1}), 0);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'log', NUM: 1}), 0);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'e ^', NUM: 1}), 2.718281828459045);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: '10 ^', NUM: 1}), 10);
|
|
|
|
t.strictEqual(blocks.mathop({OPERATOR: 'undefined', NUM: 1}), 0);
|
2016-10-03 21:43:31 -04:00
|
|
|
t.end();
|
|
|
|
});
|