Files
lodash/test/toPath.js
Benjamin Tan d5ef31929a Add initial test files from lodash v4. (#4172)
* Install test dependencies.

* Add initial test files.

These files were created using a simplistic AST manipulator using `recast` to
preserve formatting. There's bound to be a huge chunk of errors, but this serves
as a good start. QUnit was replaced with Mocha, with ES2015 imports running via
`esm`.

As far as possible, QUnit-specific syntax has been replaced with Mocha's
`describe` and `it`, while the native Node.js `assert` module is used for
assertions. Files in the `test` directory ending in `.test.js` will be treated
as test files.

* Add initial passing files to test run.
2019-02-12 09:11:32 -08:00

67 lines
2.2 KiB
JavaScript

import assert from 'assert';
import lodashStable from 'lodash';
import { symbol } from './utils.js';
import toPath from '../toPath.js';
describe('toPath', function() {
it('should convert a string to a path', function() {
assert.deepStrictEqual(toPath('a.b.c'), ['a', 'b', 'c']);
assert.deepStrictEqual(toPath('a[0].b.c'), ['a', '0', 'b', 'c']);
});
it('should coerce array elements to strings', function() {
var array = ['a', 'b', 'c'];
lodashStable.each([array, lodashStable.map(array, Object)], function(value) {
var actual = toPath(value);
assert.deepStrictEqual(actual, array);
assert.notStrictEqual(actual, array);
});
});
it('should return new path array', function() {
assert.notStrictEqual(toPath('a.b.c'), toPath('a.b.c'));
});
it('should not coerce symbols to strings', function() {
if (Symbol) {
var object = Object(symbol);
lodashStable.each([symbol, object, [symbol], [object]], function(value) {
var actual = toPath(value);
assert.ok(lodashStable.isSymbol(actual[0]));
});
}
});
it('should handle complex paths', function() {
var actual = toPath('a[-1.23]["[\\"b\\"]"].c[\'[\\\'d\\\']\'][\ne\n][f].g');
assert.deepStrictEqual(actual, ['a', '-1.23', '["b"]', 'c', "['d']", '\ne\n', 'f', 'g']);
});
it('should handle consecutive empty brackets and dots', function() {
var expected = ['', 'a'];
assert.deepStrictEqual(toPath('.a'), expected);
assert.deepStrictEqual(toPath('[].a'), expected);
expected = ['', '', 'a'];
assert.deepStrictEqual(toPath('..a'), expected);
assert.deepStrictEqual(toPath('[][].a'), expected);
expected = ['a', '', 'b'];
assert.deepStrictEqual(toPath('a..b'), expected);
assert.deepStrictEqual(toPath('a[].b'), expected);
expected = ['a', '', '', 'b'];
assert.deepStrictEqual(toPath('a...b'), expected);
assert.deepStrictEqual(toPath('a[][].b'), expected);
expected = ['a', ''];
assert.deepStrictEqual(toPath('a.'), expected);
assert.deepStrictEqual(toPath('a[]'), expected);
expected = ['a', '', ''];
assert.deepStrictEqual(toPath('a..'), expected);
assert.deepStrictEqual(toPath('a[][]'), expected);
});
});