Skip to content

Commit

Permalink
Add remaining tests; 100% coverage again
Browse files Browse the repository at this point in the history
  • Loading branch information
steeelydan committed Aug 27, 2024
1 parent e30036d commit 964e170
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { expect } from 'chai';
import { Js2EelCompiler } from '../../../compiler/Js2EelCompiler.js';

describe('onBlock()', () => {
it('Error if called elsewhere than root scope', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'onBlock', inChannels: 2, outChannels: 2 });
onSample(function callback() {
onBlock(() => {});
});
`);
expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('ScopeError');
});

it('can only be called once', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'onBlock', inChannels: 2, outChannels: 2 });
onBlock(() => {});
onBlock(() => {});
`);
expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('StageError');
});

it('Error if no callback given', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'onBlock', inChannels: 2, outChannels: 2 });
onBlock();
`);
expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(2);
expect(result.errors[0].type).to.equal('ArgumentError');
expect(result.errors[1].type).to.equal('ArgumentError');
});

it('Error if callback has args', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'onBlock', inChannels: 2, outChannels: 2 });
onBlock((sample) => {});
`);
expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('ParameterError');
});

it('Error if callback body is not a block statement', () => {
const compiler = new Js2EelCompiler();

const result =
compiler.compile(`config({ description: 'onBlock', inChannels: 2, outChannels: 2 });
onBlock(() => "nope");
`);
expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('TypeError');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ dim = 2;
);
});

it("Error if call to unknown member", () => {
it('Error if call to unknown member', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'eelBufferMemberCall', inChannels: 2, outChannels: 2 });
Expand Down Expand Up @@ -72,6 +72,184 @@ myBuf__size = 3;
dim = ;
`)
);
});

it('Error if swap called without arguments', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'swap - no args', inChannels: 2, outChannels: 2 });
const myBuf1 = new EelBuffer(1, 300);
onBlock(() => {
myBuf1.swap();
});
`);

expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(2);
expect(result.errors[0].type).to.equal('ArgumentError');
expect(result.errors[1].type).to.equal('ArgumentError');
expect(result.warnings.length).to.equal(0);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:swap - no args
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@init
myBuf1__B0 = 0 * 300 + 0;
myBuf1__size = 300;
@block
?ä__DENY_COMPILATION;
`)
);
});

it('Error if swap called with argument that is no buffer / no buffer found', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'swap - wrong arg', inChannels: 2, outChannels: 2 });
const myBuf1 = new EelBuffer(1, 300);
const myBuf2 = "Hello";
onBlock(() => {
myBuf1.swap(myBuf2);
});
`);

expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('TypeError');
expect(result.warnings.length).to.equal(0);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:swap - wrong arg
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@init
myBuf2 = Hello;
myBuf1__B0 = 0 * 300 + 0;
myBuf1__size = 300;
@block
?ä__DENY_COMPILATION;
`)
);
});

it('Error if swap with buffer of different dimensions', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'swap - dimensions', inChannels: 2, outChannels: 2 });
const myBuf1 = new EelBuffer(1, 300);
const myBuf2 = new EelBuffer(2, 300);
onBlock(() => {
myBuf1.swap(myBuf2);
});
`);

expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('BoundaryError');
expect(result.warnings.length).to.equal(0);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:swap - dimensions
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@init
myBuf1__B0 = 0 * 300 + 0;
myBuf1__size = 300;
myBuf2__B0 = 0 * 300 + 300;
myBuf2__B1 = 1 * 300 + 300;
myBuf2__size = 300;
@block
?ä__DENY_COMPILATION;
`)
);
});

it('Error if swap with buffer of different size', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'swap - size', inChannels: 2, outChannels: 2 });
const myBuf1 = new EelBuffer(1, 300);
const myBuf2 = new EelBuffer(1, 500);
onBlock(() => {
myBuf1.swap(myBuf2);
});
`);

expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('BoundaryError');
expect(result.warnings.length).to.equal(0);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:swap - size
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@init
myBuf1__B0 = 0 * 300 + 0;
myBuf1__size = 300;
myBuf2__B0 = 0 * 500 + 300;
myBuf2__size = 500;
@block
?ä__DENY_COMPILATION;
`)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,17 +223,7 @@ export const memberExpressionComputed = (
break;
}
case 'BinaryExpression': {
if (potentialBuffer) {
positionText += binaryExpression(property, instance);
} else {
instance.error(
'TypeError',
`Property access on array is not allowed with this type: ${property.type}`,
memberExpression.property
);

positionText += JSFX_DENY_COMPILATION;
}
positionText += binaryExpression(property, instance);

break;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { expect } from 'chai';
import { Js2EelCompiler } from '../../compiler/Js2EelCompiler.js';
import { testEelSrc } from '../../test/helpers.js';

describe('updateExpression()', () => {
it('decrements', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'update expression', inChannels: 2, outChannels: 2 });
let number = 1;
onBlock(() => {
number--;
});
`);

expect(result.success).to.equal(true);
expect(result.errors.length).to.equal(0);
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:update expression
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@init
number = 1;
@block
number -= 1;
`)
);
});

it('does not work with wrong type', () => {
const compiler = new Js2EelCompiler();
const result =
compiler.compile(`config({ description: 'update expression', inChannels: 2, outChannels: 2 });
onBlock(() => {
console.log++;
});
`);

expect(result.success).to.equal(false);
expect(result.errors.length).to.equal(1);
expect(result.errors[0].type).to.equal('TypeError');
expect(testEelSrc(result.src)).to.equal(
testEelSrc(`/* Compiled with JS2EEL vTO_BE_REPLACED_COMPILER_VERSION */
desc:update expression
in_pin:In 0
in_pin:In 1
out_pin:Out 0
out_pin:Out 1
@block
?ä__DENY_COMPILATION;
`)
);
});
});
Loading

0 comments on commit 964e170

Please sign in to comment.