Spaces:
Running
Running
File size: 29,905 Bytes
30c32c8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 |
const test = require('tap').test;
const Target = require('../../src/engine/target');
const Variable = require('../../src/engine/variable');
const adapter = require('../../src/engine/adapter');
const Runtime = require('../../src/engine/runtime');
const events = require('../fixtures/events.json');
test('spec', t => {
const target = new Target(new Runtime());
t.type(Target, 'function');
t.type(target, 'object');
t.ok(target instanceof Target);
t.type(target.id, 'string');
t.type(target.blocks, 'object');
t.type(target.variables, 'object');
t.type(target.comments, 'object');
t.type(target._customState, 'object');
t.type(target.createVariable, 'function');
t.type(target.renameVariable, 'function');
t.end();
});
// Create Variable tests.
test('createVariable', t => {
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar');
t.equal(variable.type, Variable.SCALAR_TYPE);
t.equal(variable.value, 0);
t.equal(variable.isCloud, false);
t.end();
});
// Create Same Variable twice.
test('createVariable2', t => {
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
t.end();
});
// Create a list
test('createListVariable creates a list', t => {
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar');
t.equal(variable.type, Variable.LIST_TYPE);
t.assert(variable.value instanceof Array, true);
t.equal(variable.value.length, 0);
t.equal(variable.isCloud, false);
t.end();
});
test('createVariable calls cloud io device\'s requestCreateVariable', t => {
const runtime = new Runtime();
// Mock the requestCreateVariable function
let requestCreateCloudWasCalled = false;
runtime.ioDevices.cloud.requestCreateVariable = () => {
requestCreateCloudWasCalled = true;
};
const target = new Target(runtime);
target.isStage = true;
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar');
t.equal(variable.type, Variable.SCALAR_TYPE);
t.equal(variable.value, 0);
t.equal(variable.isCloud, true);
t.equal(requestCreateCloudWasCalled, true);
t.end();
});
test('createVariable does not call cloud io device\'s requestCreateVariable if target is not stage', t => {
const runtime = new Runtime();
// Mock the requestCreateVariable function
let requestCreateCloudWasCalled = false;
runtime.ioDevices.cloud.requestCreateVariable = () => {
requestCreateCloudWasCalled = true;
};
const target = new Target(runtime);
target.isStage = false;
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE, true /* isCloud */);
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar');
t.equal(variable.type, Variable.SCALAR_TYPE);
t.equal(variable.value, 0);
// isCloud flag doesn't get set if the target is not the stage
t.equal(variable.isCloud, false);
t.equal(requestCreateCloudWasCalled, false);
t.end();
});
test('createVariable throws when given invalid type', t => {
const target = new Target(new Runtime());
t.throws(
(() => target.createVariable('foo', 'bar', 'baz')),
new Error('Invalid variable type: baz')
);
t.end();
});
// Rename Variable tests.
test('renameVariable', t => {
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar2');
t.equal(variable.value, 0);
t.equal(variable.isCloud, false);
t.end();
});
// Rename Variable that doesn't exist.
test('renameVariable2', t => {
const target = new Target(new Runtime());
target.renameVariable('foo', 'bar2');
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
t.end();
});
// Rename Variable that with id that exists as another variable's name.
// Expect no change.
test('renameVariable3', t => {
const target = new Target(new Runtime());
target.createVariable('foo1', 'foo', Variable.SCALAR_TYPE);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo1');
t.equal(variable.name, 'foo');
t.end();
});
test('renameVariable calls cloud io device\'s requestRenameVariable function', t => {
const runtime = new Runtime();
let requestRenameVariableWasCalled = false;
runtime.ioDevices.cloud.requestRenameVariable = () => {
requestRenameVariableWasCalled = true;
};
const target = new Target(runtime);
target.isStage = true;
const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);
target.variables[mockCloudVar.id] = mockCloudVar;
runtime.addTarget(target);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar2');
t.equal(variable.value, 0);
t.equal(variable.isCloud, true);
t.equal(requestRenameVariableWasCalled, true);
t.end();
});
test('renameVariable does not call cloud io device\'s requestRenameVariable function if target is not stage', t => {
const runtime = new Runtime();
let requestRenameVariableWasCalled = false;
runtime.ioDevices.cloud.requestRenameVariable = () => {
requestRenameVariableWasCalled = true;
};
const target = new Target(runtime);
const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);
target.variables[mockCloudVar.id] = mockCloudVar;
runtime.addTarget(target);
target.renameVariable('foo', 'bar2');
const variables = target.variables;
t.equal(Object.keys(variables).length, 1);
const variable = variables[Object.keys(variables)[0]];
t.equal(variable.id, 'foo');
t.equal(variable.name, 'bar2');
t.equal(variable.value, 0);
t.equal(variable.isCloud, true);
t.equal(requestRenameVariableWasCalled, false);
t.end();
});
// Delete Variable tests.
test('deleteVariable', t => {
const target = new Target(new Runtime());
target.createVariable('foo', 'bar', Variable.SCALAR_TYPE);
target.deleteVariable('foo');
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
t.end();
});
// Delete Variable that doesn't exist.
test('deleteVariable2', t => {
const target = new Target(new Runtime());
target.deleteVariable('foo');
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
t.end();
});
test('deleteVariable calls cloud io device\'s requestRenameVariable function', t => {
const runtime = new Runtime();
let requestDeleteVariableWasCalled = false;
runtime.ioDevices.cloud.requestDeleteVariable = () => {
requestDeleteVariableWasCalled = true;
};
const target = new Target(runtime);
target.isStage = true;
const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);
target.variables[mockCloudVar.id] = mockCloudVar;
runtime.addTarget(target);
target.deleteVariable('foo');
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
t.equal(requestDeleteVariableWasCalled, true);
t.end();
});
test('deleteVariable calls cloud io device\'s requestRenameVariable function', t => {
const runtime = new Runtime();
let requestDeleteVariableWasCalled = false;
runtime.ioDevices.cloud.requestDeleteVariable = () => {
requestDeleteVariableWasCalled = true;
};
const target = new Target(runtime);
const mockCloudVar = new Variable('foo', 'bar', Variable.SCALAR_TYPE, true);
target.variables[mockCloudVar.id] = mockCloudVar;
runtime.addTarget(target);
target.deleteVariable('foo');
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
t.equal(requestDeleteVariableWasCalled, false);
t.end();
});
test('duplicateVariable creates a new variable with a new ID by default', t => {
const target = new Target(new Runtime());
target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
const originalVariable = target.variables['a var ID'];
originalVariable.value = 10;
const newVariable = target.duplicateVariable('a var ID');
// Duplicating a variable should not add the variable to the current target
t.equal(Object.keys(target.variables).length, 1);
// Duplicate variable should have a different ID from the original unless specified to keep the original ID.
t.notEqual(newVariable.id, 'a var ID');
t.type(target.variables[newVariable.id], 'undefined');
// Duplicate variable should start out with the same value as the original variable
t.equal(newVariable.value, originalVariable.value);
// Modifying one variable should not modify the other
newVariable.value = 15;
t.notEqual(newVariable.value, originalVariable.value);
t.equal(originalVariable.value, 10);
t.end();
});
test('duplicateVariable creates new array reference for list variable.value', t => {
const target = new Target(new Runtime());
const arr = [1, 2, 3];
target.createVariable('a var ID', 'arr', Variable.LIST_TYPE);
const originalVariable = target.variables['a var ID'];
originalVariable.value = arr;
const newVariable = target.duplicateVariable('a var ID');
// Values are deeply equal but not the same object
t.deepEqual(originalVariable.value, newVariable.value);
t.notEqual(originalVariable.value, newVariable.value);
t.end();
});
test('duplicateVariable creates a new variable with a original ID if specified', t => {
const target = new Target(new Runtime());
target.createVariable('a var ID', 'foo', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
const originalVariable = target.variables['a var ID'];
originalVariable.value = 10;
const newVariable = target.duplicateVariable('a var ID', true);
// Duplicating a variable should not add the variable to the current target
t.equal(Object.keys(target.variables).length, 1);
// Duplicate variable should have the same ID as the original when specified
t.equal(newVariable.id, 'a var ID');
// Duplicate variable should start out with the same value as the original variable
t.equal(newVariable.value, originalVariable.value);
// Modifying one variable should not modify the other
newVariable.value = 15;
t.notEqual(newVariable.value, originalVariable.value);
t.equal(originalVariable.value, 10);
// The target should still have the original variable with the original value
t.equal(target.variables['a var ID'].value, 10);
t.end();
});
test('duplicateVariable returns null if variable with specified ID does not exist', t => {
const target = new Target(new Runtime());
const variable = target.duplicateVariable('a var ID');
t.equal(variable, null);
t.equal(Object.keys(target.variables).length, 0);
target.createVariable('var id', 'foo', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
const anotherVariable = target.duplicateVariable('another var ID');
t.equal(anotherVariable, null);
t.equal(Object.keys(target.variables).length, 1);
t.type(target.variables['another var ID'], 'undefined');
t.type(target.variables['var id'], 'object');
t.notEqual(target.variables['var id'], null);
t.end();
});
test('duplicateVariables duplicates all variables', t => {
const target = new Target(new Runtime());
target.createVariable('var ID 1', 'var1', Variable.SCALAR_TYPE);
target.createVariable('var ID 2', 'var2', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 2);
const var1 = target.variables['var ID 1'];
const var2 = target.variables['var ID 2'];
var1.value = 3;
var2.value = 'foo';
const duplicateVariables = target.duplicateVariables();
// Duplicating a target's variables should not change the target's own variables.
t.equal(Object.keys(target.variables).length, 2);
t.equal(Object.keys(duplicateVariables).length, 2);
// Should be able to find original var IDs in both this target's variables and
// the duplicate variables since a blocks container was not specified.
t.equal(target.variables.hasOwnProperty('var ID 1'), true);
t.equal(target.variables.hasOwnProperty('var ID 2'), true);
t.equal(duplicateVariables.hasOwnProperty('var ID 1'), true);
t.equal(duplicateVariables.hasOwnProperty('var ID 1'), true);
// Values of the duplicate varaiables should match the value of the original values at the time of duplication
t.equal(target.variables['var ID 1'].value, duplicateVariables['var ID 1'].value);
t.equal(duplicateVariables['var ID 1'].value, 3);
t.equal(target.variables['var ID 2'].value, duplicateVariables['var ID 2'].value);
t.equal(duplicateVariables['var ID 2'].value, 'foo');
// The two sets of variables should still be distinct, modifying the target's variables
// should not affect the duplicated variables, and vice-versa
var1.value = 10;
t.equal(target.variables['var ID 1'].value, 10);
t.equal(duplicateVariables['var ID 1'].value, 3); // should remain unchanged from initial value
duplicateVariables['var ID 2'].value = 'bar';
t.equal(target.variables['var ID 2'].value, 'foo');
// Deleting a variable on the target should not change the duplicated variables
target.deleteVariable('var ID 1');
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(duplicateVariables).length, 2);
t.type(duplicateVariables['var ID 1'], 'object');
t.notEqual(duplicateVariables['var ID 1'], null);
t.end();
});
test('duplicateVariables re-IDs variables when a block container is provided', t => {
const target = new Target(new Runtime());
target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);
target.createVariable('another var id', 'var2', Variable.SCALAR_TYPE);
// Create a block on the target which references the variable with id 'mock var id'
target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');
// Deep clone this target's blocks to pass in to 'duplicateVariables'
const copiedBlocks = target.blocks.duplicate();
// The copied block should still have the same ID, and its VARIABLE field should still refer to
// the original variable id
t.type(copiedBlocks.getBlock('a block'), 'object');
t.type(copiedBlocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');
const duplicateVariables = target.duplicateVariables(copiedBlocks);
// Duplicate variables should have new IDs
t.equal(Object.keys(duplicateVariables).length, 2);
t.type(duplicateVariables['mock var id'], 'undefined');
t.type(duplicateVariables['another var id'], 'undefined');
// Duplicate variables still have the same names..
const dupes = Object.values(duplicateVariables);
const dupeVarNames = dupes.map(v => v.name);
t.notEqual(dupeVarNames.indexOf('a mock variable'), -1);
t.notEqual(dupeVarNames.indexOf('var2'), -1);
// Duplicating variables should not change blocks on current target
t.type(target.blocks.getBlock('a block'), 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');
// The copied blocks passed into duplicateVariables should now reference the new
// variable ID
const mockVariableDupe = dupes[dupeVarNames.indexOf('a mock variable')];
const mockVarDupeID = mockVariableDupe.id;
t.type(copiedBlocks.getBlock('a block'), 'object');
t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.id, mockVarDupeID);
t.equal(copiedBlocks.getBlock('a block').fields.VARIABLE.value, 'a mock variable');
t.end();
});
test('lookupOrCreateList creates a list if var with given id or var with given name does not exist', t => {
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
const listVar = target.lookupOrCreateList('foo', 'bar');
t.equal(Object.keys(variables).length, 1);
t.equal(listVar.id, 'foo');
t.equal(listVar.name, 'bar');
t.end();
});
test('lookupOrCreateList returns list if one with given id exists', t => {
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
t.equal(Object.keys(variables).length, 1);
const listVar = target.lookupOrCreateList('foo', 'bar');
t.equal(Object.keys(variables).length, 1);
t.equal(listVar.id, 'foo');
t.equal(listVar.name, 'bar');
t.end();
});
test('lookupOrCreateList succeeds in finding list if id is incorrect but name matches', t => {
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
target.createVariable('foo', 'bar', Variable.LIST_TYPE);
t.equal(Object.keys(variables).length, 1);
const listVar = target.lookupOrCreateList('not foo', 'bar');
t.equal(Object.keys(variables).length, 1);
t.equal(listVar.id, 'foo');
t.equal(listVar.name, 'bar');
t.end();
});
test('lookupBroadcastMsg returns the var with given id if exists', t => {
const target = new Target(new Runtime());
const variables = target.variables;
t.equal(Object.keys(variables).length, 0);
target.createVariable('foo', 'bar', Variable.BROADCAST_MESSAGE_TYPE);
t.equal(Object.keys(variables).length, 1);
const broadcastMsg = target.lookupBroadcastMsg('foo', 'bar');
t.equal(Object.keys(variables).length, 1);
t.equal(broadcastMsg.id, 'foo');
t.equal(broadcastMsg.name, 'bar');
t.end();
});
test('createComment adds a comment to the target', t => {
const target = new Target(new Runtime());
const comments = target.comments;
t.equal(Object.keys(comments).length, 0);
target.createComment('a comment', null, 'some comment text',
10, 20, 200, 300, true);
t.equal(Object.keys(comments).length, 1);
const comment = comments['a comment'];
t.notEqual(comment, null);
t.equal(comment.blockId, null);
t.equal(comment.text, 'some comment text');
t.equal(comment.x, 10);
t.equal(comment.y, 20);
t.equal(comment.width, 200);
t.equal(comment.height, 300);
t.equal(comment.minimized, true);
t.end();
});
test('creating comment with id that already exists does not change existing comment', t => {
const target = new Target(new Runtime());
const comments = target.comments;
t.equal(Object.keys(comments).length, 0);
target.createComment('a comment', null, 'some comment text',
10, 20, 200, 300, true);
t.equal(Object.keys(comments).length, 1);
target.createComment('a comment', null,
'some new comment text', 40, 50, 300, 400, false);
const comment = comments['a comment'];
t.notEqual(comment, null);
// All of the comment properties should remain unchanged from the first
// time createComment was called
t.equal(comment.blockId, null);
t.equal(comment.text, 'some comment text');
t.equal(comment.x, 10);
t.equal(comment.y, 20);
t.equal(comment.width, 200);
t.equal(comment.height, 300);
t.equal(comment.minimized, true);
t.end();
});
test('creating a comment with a blockId also updates the comment property on the block', t => {
const target = new Target(new Runtime());
const comments = target.comments;
// Create a mock block on the target
target.blocks = {
'a mock block': {
id: 'a mock block'
}
};
// Mock the getBlock function that's used in commentCreate
target.blocks.getBlock = id => target.blocks[id];
t.equal(Object.keys(comments).length, 0);
target.createComment('a comment', 'a mock block', 'some comment text',
10, 20, 200, 300, true);
t.equal(Object.keys(comments).length, 1);
const comment = comments['a comment'];
t.equal(comment.blockId, 'a mock block');
t.equal(target.blocks.getBlock('a mock block').comment, 'a comment');
t.end();
});
test('fixUpVariableReferences fixes sprite global var conflicting with project global var', t => {
const runtime = new Runtime();
const stage = new Target(runtime);
stage.isStage = true;
const target = new Target(runtime);
target.isStage = false;
runtime.targets = [stage, target];
// Create a global variable
stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);
target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);
t.equal(Object.keys(target.variables).length, 0);
t.equal(Object.keys(stage.variables).length, 1);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
target.fixUpVariableReferences();
t.equal(Object.keys(target.variables).length, 0);
t.equal(Object.keys(stage.variables).length, 1);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'pre-existing global var id');
t.end();
});
test('fixUpVariableReferences fixes sprite local var conflicting with project global var', t => {
const runtime = new Runtime();
const stage = new Target(runtime);
stage.isStage = true;
const target = new Target(runtime);
target.isStage = false;
target.getName = () => 'Target';
runtime.targets = [stage, target];
// Create a global variable
stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);
target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);
target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 1);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.variables['mock var id'].name, 'a mock variable');
target.fixUpVariableReferences();
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 1);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.variables['mock var id'].name, 'Target: a mock variable');
t.end();
});
test('fixUpVariableReferences fixes conflicting sprite local var without blocks referencing var', t => {
const runtime = new Runtime();
const stage = new Target(runtime);
stage.isStage = true;
const target = new Target(runtime);
target.isStage = false;
target.getName = () => 'Target';
runtime.targets = [stage, target];
// Create a global variable
stage.createVariable('pre-existing global var id', 'a mock variable', Variable.SCALAR_TYPE);
target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 1);
t.equal(target.variables['mock var id'].name, 'a mock variable');
target.fixUpVariableReferences();
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 1);
t.equal(target.variables['mock var id'].name, 'Target: a mock variable');
t.end();
});
test('fixUpVariableReferences fixes sprite global var conflicting with other sprite\'s local var', t => {
const runtime = new Runtime();
const stage = new Target(runtime);
stage.isStage = true;
const target = new Target(runtime);
target.isStage = false;
const existingTarget = new Target(runtime);
existingTarget.isStage = false;
runtime.targets = [stage, target, existingTarget];
// Create a local variable on the pre-existing target
existingTarget.createVariable('pre-existing local var id', 'a mock variable', Variable.SCALAR_TYPE);
target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);
t.equal(Object.keys(existingTarget.variables).length, 1);
const existingVariable = Object.values(existingTarget.variables)[0];
t.equal(existingVariable.name, 'a mock variable');
t.equal(Object.keys(target.variables).length, 0);
t.equal(Object.keys(stage.variables).length, 0);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
target.fixUpVariableReferences();
t.equal(Object.keys(existingTarget.variables).length, 1);
t.equal(existingVariable.name, 'a mock variable');
t.equal(Object.keys(target.variables).length, 0);
t.equal(Object.keys(stage.variables).length, 1);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
const newGlobal = stage.variables[Object.keys(stage.variables)[0]];
t.equal(newGlobal.name, 'a mock variable2');
t.end();
});
test('fixUpVariableReferences does not change variable name if there is no variable conflict', t => {
const runtime = new Runtime();
const stage = new Target(runtime);
stage.isStage = true;
const target = new Target(runtime);
target.isStage = false;
target.getName = () => 'Target';
runtime.targets = [stage, target];
// Create a global variable
stage.createVariable('pre-existing global var id', 'a variable', Variable.SCALAR_TYPE);
stage.createVariable('pre-existing global list id', 'a mock variable', Variable.LIST_TYPE);
target.createVariable('mock var id', 'a mock variable', Variable.SCALAR_TYPE);
target.blocks.createBlock(adapter(events.mockVariableBlock)[0]);
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 2);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.variables['mock var id'].name, 'a mock variable');
target.fixUpVariableReferences();
t.equal(Object.keys(target.variables).length, 1);
t.equal(Object.keys(stage.variables).length, 2);
t.type(target.blocks.getBlock('a block'), 'object');
t.type(target.blocks.getBlock('a block').fields, 'object');
t.type(target.blocks.getBlock('a block').fields.VARIABLE, 'object');
t.equal(target.blocks.getBlock('a block').fields.VARIABLE.id, 'mock var id');
t.equal(target.variables['mock var id'].name, 'a mock variable');
t.end();
});
|