Skip to content

Commit 7975065

Browse files
authored
test: add compile and graceful tests
2 parents 83a650c + 4255427 commit 7975065

File tree

9 files changed

+135
-53
lines changed

9 files changed

+135
-53
lines changed

analscript.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { anallify, stringify } from './lib/stdlib.js';
1+
import { anallify, stringify } from './lib/std.js';
22

33
export { anallify, stringify };
44
export default { anallify, stringify };

cli.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#!/usr/bin/env node
22
import help from './lib/help.js';
3+
import { graceful } from './lib/utils.js';
34

45
import {
56
run,
67
compile,
78
anallify,
89
stringify,
9-
} from './lib/stdlib.js';
10+
} from './lib/std.js';
1011

1112
import {
1213
RUN,
@@ -33,4 +34,4 @@ function cli() {
3334
process.stdout.write(`${output}\n`);
3435
}
3536

36-
cli();
37+
graceful(cli);

lib/stdlib.js renamed to lib/std.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
import fs from 'node:fs';
22

3-
import checker from './utils.js';
3+
import { checker } from './utils.js';
44
import { ERROR, SUCCESS } from './dictionary.js';
55
import { ANAL_CHARACTERS } from './constants.js';
66

77
export function anallify(string) {
88
if (!checker(string)) {
9-
process.stderr.write(`${ERROR.notString}\n`);
10-
return process.exit(1);
9+
throw new Error(ERROR.notString);
1110
}
1211

1312
if (!string) {
14-
process.stderr.write(`${ERROR.missingArgument}\n`);
15-
return process.exit(1);
13+
throw new Error(ERROR.missingArgument);
1614
}
1715

1816
let anal = '';
@@ -29,13 +27,11 @@ export function anallify(string) {
2927

3028
export function stringify(anal) {
3129
if (!checker(anal)) {
32-
process.stderr.write(`${ERROR.notString}\n`);
33-
return process.exit(1);
30+
throw new Error(ERROR.notString);
3431
}
3532

3633
if (!anal) {
37-
process.stderr.write(`${ERROR.missingArgument}\n`);
38-
return process.exit(1);
34+
throw new Error(ERROR.missingArgument);
3935
}
4036

4137
let string = '';
@@ -56,23 +52,20 @@ export function run(file) {
5652
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
5753
return stringify(contents);
5854
} catch (error) {
59-
process.stderr.write(`${ERROR.fileNotFound}\n`);
60-
return process.exit(1);
55+
throw new Error(ERROR.fileNotFound);
6156
}
6257
}
6358

6459
export function compile(file) {
6560
try {
6661
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
67-
6862
let filename = file.split('.');
6963
filename = filename.filter((element, index) => index < filename.length - 1);
7064
filename = filename.join('.');
71-
7265
fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
7366
process.stdout.write(`${SUCCESS.compileSuccess}`);
67+
return true;
7468
} catch (error) {
75-
process.stderr.write(`${ERROR.fileNotFound}\n`);
76-
process.exit(1);
69+
throw new Error(ERROR.fileNotFound);
7770
}
7871
}

lib/utils.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
export default function checker(input) {
1+
export function checker(input) {
22
return typeof input === 'string';
33
}
4+
5+
export function graceful(fn) {
6+
try {
7+
fn();
8+
} catch (error) {
9+
process.stderr.write(`${error.message}\n`);
10+
process.exit(1);
11+
}
12+
}

tests/hello.anus

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, Analscript!

tests/seeds.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
export const NUMERIC_INPUT = 1;
2+
export const EMPTY_STRING_INPUT = '';
3+
14
export const STRINGIFY_CORRECT_OUTPUT = 'B';
25
export const STRINGIFY_WRONG_OUTPUT = '🍑🍆🍑🍆';
36
export const STRINGIFY_INPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆';
@@ -9,3 +12,7 @@ export const ANALLIFY_CORRECT_OUTPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆
912
export const ANAL_FILE_LOCATION = 'tests/script.anal';
1013
export const RUN_CORRECT_OUTPUT = 'Welcome to Analscript!';
1114
export const RUN_WRONG_OUTPUT = 'Welcome to Jurassic Park!';
15+
16+
export const COMPILE_CORRECT_OUTPUT = true;
17+
export const FILE_LOCATION = 'tests/hello.anus';
18+
export const COMPILED_FILE_LOCATION = 'tests/hello.anal';

tests/std.test.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import fs from 'node:fs';
2+
import { test, expect, describe } from 'vitest';
3+
4+
import {
5+
FILE_LOCATION,
6+
NUMERIC_INPUT,
7+
ANALLIFY_INPUT,
8+
STRINGIFY_INPUT,
9+
RUN_WRONG_OUTPUT,
10+
ANAL_FILE_LOCATION,
11+
EMPTY_STRING_INPUT,
12+
RUN_CORRECT_OUTPUT,
13+
ANALLIFY_WRONG_OUTPUT,
14+
STRINGIFY_WRONG_OUTPUT,
15+
COMPILE_CORRECT_OUTPUT,
16+
COMPILED_FILE_LOCATION,
17+
ANALLIFY_CORRECT_OUTPUT,
18+
STRINGIFY_CORRECT_OUTPUT,
19+
} from './seeds.js';
20+
21+
import {
22+
run,
23+
anallify,
24+
stringify,
25+
compile,
26+
} from '../lib/std.js';
27+
import { ERROR } from '../lib/dictionary.js';
28+
29+
describe('Anallify', () => {
30+
test('Encode string to anal', () => {
31+
expect(anallify(ANALLIFY_INPUT)).toBe(ANALLIFY_CORRECT_OUTPUT);
32+
expect(anallify(ANALLIFY_INPUT)).not.toBe(ANALLIFY_WRONG_OUTPUT);
33+
});
34+
35+
test('Throw error if argument is not a string', () => {
36+
expect(() => anallify(1)).toThrowError(Error(ERROR.notString));
37+
});
38+
39+
test('Throw error if argument is missing', () => {
40+
expect(() => anallify(EMPTY_STRING_INPUT)).toThrowError(
41+
Error(ERROR.missingArgument),
42+
);
43+
});
44+
});
45+
46+
describe('Stringify', () => {
47+
test('Decode anal to string', () => {
48+
expect(stringify(STRINGIFY_INPUT)).toBe(STRINGIFY_CORRECT_OUTPUT);
49+
expect(stringify(STRINGIFY_INPUT)).not.toBe(STRINGIFY_WRONG_OUTPUT);
50+
});
51+
52+
test('Throw error if argument is not a string', () => {
53+
expect(() => stringify(NUMERIC_INPUT)).toThrowError(
54+
Error(ERROR.notString),
55+
);
56+
});
57+
58+
test('Throw error if argument is missing', () => {
59+
expect(() => stringify(EMPTY_STRING_INPUT)).toThrowError(
60+
Error(ERROR.missingArgument),
61+
);
62+
});
63+
});
64+
65+
describe('Run', () => {
66+
test('Run .anal file', () => {
67+
expect(run(ANAL_FILE_LOCATION)).toBe(RUN_CORRECT_OUTPUT);
68+
expect(run(ANAL_FILE_LOCATION)).not.toBe(RUN_WRONG_OUTPUT);
69+
});
70+
71+
test('Throw error if file is not found', () => {
72+
expect(() => run(EMPTY_STRING_INPUT)).toThrowError(
73+
Error(ERROR.fileNotFound),
74+
);
75+
});
76+
});
77+
78+
describe('Compile', () => {
79+
test('Compile file to .anal', () => {
80+
expect(compile(FILE_LOCATION)).toBe(COMPILE_CORRECT_OUTPUT);
81+
fs.rmSync(COMPILED_FILE_LOCATION);
82+
});
83+
84+
test('Throw error if file is not found', () => {
85+
expect(() => compile(EMPTY_STRING_INPUT)).toThrowError(
86+
Error(ERROR.fileNotFound),
87+
);
88+
});
89+
});

tests/stdlib.test.js

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/utils.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
vi,
3+
test,
4+
expect,
5+
describe,
6+
} from 'vitest';
7+
8+
import { graceful } from '../lib/utils';
9+
10+
describe('Graceful', () => {
11+
test('Execute graceful', () => {
12+
const fn = vi.fn();
13+
graceful(fn);
14+
expect(fn).toBeCalled();
15+
});
16+
});

0 commit comments

Comments
 (0)