Skip to content

Commit

Permalink
test: added test cases for left functions
Browse files Browse the repository at this point in the history
  • Loading branch information
USERSATOSHI committed Oct 21, 2024
1 parent 142bbd5 commit aafd2be
Show file tree
Hide file tree
Showing 9 changed files with 390 additions and 2 deletions.
82 changes: 82 additions & 0 deletions lib/aoi.js/src/functions/js/math/$abs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $abs } from './$abs.js';

const client = new TestClient();
client.transpiler.addFunctions({ $abs });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToFail = '$abs';
const codeToPass = '$abs[2000]';
const codeWithPositive = '$abs[200]';
const codeWithNegative = '$abs[-200]';


void describe('$abs', () => {
void it('should not compile successfully without arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFail, transpilerOptions);
});
});

void it('should compile successfully with arg', () => {
const func = client.transpiler.transpile(codeToPass, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});

void it('should return 200 for 200', async () => {
// logs true
const orignalLog = console.log;
let logged: Record<string, string> = { content: 'hi' };

console.log = (log: Record<string, string>) => {
logged = log;
// orignalLog(log);
};

const { func } = client.transpiler.transpile(codeWithPositive, transpilerOptions);

// @ts-expect-error: func is a function
await func?.();

console.log = orignalLog;

assert.strictEqual(logged.content.toString(), '200');
});

void it('should return 200 for -200', async () => {
// logs false
const orignalLog = console.log;
let logged: Record<string, string> = { content: 'hi' };

console.log = (log: Record<string, string>) => {
logged = log;
// orignalLog(log);
};

const { func } = client.transpiler.transpile(codeWithNegative, transpilerOptions);

// @ts-expect-error: func is a function
await func?.();

console.log = orignalLog;

assert.strictEqual(logged.content.toString(), -200);
});
});
91 changes: 91 additions & 0 deletions lib/aoi.js/src/functions/js/math/$math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $math } from './$math.js';

const client = new TestClient();
client.transpiler.addFunctions({ $math });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToFail = '$math';
const codeToFailWithArg = '$math[hi+bye]';
const codeToPass = '$math[2000+1]';
const codewithBasicMath = '$math[2000/2+2-2*2]';
const codeWithAdvMath = '$math[pow(sin(90), 2) + pow(cos(90), 2)]';


void describe('$math', () => {
void it('should not compile successfully without arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFail, transpilerOptions);
});
});

void it('should not compile successfully with arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFailWithArg, transpilerOptions);
});
});

void it('should compile successfully with arg', () => {
const func = client.transpiler.transpile(codeToPass, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});

void it('should return 998 for 2000/2+2-2*2', async () => {
// logs true
const orignalLog = console.log;
let logged: Record<string, string> = { content: 'hi' };

console.log = (log: Record<string, string>) => {
logged = log;
// orignalLog(log);
};

const { func } = client.transpiler.transpile(codewithBasicMath, transpilerOptions);

// @ts-expect-error: func is a function
await func?.();

console.log = orignalLog;

assert.strictEqual(logged.content.toString(), '998');
});

void it('should return 1 for pow(sin(90), 2) + pow(cos(90), 2)', async () => {
// logs false
const orignalLog = console.log;
let logged: Record<string, string> = { content: 'hi' };

console.log = (log: Record<string, string>) => {
logged = log;
// orignalLog(log);
};

const { func } = client.transpiler.transpile(codeWithAdvMath, transpilerOptions);

// @ts-expect-error: func is a function
await func?.();

console.log = orignalLog;

assert.strictEqual(logged.content.toString(), '1');
});
});
49 changes: 49 additions & 0 deletions lib/aoi.js/src/functions/js/misc/$let.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $let } from './$let.js';

const client = new TestClient();
client.transpiler.addFunctions({ $let });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToFail = '$let';
const codeToFailWithArg = '$let[hi+bye]';
const codeToPass = '$let[hi;bye]';


void describe('$let', () => {
void it('should not compile successfully without arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFail, transpilerOptions);
});
});

void it('should not compile successfully with arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFailWithArg, transpilerOptions);
});
});

void it('should compile successfully with arg', () => {
const func = client.transpiler.transpile(codeToPass, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});
});
48 changes: 48 additions & 0 deletions lib/aoi.js/src/functions/js/process/$cpu.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $cpu } from './$cpu.js';

const client = new TestClient();
client.transpiler.addFunctions({ $cpu });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToPassWithoutArg = '$cpu';
const codeToFailWithArg = '$cpu[hi]';
const codeToPass = '$cpu[os]';


void describe('$cpu', () => {
void it('should compile successfully without arg', () => {

const func = client.transpiler.transpile(codeToPassWithoutArg, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});

void it('should not compile successfully with arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFailWithArg, transpilerOptions);
});
});

void it('should compile successfully with arg', () => {
const func = client.transpiler.transpile(codeToPass, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});
});
32 changes: 32 additions & 0 deletions lib/aoi.js/src/functions/js/process/$cwd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $cwd } from './$cwd.js';

const client = new TestClient();
client.transpiler.addFunctions({ $cwd });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToPassWithoutArg = '$cwd';


void describe('$cwd', () => {
void it('should compile successfully without arg', () => {

const func = client.transpiler.transpile(codeToPassWithoutArg, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});
});
40 changes: 40 additions & 0 deletions lib/aoi.js/src/functions/js/process/$procenv.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $procenv } from './$procenv.js';

const client = new TestClient();
client.transpiler.addFunctions({ $procenv });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToPassWithoutArg = '$procenv';
const codeToPassWithArg = '$procenv[hi]';


void describe('$procenv', () => {
void it('should compile successfully without arg', () => {

const func = client.transpiler.transpile(codeToPassWithoutArg, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});

void it('should compile successfully with arg', () => {

const func = client.transpiler.transpile(codeToPassWithArg, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});
});
47 changes: 47 additions & 0 deletions lib/aoi.js/src/functions/js/process/$ram.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { describe, it } from 'node:test';
import assert from 'node:assert';

import TestClient from '@aoi.js/testing/testClient.js';
import { $ram } from './$ram.js';

const client = new TestClient();
client.transpiler.addFunctions({ $ram });

const transpilerOptions = {
scopeData: {
name: 'global',
vars: [],
embeds: [],
env: [],
object: {},
embeddedJS: [],
sendFunction: 'console.log',
},
};

const codeToPassWithoutArg = '$ram';
const codeToFail = '$ram[hi]';
const codeWithParam = '$ram[rss]';

void describe('$ram', () => {
void it('should compile successfully without arg', () => {

const func = client.transpiler.transpile(codeToPassWithoutArg, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});

void it('should not compile successfully with arg', () => {

// expect this to throw an error
assert.throws(() => {
client.transpiler.transpile(codeToFail, transpilerOptions);
});
});

void it('should compile successfully with arg', () => {
const func = client.transpiler.transpile(codeWithParam, transpilerOptions);
assert.ok(func);
assert.strictEqual(typeof func.func, 'function');
});
});
Loading

0 comments on commit aafd2be

Please sign in to comment.