blockly_test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license
  3. * Visual Blocks Editor
  4. *
  5. * Copyright 2011 Google Inc.
  6. * https://developers.google.com/blockly/
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License");
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. 'use strict';
  21. function test_genUid() {
  22. var uuids = {};
  23. for (var i = 0; i < 1000; i++) {
  24. var uuid = Blockly.genUid();
  25. assertFalse('UUID different: ' + uuid, uuid in uuids);
  26. uuids[uuid] = true;
  27. }
  28. }
  29. function test_addClass() {
  30. var p = document.createElement('p');
  31. Blockly.addClass_(p, 'one');
  32. assertEquals('Adding "one"', 'one', p.className);
  33. Blockly.addClass_(p, 'one');
  34. assertEquals('Adding duplicate "one"', 'one', p.className);
  35. Blockly.addClass_(p, 'two');
  36. assertEquals('Adding "two"', 'one two', p.className);
  37. Blockly.addClass_(p, 'two');
  38. assertEquals('Adding duplicate "two"', 'one two', p.className);
  39. Blockly.addClass_(p, 'three');
  40. assertEquals('Adding "three"', 'one two three', p.className);
  41. }
  42. function test_removeClass() {
  43. var p = document.createElement('p');
  44. p.className = ' one three two three ';
  45. Blockly.removeClass_(p, 'two');
  46. assertEquals('Removing "two"', 'one three three', p.className);
  47. Blockly.removeClass_(p, 'four');
  48. assertEquals('Removing "four"', 'one three three', p.className);
  49. Blockly.removeClass_(p, 'three');
  50. assertEquals('Removing "three"', 'one', p.className);
  51. Blockly.removeClass_(p, 'ne');
  52. assertEquals('Removing "ne"', 'one', p.className);
  53. Blockly.removeClass_(p, 'one');
  54. assertEquals('Removing "one"', '', p.className);
  55. Blockly.removeClass_(p, 'zero');
  56. assertEquals('Removing "zero"', '', p.className);
  57. }
  58. function test_hasClass() {
  59. var p = document.createElement('p');
  60. p.className = ' one three two three ';
  61. assertTrue('Has "one"', Blockly.hasClass_(p, 'one'));
  62. assertTrue('Has "two"', Blockly.hasClass_(p, 'two'));
  63. assertTrue('Has "three"', Blockly.hasClass_(p, 'three'));
  64. assertFalse('Has no "four"', Blockly.hasClass_(p, 'four'));
  65. assertFalse('Has no "t"', Blockly.hasClass_(p, 't'));
  66. }
  67. function test_shortestStringLength() {
  68. var len = Blockly.shortestStringLength('one,two,three,four,five'.split(','));
  69. assertEquals('Length of "one"', 3, len);
  70. len = Blockly.shortestStringLength('one,two,three,four,five,'.split(','));
  71. assertEquals('Length of ""', 0, len);
  72. len = Blockly.shortestStringLength(['Hello World']);
  73. assertEquals('List of one', 11, len);
  74. len = Blockly.shortestStringLength([]);
  75. assertEquals('Empty list', 0, len);
  76. }
  77. function test_commonWordPrefix() {
  78. var len = Blockly.commonWordPrefix('one,two,three,four,five'.split(','));
  79. assertEquals('No prefix', 0, len);
  80. len = Blockly.commonWordPrefix('Xone,Xtwo,Xthree,Xfour,Xfive'.split(','));
  81. assertEquals('No word prefix', 0, len);
  82. len = Blockly.commonWordPrefix('abc de,abc de,abc de,abc de'.split(','));
  83. assertEquals('Full equality', 6, len);
  84. len = Blockly.commonWordPrefix('abc deX,abc deY'.split(','));
  85. assertEquals('One word prefix', 4, len);
  86. len = Blockly.commonWordPrefix('abc de,abc deY'.split(','));
  87. assertEquals('Overflow no', 4, len);
  88. len = Blockly.commonWordPrefix('abc de,abc de Y'.split(','));
  89. assertEquals('Overflow yes', 6, len);
  90. len = Blockly.commonWordPrefix(['Hello World']);
  91. assertEquals('List of one', 11, len);
  92. len = Blockly.commonWordPrefix([]);
  93. assertEquals('Empty list', 0, len);
  94. len = Blockly.commonWordPrefix('turn&nbsp;left,turn&nbsp;right'.split(','));
  95. assertEquals('No prefix due to &amp;nbsp;', 0, len);
  96. len = Blockly.commonWordPrefix('turn\u00A0left,turn\u00A0right'.split(','));
  97. assertEquals('No prefix due to \\u00A0', 0, len);
  98. }
  99. function test_commonWordSuffix() {
  100. var len = Blockly.commonWordSuffix('one,two,three,four,five'.split(','));
  101. assertEquals('No prefix', 0, len);
  102. len = Blockly.commonWordSuffix('oneX,twoX,threeX,fourX,fiveX'.split(','));
  103. assertEquals('No word prefix', 0, len);
  104. len = Blockly.commonWordSuffix('abc de,abc de,abc de,abc de'.split(','));
  105. assertEquals('Full equality', 6, len);
  106. len = Blockly.commonWordSuffix('Xabc de,Yabc de'.split(','));
  107. assertEquals('One word prefix', 3, len);
  108. len = Blockly.commonWordSuffix('abc de,Yabc de'.split(','));
  109. assertEquals('Overflow no', 3, len);
  110. len = Blockly.commonWordSuffix('abc de,Y abc de'.split(','));
  111. assertEquals('Overflow yes', 6, len);
  112. len = Blockly.commonWordSuffix(['Hello World']);
  113. assertEquals('List of one', 11, len);
  114. len = Blockly.commonWordSuffix([]);
  115. assertEquals('Empty list', 0, len);
  116. }
  117. function test_tokenizeInterpolation() {
  118. var tokens = Blockly.utils.tokenizeInterpolation('');
  119. assertArrayEquals('Null interpolation', [], tokens);
  120. tokens = Blockly.utils.tokenizeInterpolation('Hello');
  121. assertArrayEquals('No interpolation', ['Hello'], tokens);
  122. tokens = Blockly.utils.tokenizeInterpolation('Hello%World');
  123. assertArrayEquals('Unescaped %.', ['Hello%World'], tokens);
  124. tokens = Blockly.utils.tokenizeInterpolation('Hello%%World');
  125. assertArrayEquals('Escaped %.', ['Hello%World'], tokens);
  126. tokens = Blockly.utils.tokenizeInterpolation('Hello %1 World');
  127. assertArrayEquals('Interpolation.', ['Hello ', 1, ' World'], tokens);
  128. tokens = Blockly.utils.tokenizeInterpolation('%123Hello%456World%789');
  129. assertArrayEquals('Interpolations.', [123, 'Hello', 456, 'World', 789], tokens);
  130. tokens = Blockly.utils.tokenizeInterpolation('%%%x%%0%00%01%');
  131. assertArrayEquals('Torture interpolations.', ['%%x%0', 0, 1, '%'], tokens);
  132. }