Skip to content

Commit 059f3f3

Browse files
committed
test: add compile's functional test
1 parent eaea481 commit 059f3f3

File tree

7 files changed

+52
-30
lines changed

7 files changed

+52
-30
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 & 7 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,9 +34,4 @@ function cli() {
3334
process.stdout.write(`${output}\n`);
3435
}
3536

36-
try {
37-
cli();
38-
} catch (error) {
39-
process.stderr.write(`${error.message}\n`);
40-
process.exit(1);
41-
}
37+
graceful(cli());

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

@@ -59,13 +59,12 @@ export function run(file) {
5959
export function compile(file) {
6060
try {
6161
const contents = fs.readFileSync(file, { encoding: 'utf-8' });
62-
6362
let filename = file.split('.');
6463
filename = filename.filter((element, index) => index < filename.length - 1);
6564
filename = filename.join('.');
66-
6765
fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
6866
process.stdout.write(`${SUCCESS.compileSuccess}`);
67+
return true;
6968
} catch (error) {
7069
throw new Error(ERROR.fileNotFound);
7170
}

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';
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
import { test, expect, describe, is } from 'vitest';
1+
import fs from 'node:fs';
2+
import { test, expect, describe } from 'vitest';
23

34
import {
5+
FILE_LOCATION,
6+
NUMERIC_INPUT,
47
ANALLIFY_INPUT,
58
STRINGIFY_INPUT,
69
RUN_WRONG_OUTPUT,
710
ANAL_FILE_LOCATION,
11+
EMPTY_STRING_INPUT,
812
RUN_CORRECT_OUTPUT,
913
ANALLIFY_WRONG_OUTPUT,
1014
STRINGIFY_WRONG_OUTPUT,
15+
COMPILE_CORRECT_OUTPUT,
16+
COMPILED_FILE_LOCATION,
1117
ANALLIFY_CORRECT_OUTPUT,
1218
STRINGIFY_CORRECT_OUTPUT,
1319
} from './seeds.js';
@@ -17,7 +23,7 @@ import {
1723
anallify,
1824
stringify,
1925
compile,
20-
} from '../lib/stdlib.js';
26+
} from '../lib/std.js';
2127
import { ERROR } from '../lib/dictionary.js';
2228

2329
describe('Anallify', () => {
@@ -31,7 +37,9 @@ describe('Anallify', () => {
3137
});
3238

3339
test('Throw error if argument is missing', () => {
34-
expect(() => anallify('')).toThrowError(Error(ERROR.missingArgument));
40+
expect(() => anallify(EMPTY_STRING_INPUT)).toThrowError(
41+
Error(ERROR.missingArgument),
42+
);
3543
});
3644
});
3745

@@ -41,23 +49,16 @@ describe('Stringify', () => {
4149
expect(stringify(STRINGIFY_INPUT)).not.toBe(STRINGIFY_WRONG_OUTPUT);
4250
});
4351

44-
test('Decode with correct chars', () => {
45-
const ANAL_CHARACTERS = '';
46-
const charCodeA = ANAL_CHARACTERS.length;
47-
const charCodeB = ANAL_CHARACTERS.length * 2;
48-
49-
const anal = `${ANAL_CHARACTERS.repeat(charCodeA)} ${ANAL_CHARACTERS.repeat(charCodeB)}`;
50-
51-
const result = stringify(anal);
52-
expect(result).toBe(String.fromCharCode(charCodeA) + String.fromCharCode(charCodeB));
53-
});
54-
5552
test('Throw error if argument is not a string', () => {
56-
expect(() => stringify(1)).toThrowError(Error(ERROR.notString));
53+
expect(() => stringify(NUMERIC_INPUT)).toThrowError(
54+
Error(ERROR.notString),
55+
);
5756
});
5857

5958
test('Throw error if argument is missing', () => {
60-
expect(() => stringify('')).toThrowError(Error(ERROR.missingArgument));
59+
expect(() => stringify(EMPTY_STRING_INPUT)).toThrowError(
60+
Error(ERROR.missingArgument),
61+
);
6162
});
6263
});
6364

@@ -68,12 +69,21 @@ describe('Run', () => {
6869
});
6970

7071
test('Throw error if file is not found', () => {
71-
expect(() => run('')).toThrowError(Error(ERROR.fileNotFound));
72+
expect(() => run(EMPTY_STRING_INPUT)).toThrowError(
73+
Error(ERROR.fileNotFound),
74+
);
7275
});
7376
});
7477

7578
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+
7684
test('Throw error if file is not found', () => {
77-
expect(() => compile('')).toThrowError(Error(ERROR.fileNotFound));
85+
expect(() => compile(EMPTY_STRING_INPUT)).toThrowError(
86+
Error(ERROR.fileNotFound),
87+
);
7888
});
7989
});

0 commit comments

Comments
 (0)