Files
blockly/tests/mocha/names_test.js
dependabot[bot] bfb5b1dd49 chore(deps): Bump chai from 4.3.10 to 5.1.1 (#8092)
* chore(deps): Bump chai from 4.3.10 to 5.1.1

  Bumps [chai](https://github.com/chaijs/chai) from 4.3.10 to 5.1.1.
  - [Release notes](https://github.com/chaijs/chai/releases)
  - [Changelog](https://github.com/chaijs/chai/blob/main/History.md)
  - [Commits](https://github.com/chaijs/chai/compare/v4.3.10...v5.1.1)

  ---
  updated-dependencies:
  - dependency-name: chai
    dependency-type: direct:development
    update-type: version-update:semver-major
  ...

  Signed-off-by: dependabot[bot] <support@github.com>

* fix(tests): Migrate all usage of chai to ESM (#8216)

* fix(tests): Migrate node tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by that package dropping suppport for CJS in chai v5.0.0.

* fix(tests): Have mocha tests directly import chai

  Previously they relied on obtaining it from the global scope, but it's
  better if imports are explicit.

* fix(tests): Remove broken load of chai as script

  Chai v5.0.0 no longer supports being loaded as a script, so this did
  nothing but emit an syntax error message on the console.

* fix(tests): Migrate browser tests from CJS to ESM

  This allows us to import (rather than require) chai, fixing failures
  caused by chai no longer supporting CJS.

* chore(tests): format

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Christopher Allen <cpcallen+git@google.com>
2024-06-17 16:48:21 +01:00

98 lines
2.8 KiB
JavaScript

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