Skip to content

Commit

Permalink
fix(operator): test for cachedValueOf
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed May 19, 2019
1 parent 1447267 commit 8180ccd
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { assert } from 'chai';
import {
victor, ipoint, Victor, vector, point, calc
} from '../src';
import { cachedValueOf, cachedFactory, operatorCalc } from '../src/operator';

describe('mixed 2D and 3D test.', () => {
it('fetches higher order operand automatically in calc', () => {
Expand Down Expand Up @@ -44,3 +45,32 @@ describe('mixed 2D and 3D test.', () => {
assert.equal(pos.y, 14);
});
});

class Tuple {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
}
cachedValueOf(Tuple);
const tupleFactory = cachedFactory(Tuple);
const tuple = (x, y, z) => {
if (typeof x === 'function') {
return operatorCalc(x, new Tuple());
}
return tupleFactory(x, y, z);
};

describe('override valueOf of a new class', () => {
it('alculations with the new class', () => {
const t1 = tuple(3, 4, 5);
const t2 = tuple(6, 7, 8);
const pos = tuple(() => t1 + t2 * 2);

assert.instanceOf(pos, Tuple);
assert.equal(pos.x, 15);
assert.equal(pos.y, 18);
assert.equal(pos.z, 21);
});
});

0 comments on commit 8180ccd

Please sign in to comment.