names_test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * @license
  3. * Blockly Tests
  4. *
  5. * Copyright 2012 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_safeName() {
  22. var varDB = new Blockly.Names('window,door');
  23. assertEquals('SafeName empty.', 'unnamed', varDB.safeName_(''));
  24. assertEquals('SafeName ok.', 'foobar', varDB.safeName_('foobar'));
  25. assertEquals('SafeName number start.', 'my_9lives',
  26. varDB.safeName_('9lives'));
  27. assertEquals('SafeName number end.', 'lives9', varDB.safeName_('lives9'));
  28. assertEquals('SafeName special chars.', '____', varDB.safeName_('!@#$'));
  29. assertEquals('SafeName reserved.', 'door', varDB.safeName_('door'));
  30. }
  31. function test_getName() {
  32. var varDB = new Blockly.Names('window,door');
  33. assertEquals('Name add #1.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
  34. assertEquals('Name get #1.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
  35. assertEquals('Name add #2.', 'Foo_bar2', varDB.getName('Foo bar', 'var'));
  36. assertEquals('Name get #2.', 'Foo_bar2', varDB.getName('foo BAR', 'var'));
  37. assertEquals('Name add #3.', 'door2', varDB.getName('door', 'var'));
  38. assertEquals('Name add #4.', 'Foo_bar3', varDB.getName('Foo.bar', 'proc'));
  39. assertEquals('Name get #1b.', 'Foo_bar', varDB.getName('Foo.bar', 'var'));
  40. assertEquals('Name get #4.', 'Foo_bar3', varDB.getName('Foo.bar', 'proc'));
  41. }
  42. function test_getDistinctName() {
  43. var varDB = new Blockly.Names('window,door');
  44. assertEquals('Name distinct #1.', 'Foo_bar',
  45. varDB.getDistinctName('Foo.bar', 'var'));
  46. assertEquals('Name distinct #2.', 'Foo_bar2',
  47. varDB.getDistinctName('Foo.bar', 'var'));
  48. assertEquals('Name distinct #3.', 'Foo_bar3',
  49. varDB.getDistinctName('Foo.bar', 'proc'));
  50. varDB.reset();
  51. assertEquals('Name distinct #4.', 'Foo_bar',
  52. varDB.getDistinctName('Foo.bar', 'var'));
  53. }
  54. function test_nameEquals() {
  55. assertTrue('Name equals #1.', Blockly.Names.equals('Foo.bar', 'Foo.bar'));
  56. assertFalse('Name equals #2.', Blockly.Names.equals('Foo.bar', 'Foo_bar'));
  57. assertTrue('Name equals #3.', Blockly.Names.equals('Foo.bar', 'FOO.BAR'));
  58. }