Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analscript.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { anallify, stringify } from './lib/stdlib.js';
import { anallify, stringify } from './lib/std.js';

export { anallify, stringify };
export default { anallify, stringify };
5 changes: 3 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#!/usr/bin/env node
import help from './lib/help.js';
import { graceful } from './lib/utils.js';

import {
run,
compile,
anallify,
stringify,
} from './lib/stdlib.js';
} from './lib/std.js';

import {
RUN,
Expand All @@ -33,4 +34,4 @@ function cli() {
process.stdout.write(`${output}\n`);
}

cli();
graceful(cli);
23 changes: 8 additions & 15 deletions lib/stdlib.js → lib/std.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import fs from 'node:fs';

import checker from './utils.js';
import { checker } from './utils.js';
import { ERROR, SUCCESS } from './dictionary.js';
import { ANAL_CHARACTERS } from './constants.js';

export function anallify(string) {
if (!checker(string)) {
process.stderr.write(`${ERROR.notString}\n`);
return process.exit(1);
throw new Error(ERROR.notString);
}

if (!string) {
process.stderr.write(`${ERROR.missingArgument}\n`);
return process.exit(1);
throw new Error(ERROR.missingArgument);
}

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

export function stringify(anal) {
if (!checker(anal)) {
process.stderr.write(`${ERROR.notString}\n`);
return process.exit(1);
throw new Error(ERROR.notString);
}

if (!anal) {
process.stderr.write(`${ERROR.missingArgument}\n`);
return process.exit(1);
throw new Error(ERROR.missingArgument);
}

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

export function compile(file) {
try {
const contents = fs.readFileSync(file, { encoding: 'utf-8' });

let filename = file.split('.');
filename = filename.filter((element, index) => index < filename.length - 1);
filename = filename.join('.');

fs.writeFileSync(`${filename}.anal`, anallify(contents), { encoding: 'utf-8' });
process.stdout.write(`${SUCCESS.compileSuccess}`);
return true;
} catch (error) {
process.stderr.write(`${ERROR.fileNotFound}\n`);
process.exit(1);
throw new Error(ERROR.fileNotFound);
}
}
11 changes: 10 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export default function checker(input) {
export function checker(input) {
return typeof input === 'string';
}

export function graceful(fn) {
try {
fn();
} catch (error) {
process.stderr.write(`${error.message}\n`);
process.exit(1);
}
}
1 change: 1 addition & 0 deletions tests/hello.anus
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, Analscript!
7 changes: 7 additions & 0 deletions tests/seeds.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const NUMERIC_INPUT = 1;
export const EMPTY_STRING_INPUT = '';

export const STRINGIFY_CORRECT_OUTPUT = 'B';
export const STRINGIFY_WRONG_OUTPUT = '🍑🍆🍑🍆';
export const STRINGIFY_INPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆';
Expand All @@ -9,3 +12,7 @@ export const ANALLIFY_CORRECT_OUTPUT = '🍑🍆🍑🍆🍑🍆🍑🍆🍑🍆
export const ANAL_FILE_LOCATION = 'tests/script.anal';
export const RUN_CORRECT_OUTPUT = 'Welcome to Analscript!';
export const RUN_WRONG_OUTPUT = 'Welcome to Jurassic Park!';

export const COMPILE_CORRECT_OUTPUT = true;
export const FILE_LOCATION = 'tests/hello.anus';
export const COMPILED_FILE_LOCATION = 'tests/hello.anal';
89 changes: 89 additions & 0 deletions tests/std.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import fs from 'node:fs';
import { test, expect, describe } from 'vitest';

import {
FILE_LOCATION,
NUMERIC_INPUT,
ANALLIFY_INPUT,
STRINGIFY_INPUT,
RUN_WRONG_OUTPUT,
ANAL_FILE_LOCATION,
EMPTY_STRING_INPUT,
RUN_CORRECT_OUTPUT,
ANALLIFY_WRONG_OUTPUT,
STRINGIFY_WRONG_OUTPUT,
COMPILE_CORRECT_OUTPUT,
COMPILED_FILE_LOCATION,
ANALLIFY_CORRECT_OUTPUT,
STRINGIFY_CORRECT_OUTPUT,
} from './seeds.js';

import {
run,
anallify,
stringify,
compile,
} from '../lib/std.js';
import { ERROR } from '../lib/dictionary.js';

describe('Anallify', () => {
test('Encode string to anal', () => {
expect(anallify(ANALLIFY_INPUT)).toBe(ANALLIFY_CORRECT_OUTPUT);
expect(anallify(ANALLIFY_INPUT)).not.toBe(ANALLIFY_WRONG_OUTPUT);
});

test('Throw error if argument is not a string', () => {
expect(() => anallify(1)).toThrowError(Error(ERROR.notString));
});

test('Throw error if argument is missing', () => {
expect(() => anallify(EMPTY_STRING_INPUT)).toThrowError(
Error(ERROR.missingArgument),
);
});
});

describe('Stringify', () => {
test('Decode anal to string', () => {
expect(stringify(STRINGIFY_INPUT)).toBe(STRINGIFY_CORRECT_OUTPUT);
expect(stringify(STRINGIFY_INPUT)).not.toBe(STRINGIFY_WRONG_OUTPUT);
});

test('Throw error if argument is not a string', () => {
expect(() => stringify(NUMERIC_INPUT)).toThrowError(
Error(ERROR.notString),
);
});

test('Throw error if argument is missing', () => {
expect(() => stringify(EMPTY_STRING_INPUT)).toThrowError(
Error(ERROR.missingArgument),
);
});
});

describe('Run', () => {
test('Run .anal file', () => {
expect(run(ANAL_FILE_LOCATION)).toBe(RUN_CORRECT_OUTPUT);
expect(run(ANAL_FILE_LOCATION)).not.toBe(RUN_WRONG_OUTPUT);
});

test('Throw error if file is not found', () => {
expect(() => run(EMPTY_STRING_INPUT)).toThrowError(
Error(ERROR.fileNotFound),
);
});
});

describe('Compile', () => {
test('Compile file to .anal', () => {
expect(compile(FILE_LOCATION)).toBe(COMPILE_CORRECT_OUTPUT);
fs.rmSync(COMPILED_FILE_LOCATION);
});

test('Throw error if file is not found', () => {
expect(() => compile(EMPTY_STRING_INPUT)).toThrowError(
Error(ERROR.fileNotFound),
);
});
});
34 changes: 0 additions & 34 deletions tests/stdlib.test.js

This file was deleted.

16 changes: 16 additions & 0 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
vi,
test,
expect,
describe,
} from 'vitest';

import { graceful } from '../lib/utils';

describe('Graceful', () => {
test('Execute graceful', () => {
const fn = vi.fn();
graceful(fn);
expect(fn).toBeCalled();
});
});