scratch-vm/test/unit/util_string.js
Christopher Willis-Ford eb931fd99b Specify dataFormat when loading asset for import
When importing a project we know the file extension for each asset to be
loaded. This change provides that information to the storage system so
that we can load assets which don't use the default. For example, this
allows loading JPG-format backdrops.

In support of this change, there's a new function on `StringUtil` called
`splitFirst`, which splits a string on the first instance of a separator
character. This change includes unit tests for this new function.
2017-05-24 15:35:10 -07:00

65 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const test = require('tap').test;
const StringUtil = require('../../src/util/string-util');
test('splitFirst', t => {
t.deepEqual(StringUtil.splitFirst('asdf.1234', '.'), ['asdf', '1234']);
t.deepEqual(StringUtil.splitFirst('asdf.', '.'), ['asdf', '']);
t.deepEqual(StringUtil.splitFirst('.1234', '.'), ['', '1234']);
t.deepEqual(StringUtil.splitFirst('foo', '.'), ['foo', null]);
t.end();
});
test('withoutTrailingDigits', t => {
t.strictEqual(StringUtil.withoutTrailingDigits('boeing747'), 'boeing');
t.strictEqual(StringUtil.withoutTrailingDigits('boeing747 '), 'boeing747 ');
t.strictEqual(StringUtil.withoutTrailingDigits('boeing𝟨'), 'boeing𝟨');
t.strictEqual(StringUtil.withoutTrailingDigits('boeing 747'), 'boeing ');
t.strictEqual(StringUtil.withoutTrailingDigits('747'), '');
t.end();
});
test('unusedName', t => {
t.strictEqual(
StringUtil.unusedName(
'name',
['not the same name']
),
'name'
);
t.strictEqual(
StringUtil.unusedName(
'name',
['name']
),
'name2'
);
t.strictEqual(
StringUtil.unusedName(
'name',
['name30']
),
'name'
);
t.strictEqual(
StringUtil.unusedName(
'name',
['name', 'name2']
),
'name3'
);
t.strictEqual(
StringUtil.unusedName(
'name',
['name', 'name3']
),
'name2'
);
t.strictEqual(
StringUtil.unusedName(
'boeing747',
['boeing747']
),
'boeing2' // Yup, this matches scratch-flash...
);
t.end();
});