array type

This commit is contained in:
2020-05-09 21:30:37 +02:00
parent 469afeb777
commit 7a13de1d03
11 changed files with 138 additions and 56 deletions

View File

@ -6,6 +6,7 @@ test ('string', async (t) => {
const res = await validator.to_type ('foo');
t.is (res, 'foo');
});
test ('no number', (t) => {
const validator = new TypeValidation ('number');
t.throws (
@ -13,11 +14,13 @@ test ('no number', (t) => {
{ 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 (
@ -25,6 +28,7 @@ test ('no boolean', (t) => {
{ message: 'value is not a boolean' }
);
});
test ('boolean', async (t) => {
const validator = new TypeValidation ('boolean');
const r1 = await validator.to_type ('false');
@ -32,6 +36,7 @@ test ('boolean', async (t) => {
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);
@ -39,3 +44,35 @@ test ('boolean number', async (t) => {
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'
]);
});