79 lines
1.8 KiB
TypeScript
79 lines
1.8 KiB
TypeScript
import test from 'ava';
|
|
import { TypeValidation } from '../lib/Types/TypeValidation';
|
|
|
|
test ('string', async (t) => {
|
|
const validator = new TypeValidation ('string');
|
|
const res = await validator.to_type ('foo');
|
|
t.is (res, 'foo');
|
|
});
|
|
|
|
test ('no number', (t) => {
|
|
const validator = new TypeValidation ('number');
|
|
t.throws (
|
|
() => validator.to_type ('foo'),
|
|
{ message: 'value is not a number' }
|
|
);
|
|
});
|
|
|
|
test ('number', async (t) => {
|
|
const validator = new TypeValidation ('number');
|
|
const res = await validator.to_type ('123');
|
|
t.is (res, 123);
|
|
});
|
|
|
|
test ('no boolean', (t) => {
|
|
const validator = new TypeValidation ('boolean');
|
|
t.throws (
|
|
() => validator.to_type ('foo'),
|
|
{ message: 'value is not a boolean' }
|
|
);
|
|
});
|
|
|
|
test ('boolean', async (t) => {
|
|
const validator = new TypeValidation ('boolean');
|
|
const r1 = await validator.to_type ('false');
|
|
const r2 = await validator.to_type ('true');
|
|
t.is (r1, false);
|
|
t.is (r2, true);
|
|
});
|
|
|
|
test ('boolean number', async (t) => {
|
|
const validator = new TypeValidation ('boolean');
|
|
const r1 = await validator.to_type (0);
|
|
const r2 = await validator.to_type (1);
|
|
t.is (r1, false);
|
|
t.is (r2, true);
|
|
});
|
|
|
|
test ('no array', (t) => {
|
|
const validator = new TypeValidation ('array');
|
|
t.throws (
|
|
() => validator.to_type (1),
|
|
{ message: 'value is not an array' }
|
|
);
|
|
});
|
|
|
|
test ('array', async (t) => {
|
|
const validator = new TypeValidation ('array');
|
|
const res = await validator.to_type ([
|
|
'foo',
|
|
'bar',
|
|
'baz'
|
|
]);
|
|
t.deepEqual (res, [
|
|
'foo',
|
|
'bar',
|
|
'baz'
|
|
]);
|
|
});
|
|
|
|
test ('string array', async (t) => {
|
|
const validator = new TypeValidation ('array');
|
|
const res = await validator.to_type ('f o o,bar , baz');
|
|
t.deepEqual (res, [
|
|
'f o o',
|
|
'bar',
|
|
'baz'
|
|
]);
|
|
});
|