mirror of https://github.com/lukechilds/ow.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
3.8 KiB
167 lines
3.8 KiB
7 years ago
|
import test from 'ava';
|
||
6 years ago
|
import ow from '../source';
|
||
7 years ago
|
|
||
|
test('array', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow([], ow.array);
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow([], 'foo', ow.array);
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow('12' as any, ow.array);
|
||
|
}, 'Expected argument to be of type `array` but received type `string`');
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow('12' as any, 'foo', ow.array);
|
||
|
}, 'Expected `foo` to be of type `array` but received type `string`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.length', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo'], ow.array.length(1));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.length(2));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo'], ow.array.length(2));
|
||
|
}, 'Expected array to have length `2`, got `1`');
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo'], 'foo', ow.array.length(2));
|
||
|
}, 'Expected array `foo` to have length `2`, got `1`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.minLength', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo'], ow.array.minLength(1));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.minLength(1));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo'], ow.array.minLength(2));
|
||
|
}, 'Expected array to have a minimum length of `2`, got `1`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.maxLength', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo'], ow.array.maxLength(1));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.maxLength(4));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'bar'], ow.array.maxLength(1));
|
||
|
}, 'Expected array to have a maximum length of `1`, got `2`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.startsWith', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.startsWith('foo'));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'bar'], ow.array.startsWith('bar'));
|
||
|
}, 'Expected array to start with `bar`, got `foo`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.endsWith', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.endsWith('bar'));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'bar'], ow.array.endsWith('foo'));
|
||
|
}, 'Expected array to end with `foo`, got `bar`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.includes', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.includes('foo'));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar', 'unicorn'], ow.array.includes('foo', 'bar'));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'bar'], ow.array.includes('foo', 'unicorn'));
|
||
|
}, 'Expected array to include all elements of `["foo","unicorn"]`, got `["foo","bar"]`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.includesAny', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.includesAny('foo'));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar', 'unicorn'], ow.array.includesAny('unicorn', 'rainbow'));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'bar'], ow.array.includesAny('unicorn'));
|
||
|
}, 'Expected array to include any element of `["unicorn"]`, got `["foo","bar"]`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.empty', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow([], ow.array.empty);
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo'], ow.array.empty);
|
||
|
}, 'Expected array to be empty, got `["foo"]`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.nonEmpty', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo'], ow.array.nonEmpty);
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow([], ow.array.nonEmpty);
|
||
|
}, 'Expected array to not be empty');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.deepEqual', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo'], ow.array.deepEqual(['foo']));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', {id: 1}], ow.array.deepEqual(['foo', {id: 1}]));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', {id: 1}], ow.array.deepEqual(['foo', {id: 2}]));
|
||
|
}, 'Expected array to be deeply equal to `["foo",{"id":2}]`, got `["foo",{"id":1}]`');
|
||
7 years ago
|
});
|
||
|
|
||
|
test('array.ofType', t => {
|
||
6 years ago
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.ofType(ow.string));
|
||
|
});
|
||
|
|
||
|
t.notThrows(() => {
|
||
|
ow(['foo', 'bar'], ow.array.ofType(ow.string.minLength(3)));
|
||
|
});
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'b'], ow.array.ofType(ow.string.minLength(3)));
|
||
|
}, '(array) Expected string to have a minimum length of `3`, got `b`');
|
||
|
|
||
|
t.throws(() => {
|
||
|
ow(['foo', 'b'], 'foo', ow.array.ofType(ow.string.minLength(3)));
|
||
|
}, '(array `foo`) Expected string to have a minimum length of `3`, got `b`');
|
||
7 years ago
|
});
|