Files
blockly/tests/mocha/names_test.js
Beka Westberg 2edd228117 fix: move test helpers from samples into core (#5969)
* fix: move core test helpers into new directory

* fix: add test helpers to core and convert to goog modules

* fix: change tests to use local helpers

* fix: change local tests to use chai asserts

* fix: skip field tests in serializer b/c test blocks are unavailable

* fix: rename some helper files

* fix: rename some helper modules

* fix: split block helpers into code gen and serialization

* fix: split block defs into new helper file

* fix: split warning helpers into new file

* fix: split user input helpers into new file

* fix: split event helpers into a new file

* fix: split variable helper into new file

* fix: move remaining test helpers to new setup-teardown file

* fix: rename setup and teardown module

* fix: cleanup from rebase

* fix: undo accidental rename

* fix: lint?

* fix: bad toolbox definitions namespace

* fix: fixup warning helpers

* fix: remove inclusion of dev-tools in mocha tests

* move to modules, but break mocha

* fix: run mocha as a module

* fix: lint
2022-03-04 13:10:03 -08:00

64 lines
2.7 KiB
JavaScript

/**
* @license
* Copyright 2019 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.module('Blockly.test.names');
const {sharedTestSetup, sharedTestTeardown} = goog.require('Blockly.test.helpers.setupTeardown');
suite('Names', function() {
setup(function() {
sharedTestSetup.call(this);
});
teardown(function() {
sharedTestTeardown.call(this);
});
test('Safe name', function() {
const varDB = new Blockly.Names('window,door');
chai.assert.equal(varDB.safeName_(''), 'unnamed', 'SafeName empty.');
chai.assert.equal(varDB.safeName_('foobar'), 'foobar', 'SafeName ok.');
chai.assert.equal(varDB.safeName_('9lives'), 'my_9lives', 'SafeName number start.');
chai.assert.equal(varDB.safeName_('lives9'), 'lives9', 'SafeName number end.');
chai.assert.equal(varDB.safeName_('!@#$'), '____', 'SafeName special chars.');
chai.assert.equal(varDB.safeName_('door'), 'door', 'SafeName reserved.');
});
test('Get name', function() {
const varDB = new Blockly.Names('window,door');
chai.assert.equal(varDB.getName('Foo.bar', 'var'), 'Foo_bar', 'Name add #1.');
chai.assert.equal(varDB.getName('Foo.bar', 'var'), 'Foo_bar', 'Name get #1.');
chai.assert.equal(varDB.getName('Foo bar', 'var'), 'Foo_bar2', 'Name add #2.');
chai.assert.equal(varDB.getName('foo BAR', 'var'), 'Foo_bar2', 'Name get #2.');
chai.assert.equal(varDB.getName('door', 'var'), 'door2', 'Name add #3.');
chai.assert.equal(varDB.getName('Foo.bar', 'proc'), 'Foo_bar3', 'Name add #4.');
chai.assert.equal(varDB.getName('Foo.bar', 'var'), 'Foo_bar', 'Name get #1b.');
chai.assert.equal(varDB.getName('Foo.bar', 'proc'), 'Foo_bar3', 'Name get #4.');
chai.assert.equal(String(varDB.getUserNames('var')), 'foo.bar,foo bar,door', 'Get var names.');
chai.assert.equal(String(varDB.getUserNames('proc')), 'foo.bar', 'Get proc names.');
});
test('Get distinct name', function() {
const varDB = new Blockly.Names('window,door');
chai.assert.equal(varDB.getDistinctName('Foo.bar', 'var'), 'Foo_bar',
'Name distinct #1.');
chai.assert.equal(varDB.getDistinctName('Foo.bar', 'var'), 'Foo_bar2',
'Name distinct #2.');
chai.assert.equal(varDB.getDistinctName('Foo.bar', 'proc'), 'Foo_bar3',
'Name distinct #3.');
varDB.reset();
chai.assert.equal(varDB.getDistinctName('Foo.bar', 'var'), 'Foo_bar',
'Name distinct #4.');
});
test('name equals', function() {
chai.assert.isTrue(Blockly.Names.equals('Foo.bar', 'Foo.bar'), 'Name equals #1.');
chai.assert.isFalse(Blockly.Names.equals('Foo.bar', 'Foo_bar'), 'Name equals #2.');
chai.assert.isTrue(Blockly.Names.equals('Foo.bar', 'FOO.BAR'), 'Name equals #3.');
});
});