smartpath/test/test.ts

67 lines
2.6 KiB
TypeScript
Raw Normal View History

2024-04-12 12:59:49 +00:00
import { tap, expect, expectAsync } from '@push.rocks/tapbundle';
2022-03-14 12:26:48 +00:00
import * as smartpath from '../ts/index.js';
2016-03-12 08:36:30 +00:00
2018-07-21 21:26:11 +00:00
let mySmartpath: smartpath.Smartpath;
tap.test('expect create a valid instance', async () => {
mySmartpath = new smartpath.Smartpath('/some/path/to/some.file');
2022-03-14 12:26:48 +00:00
expect(mySmartpath).toBeInstanceOf(smartpath.Smartpath);
expect(mySmartpath.pathLevelsBackwards.length === 5).toBeTrue();
2018-07-21 21:26:11 +00:00
});
2016-03-12 08:36:30 +00:00
2018-07-21 21:26:11 +00:00
let filePathString = './somedir/somefile.json';
let dirPathString = './somedir/anotherdir';
let dirPathString2 = './somedir/another.dir/';
2017-02-19 02:29:54 +00:00
2018-07-21 21:26:11 +00:00
tap.test('expect be true for a file path', async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.check.isFile(filePathString)).toBeTrue();
2018-07-21 21:26:11 +00:00
});
tap.test('expect be false for a directory path', async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.check.isFile(dirPathString)).toBeFalse();
expect(smartpath.check.isFile(dirPathString2)).toBeFalse();
2018-07-21 21:26:11 +00:00
});
2017-02-19 02:29:54 +00:00
2018-07-21 21:26:11 +00:00
tap.test('expect be true for a directory path', async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.check.isDir(dirPathString)).toBeTrue();
2017-02-19 02:29:54 +00:00
2022-03-14 12:26:48 +00:00
expect(smartpath.check.isDir(dirPathString2)).toBeTrue();
2018-07-21 21:26:11 +00:00
});
2017-02-19 02:29:54 +00:00
2018-07-21 21:26:11 +00:00
tap.test('expect be false for a file path', async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.check.isDir(filePathString)).toBeFalse();
2018-07-21 21:26:11 +00:00
});
let baseString = '/basedir';
let relativeString = 'somedir/somefile.txt';
let relativeString2 = 'anotherdir/anotherfile.txt';
let relativeArray = [relativeString, relativeString, relativeString2];
tap.test('expect make a string absolute', async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.transform.toAbsolute(relativeString)).toStartWith('/');
expect(smartpath.transform.toAbsolute(relativeString)).toEndWith(relativeString);
expect(smartpath.transform.toAbsolute(relativeString, baseString)).toEqual(
2018-07-21 21:26:11 +00:00
'/basedir/somedir/somefile.txt'
);
});
tap.test('expect make an array of relative Strings an Array of absolute Strings', async () => {
let absoluteArray = smartpath.transform.toAbsolute(relativeArray, baseString);
2022-03-14 12:26:48 +00:00
expect(absoluteArray[2]).toStartWith('/');
expect(absoluteArray[2]).toEndWith(relativeString2);
2018-07-21 21:26:11 +00:00
});
tap.test("expect return 'url' for an URL", async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.get.type('https://push.rocks/some/url')).toEqual('url');
expect(smartpath.get.type('https://push.rocks/some/url')).not.toEqual('local');
2018-07-21 21:26:11 +00:00
});
tap.test("expect return 'path' for a Path", async () => {
2022-03-14 12:26:48 +00:00
expect(smartpath.get.type('/some/absolute/path/')).toEqual('local');
expect(smartpath.get.type('./some/relative/path/')).not.toEqual('url');
2018-07-21 21:26:11 +00:00
});
tap.test('expect a absolute path for an home relative URL', async () => {
console.log(smartpath.get.home('~/test'));
});
tap.test('expect return the home directory path when no argument is specified', async () => {
console.log(smartpath.get.home());
});
tap.start();