avocado-old/packages/behavior/item/condition.spec.js

110 lines
3.0 KiB
JavaScript
Raw Normal View History

2019-03-17 23:45:48 -05:00
import {expect} from 'chai';
import {Condition} from './condition';
import {Literal} from './literal';
import {deregister, register} from './registry';
describe('behavior', () => {
describe('condition', () => {
it('can do binary compares', () => {
const condition = new Condition();
condition.setOperand(0, new Literal().fromJSON({
type: 'literal',
value: 500,
}));
condition.setOperand(1, new Literal().fromJSON({
type: 'literal',
value: 420,
}));
// Greater than.
condition.setOperator('>');
expect(condition.check()).to.be.true;
// Less than.
condition.setOperator('<');
expect(condition.check()).to.be.false;
// Greater than or equal.
condition.setOperator('>=');
expect(condition.check()).to.be.true;
// Less than or equal.
condition.setOperator('<=');
expect(condition.check()).to.be.false;
// Is.
condition.setOperator('is');
expect(condition.check()).to.be.false;
// Is not.
condition.setOperator('isnt');
expect(condition.check()).to.be.true;
});
it('can do varnary compares', () => {
const condition = new Condition();
condition.setOperand(0, new Literal().fromJSON({
type: 'literal',
value: true,
}));
condition.setOperand(1, new Literal().fromJSON({
type: 'literal',
value: true,
}));
condition.setOperand(2, new Literal().fromJSON({
type: 'literal',
value: false,
}));
// AND, mixed.
condition.setOperator('and');
expect(condition.check()).to.be.false;
// OR, mixed.
condition.setOperator('or');
expect(condition.check()).to.be.true;
// OR, all true.
condition.setOperand(2, new Literal().fromJSON({
type: 'literal',
value: true,
}));
expect(condition.check()).to.be.true;
// AND, all true.
condition.setOperator('and');
expect(condition.check()).to.be.true;
// AND, all false.
for (const i of [0, 1, 2]) {
condition.setOperand(i, new Literal().fromJSON({
type: 'literal',
value: false,
}));
}
expect(condition.check()).to.be.false;
// OR, all false.
condition.setOperator('or');
expect(condition.check()).to.be.false;
});
describe('JSON', () => {
beforeEach(() => {
register(Literal);
});
afterEach(() => {
deregister(Literal);
});
it('can instantiate operands', () => {
const condition = new Condition();
// Greater than.
condition.fromJSON({
operator: '>',
operands: [
{
type: 'literal',
value: 500,
},
{
type: 'literal',
value: 420,
},
],
});
expect(condition.check()).to.be.true;
// Less than.
condition.setOperator('<');
expect(condition.check()).to.be.false;
});
});
});
});