2021-07-10 22:32:47 +00:00
|
|
|
/* eslint-disable no-magic-numbers */
|
2020-03-29 13:43:52 +00:00
|
|
|
import { expect } from 'chai';
|
2021-07-25 00:41:28 +00:00
|
|
|
import { integer, nat, string, tuple } from 'fast-check';
|
|
|
|
import { over } from 'mocha-foam';
|
2020-03-29 13:43:52 +00:00
|
|
|
|
2020-03-31 13:29:47 +00:00
|
|
|
import { leftPad, trim } from '../../src/String';
|
2020-03-29 13:43:52 +00:00
|
|
|
|
|
|
|
const TEST_SHORT = 'hello';
|
|
|
|
const TEST_LONG = 'hello world';
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
describe('left pad helper', async () => {
|
|
|
|
it('should prepend padding', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
expect(leftPad('test')).to.equal('0000test');
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should return long strings as-is', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
const long = 'testing-words';
|
|
|
|
expect(leftPad(long, 8)).to.equal(long);
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should use padding string', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
expect(leftPad('test', 8, 'too')).to.equal('toottest', 'must repeat and truncate the padding string');
|
|
|
|
});
|
2021-07-25 00:41:28 +00:00
|
|
|
|
|
|
|
over('short strings', integer({ min: 1, max: 100 }).chain((n) => tuple(integer({ min: n + 1, max: n + 10 }), string({ maxLength: n }))), (it) => {
|
|
|
|
it('should prefix the string with padding', ([len, str]) => {
|
|
|
|
expect(leftPad(str, len, '0')).to.match(/^0+/);
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 13:43:52 +00:00
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
describe('trim helper', async () => {
|
|
|
|
it('should return strings shorter than max', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
expect(trim('yes', 5)).to.equal('yes', 'shorter than max');
|
|
|
|
expect(trim(TEST_SHORT, 5)).to.equal(TEST_SHORT, 'equal to max');
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should trim strings longer than max', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
expect(trim(TEST_LONG, 3, '...')).to.equal('...');
|
|
|
|
expect(trim(TEST_LONG, 5)).to.equal('he...');
|
|
|
|
expect(trim(TEST_LONG, 8)).to.equal('hello...');
|
|
|
|
});
|
|
|
|
|
2020-06-30 13:14:30 +00:00
|
|
|
it('should not add tail when max is small', async () => {
|
2020-03-29 13:43:52 +00:00
|
|
|
expect(trim(TEST_SHORT, 2, '...')).to.equal('he');
|
|
|
|
expect(trim(TEST_LONG, 5, 'very long tail')).to.equal(TEST_SHORT);
|
|
|
|
expect(trim(TEST_SHORT, 8, 'very long tail')).to.equal(TEST_SHORT);
|
|
|
|
});
|
2021-07-25 00:41:28 +00:00
|
|
|
|
|
|
|
over('short strings', nat(100).chain((n) => tuple(integer({ min: n }), string({ maxLength: n }))), (it) => {
|
|
|
|
it('should return the whole string', ([len, str]) => {
|
|
|
|
expect(trim(str, len)).to.have.lengthOf(str.length);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
over('long strings', nat(100).chain((n) => tuple(nat(n), string({ minLength: n }))), (it) => {
|
|
|
|
it('should trim the string', ([len, str]) => {
|
|
|
|
expect(trim(str, len)).to.have.lengthOf(len);
|
|
|
|
});
|
|
|
|
});
|
2020-03-29 13:43:52 +00:00
|
|
|
});
|