scratch-vm/test/unit/engine_runtime.js

111 lines
2.8 KiB
JavaScript
Raw Normal View History

const test = require('tap').test;
const Runtime = require('../../src/engine/runtime');
2017-05-30 10:24:09 -04:00
const MonitorRecord = require('../../src/engine/monitor-record');
2017-05-19 17:28:00 -04:00
const {Map} = require('immutable');
2016-04-18 17:20:30 -04:00
test('spec', t => {
const r = new Runtime();
2016-04-18 17:20:30 -04:00
t.type(Runtime, 'function');
t.type(r, 'object');
t.ok(r instanceof Runtime);
t.end();
});
2017-05-19 12:28:05 -04:00
2017-05-19 17:28:00 -04:00
test('monitorStateEquals', t => {
2017-05-19 12:28:05 -04:00
const r = new Runtime();
2017-05-19 17:28:00 -04:00
const id = 'xklj4#!';
const prevMonitorState = MonitorRecord({
2017-05-19 17:28:00 -04:00
id,
opcode: 'turtle whereabouts',
value: '25'
2017-05-19 17:28:00 -04:00
});
const newMonitorDelta = Map({
id,
2017-05-19 12:28:05 -04:00
value: String(25)
2017-05-19 17:28:00 -04:00
});
r.requestAddMonitor(prevMonitorState);
r.requestUpdateMonitor(newMonitorDelta);
t.equals(true, prevMonitorState === r._monitorState.get(id));
2017-05-19 17:28:00 -04:00
t.equals(String(25), r._monitorState.get(id).get('value'));
2017-05-19 12:28:05 -04:00
t.end();
});
2017-05-19 17:28:00 -04:00
test('monitorStateDoesNotEqual', t => {
2017-05-19 12:28:05 -04:00
const r = new Runtime();
2017-05-19 17:28:00 -04:00
const id = 'xklj4#!';
const params = {seven: 7};
const prevMonitorState = MonitorRecord({
2017-05-19 17:28:00 -04:00
id,
opcode: 'turtle whereabouts',
value: '25'
2017-05-19 17:28:00 -04:00
});
2017-05-19 12:28:05 -04:00
// Value change
2017-05-19 17:28:00 -04:00
let newMonitorDelta = Map({
id,
2017-05-19 12:28:05 -04:00
value: String(24)
2017-05-19 17:28:00 -04:00
});
r.requestAddMonitor(prevMonitorState);
r.requestUpdateMonitor(newMonitorDelta);
2018-09-12 17:23:01 -04:00
2017-05-19 17:28:00 -04:00
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).get('value'));
2017-05-19 12:28:05 -04:00
// Prop change
2017-05-19 17:28:00 -04:00
newMonitorDelta = Map({
2017-05-19 12:28:05 -04:00
id: 'xklj4#!',
params: params
2017-05-19 17:28:00 -04:00
});
r.requestUpdateMonitor(newMonitorDelta);
t.equals(false, prevMonitorState.equals(r._monitorState.get(id)));
t.equals(String(24), r._monitorState.get(id).value);
t.equals(params, r._monitorState.get(id).params);
2017-05-19 12:28:05 -04:00
t.end();
});
2018-09-12 17:23:01 -04:00
test('getLabelForOpcode', t => {
const r = new Runtime();
const fakeExtension = {
id: 'fakeExtension',
name: 'Fake Extension',
blocks: [
{
info: {
opcode: 'foo',
json: {},
text: 'Foo',
xml: ''
}
},
{
info: {
opcode: 'foo_2',
json: {},
text: 'Foo 2',
xml: ''
}
}
]
};
r._blockInfo.push(fakeExtension);
const result1 = r.getLabelForOpcode('fakeExtension_foo');
t.type(result1.category, 'string');
t.type(result1.label, 'string');
t.equals(result1.label, 'Fake Extension: Foo');
const result2 = r.getLabelForOpcode('fakeExtension_foo_2');
t.type(result2.category, 'string');
t.type(result2.label, 'string');
t.equals(result2.label, 'Fake Extension: Foo 2');
t.end();
});