Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CordlessWool committed Feb 10, 2024
1 parent e69ab3c commit 0415e38
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 33 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cSpell.words": [
"YOTTABYTE",
"ZETTABYTE"
]
}
9 changes: 6 additions & 3 deletions src/core/editor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as fs from 'node:fs/promises';

import { faker } from '@faker-js/faker';
import { TextItemList } from './helper/textItemList';
import exp from 'node:constants';

function createEditor(testFile: string) {
const dir = new Directory(dirname(testFile));
Expand Down Expand Up @@ -129,14 +128,15 @@ describe('Editor', () => {
const result = await reader.searchFirst('co');
expect(result).toBeDefined();
let count = 1;
while(await result?.hasNext()) {
while(await result!.hasNext()) {

await result?.next();
await result!.next();
expect(result).toBeDefined();
count++;
}
//find 5 with small letter, search is case insensitive
expect(count).toBe(5);
expect(await result!.next()).toBeUndefined();
reader.close();
});

Expand Down Expand Up @@ -206,6 +206,7 @@ describe('Editor', () => {
count++;
}
expect(count).toBe(17);
expect(result?.prev()).resolves.toBeUndefined();
reader.close();
});

Expand Down Expand Up @@ -383,6 +384,7 @@ describe('Editor', () => {
}
expect(result.read('utf8')).resolves.toBe('### EOF');
expect(count).toBe(2);
expect(result.next()).resolves.toBeUndefined();
reader.close();
});

Expand Down Expand Up @@ -435,6 +437,7 @@ describe('Editor', () => {
}
}
expect(result.read('utf8')).resolves.toBe('---');
expect(result.prev()).resolves.toBeUndefined();
expect(count).toBe(2);
reader.close();
});
Expand Down
35 changes: 33 additions & 2 deletions src/core/file.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { LoomFile } from './file.js';
import { FileConvertException, PluginNotFoundException } from './exceptions.js';
import { TestFilesystemHelper } from '../../test/helpers/testFilesystemHelper.js';


import jsonConverter from '../plugins/jsonConverter.js';
import yamlConverter from '../plugins/yamlConverter.js';
import { basename, dirname } from 'node:path';
import { Directory } from './dir.js';
import { FILE_SIZE_UNIT } from './types.js';
import { faker } from '@faker-js/faker';
import { Editor } from './editor.js';

class FileTest extends LoomFile {

Expand Down Expand Up @@ -58,6 +60,28 @@ describe('Test File Service', () => {
expect(exists).toBeFalsy();
});

test('Get file size', async () => {
const helper = await TestFilesystemHelper.init();
const path = 'test/data/test.txt';
const content = faker.lorem.words(10000);
const testFilePath = (await helper.createFile(content, { path })).includeBasePath().getPath();
const file = LoomFile.from(testFilePath);
const bytes = await file.getSize(FILE_SIZE_UNIT.BYTE);
expect(bytes).toBe(content.length);
const megaBytes = await file.getSize(FILE_SIZE_UNIT.MEGABYTE);
expect(megaBytes).toBe(content.length / 1024 / 1024);
const yottaBytes = await file.getSize(FILE_SIZE_UNIT.YOTTABYTE);
expect(yottaBytes).toBe(content.length / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024 / 1024);
const gigaByte = await file.getSize(FILE_SIZE_UNIT.GIGABYTE);
expect(gigaByte).toBe(content.length / 1024 / 1024 / 1024);
});

test('If File exists on Object', async () => {
const file = LoomFile.from('./test/data/test.json');
const exists = await file.exists();
expect(exists).toBeTruthy();
});

test('Register plugins', async () => {
const plugins = FileTest.getConvertPlugins();
expect(plugins).toHaveLength(2);
Expand All @@ -69,8 +93,15 @@ describe('Test File Service', () => {
expect(file.dir.path).toBe(`${process.cwd()}/test/data`);
expect(file.dir).toBe(file.parent);
});


test('get reader object', async () => {
const file = LoomFile.from('./test/data/test.json');
const reader = await file.reader();
expect(reader).toBeDefined();
expect(reader).toBeInstanceOf(Editor);
reader.close();
});

describe('Test with generated file', () => {

beforeEach(async () => {
Expand Down
4 changes: 4 additions & 0 deletions src/core/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export class LoomFile {

}

async exists() {
return await LoomFile.exists(this.dir, this.name);
}

async reader() {
return await Editor.from(this);
}
Expand Down
20 changes: 2 additions & 18 deletions src/core/helper/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,6 @@ export class SearchResult implements BaseResult{
get meta(): TextMeta {
return this._listItem.content;
}

protected handleSearchResult(result: SearchResult | undefined): SearchResult | undefined {
if(result !== undefined) {
result._listItem.add(this._listItem);
}
return result;
}

protected generateInstance(item: TextItemList): SearchResult {
return new SearchResult(item, this._searchValue, this.reader);
}

async hasNext(): Promise<boolean> {
if(this._listItem.hasNext()) {
Expand All @@ -40,9 +29,7 @@ export class SearchResult implements BaseResult{

const fileSize = await this.reader.getSizeInBytes();
const nextItem = await this.reader.loopForward(this._searchValue, this._listItem.end, fileSize);
if(nextItem === undefined) {
return false;
} else if (nextItem.getLastItem().end === this._listItem.end) {
if(nextItem === undefined || nextItem.getLastItem().end === this._listItem.end) {
return false;
} else {
this._listItem.add(nextItem);
Expand All @@ -55,7 +42,6 @@ export class SearchResult implements BaseResult{
if(await this.hasNext()) {
this._listItem = this._listItem.next() as TextItemList;
return this;
//return this.generateInstance(this._listItem.next() as TextItemList);
} else {
return undefined;
}
Expand All @@ -66,9 +52,7 @@ export class SearchResult implements BaseResult{
return true;
}
const prevItem = await this.reader.loopReverse(this._searchValue, 0, this._listItem.start);
if(prevItem === undefined) {
return false;
} else if (prevItem.getFirstItem().start === this._listItem.start ) {
if(prevItem === undefined || prevItem.getFirstItem().end === this._listItem.start) {
return false;
} else {
this._listItem.add(prevItem);
Expand Down
10 changes: 0 additions & 10 deletions src/core/helper/textItemList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ export interface TextMeta {
readonly start: number,
readonly end: number,
readonly readReverse?: boolean
readonly first?: boolean,
readonly last?: boolean
}

export class TextItemList {
Expand Down Expand Up @@ -39,14 +37,6 @@ export class TextItemList {
return this.end - this.start;
}

get first() {
return !!this.content.first;
}

get last() {
return !!this.content.last;
}

isReverseRead() {
return !!this.content.readReverse;
}
Expand Down

0 comments on commit 0415e38

Please sign in to comment.