use jasmine
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-10-03 15:14:14 +02:00
parent b81b77b924
commit d9de76b188
25 changed files with 1332 additions and 1272 deletions

View File

@ -1,46 +0,0 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import test from 'ava';
import { PathType } from '../lib/TypeValidation/PathType';
test ('no file', async (t) => {
const validator = new PathType ('file');
await t.throwsAsync (
() => validator.to_type ('test'),
{ message: 'cannot assign folder to file' }
);
});
test ('file', async (t) => {
const validator = new PathType ('file');
const res = await validator.to_type ('package.json');
t.is (res, 'package.json');
});
test ('no folder', async (t) => {
const validator = new PathType ('folder');
await t.throwsAsync (
() => validator.to_type ('package.json'),
{ message: 'cannot assign folder to file' }
);
});
test ('folder', async (t) => {
const validator = new PathType ('folder');
const res = await validator.to_type ('test');
t.is (res, 'test');
});
test ('no path', async (t) => {
const validator = new PathType ('path');
await t.throwsAsync (
() => validator.to_type ('doesnotexist.file'),
{ message: 'path does not exist' }
);
});
test ('path', async (t) => {
const validator = new PathType ('path');
const res = await validator.to_type ('test');
t.is (res, 'test');
});

View File

@ -1,91 +0,0 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, May 2020
*/
import test from 'ava';
import { TypeValidation } from '../lib/TypeValidation/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.4');
t.is (res, 123.4);
});
test ('int', async (t) => {
const validator = new TypeValidation ('int');
const res = await validator.to_type ('123.4');
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'
]);
});

46
test/spec/paths.ts Normal file
View File

@ -0,0 +1,46 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, October 2020
*/
import { PathType } from '../../lib/TypeValidation/PathType';
describe ('paths', () => {
it ('no file', async () => {
const validator = new PathType ('file');
await expectAsync (
validator.to_type ('test')
)
.toBeRejectedWithError ('cannot assign folder to file');
});
it ('file', async () => {
const validator = new PathType ('file');
const res = await validator.to_type ('package.json');
expect (res)
.toEqual ('package.json');
});
it ('no folder', async () => {
const validator = new PathType ('folder');
await expectAsync (validator.to_type ('package.json'))
.toBeRejectedWithError ('cannot assign folder to file');
});
it ('folder', async () => {
const validator = new PathType ('folder');
const res = await validator.to_type ('test');
expect (res)
.toEqual ('test');
});
it ('no path', async () => {
const validator = new PathType ('path');
await expectAsync (validator.to_type ('doesnotexist.file'))
.toBeRejectedWithError ('path does not exist');
});
it ('path', async () => {
const validator = new PathType ('path');
const res = await validator.to_type ('test');
expect (res)
.toEqual ('test');
});
});

102
test/spec/types.ts Normal file
View File

@ -0,0 +1,102 @@
/*
* Copyright (C) Sapphirecode - All Rights Reserved
* This file is part of console-app which is released under MIT.
* See file 'LICENSE' for full license details.
* Created by Timo Hocker <timo@scode.ovh>, October 2020
*/
import { TypeValidation } from '../../lib/TypeValidation/TypeValidation';
// eslint-disable-next-line max-lines-per-function
describe ('type validation', () => {
it ('string', async () => {
const validator = new TypeValidation ('string');
const res = await validator.to_type ('foo');
expect (res)
.toEqual ('foo');
});
it ('no number', () => {
const validator = new TypeValidation ('number');
expect (
() => validator.to_type ('foo')
)
.toThrowError ('value is not a number');
});
it ('number', async () => {
const validator = new TypeValidation ('number');
const res = await validator.to_type ('123.4');
expect (res)
.toEqual (123.4);
});
it ('int', async () => {
const validator = new TypeValidation ('int');
const res = await validator.to_type ('123.4');
expect (res)
.toEqual (123);
});
it ('no boolean', () => {
const validator = new TypeValidation ('boolean');
expect (
() => validator.to_type ('foo')
)
.toThrowError ('value is not a boolean');
});
it ('boolean', async () => {
const validator = new TypeValidation ('boolean');
const r1 = await validator.to_type ('false');
const r2 = await validator.to_type ('true');
expect (r1)
.toEqual (false);
expect (r2)
.toEqual (true);
});
it ('boolean number', async () => {
const validator = new TypeValidation ('boolean');
const r1 = await validator.to_type (0);
const r2 = await validator.to_type (1);
expect (r1)
.toEqual (false);
expect (r2)
.toEqual (true);
});
it ('no array', () => {
const validator = new TypeValidation ('array');
expect (
() => validator.to_type (1)
)
.toThrowError ('value is not an array');
});
it ('array', async () => {
const validator = new TypeValidation ('array');
const res = await validator.to_type ([
'foo',
'bar',
'baz'
]);
expect (res)
.toEqual ([
'foo',
'bar',
'baz'
]);
});
it ('string array', async () => {
const validator = new TypeValidation ('array');
const res = await validator.to_type ('f o o,bar , baz');
expect (res)
.toEqual ([
'f o o',
'bar',
'baz'
]);
});
});