-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operator): cachedFactory function
- Loading branch information
Showing
2 changed files
with
30 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,43 @@ | ||
// @ts-nocheck | ||
import { assert } from 'chai'; | ||
import { operatorCalc } from '../src/operator'; | ||
import { Victor, Vector } from '../src'; | ||
import { operatorCalc, cachedFactory } from '../src/operator'; | ||
import { victor, vector } from '../src'; | ||
|
||
describe('operatorCalc with Victor test.', () => { | ||
it('should throw error with assgined Victor', () => { | ||
const pos = new Victor(5, 6, 7); | ||
const dir = new Victor(1, 0, 0); | ||
const pos = victor(5, 6, 7); | ||
const dir = victor(1, 0, 0); | ||
|
||
assert.throws(() => operatorCalc(() => dir * pos, new Victor(1, 0, 0)), Error); | ||
assert.throws(() => operatorCalc(() => dir * pos, victor(1, 0, 0)), Error); | ||
}); | ||
}); | ||
|
||
describe('operatorCalc with Vector test.', () => { | ||
it('should throw error with assgined Victor', () => { | ||
const pos = new Vector(5, 6, 7); | ||
const dir = new Vector(1, 0, 0); | ||
const pos = vector(5, 6, 7); | ||
const dir = vector(1, 0, 0); | ||
|
||
const scale = operatorCalc(() => dir * pos, new Vector(1, 0.9, 0)); | ||
const scale = operatorCalc(() => dir * pos, vector(1, 0.9, 0)); | ||
|
||
assert.equal(scale.x, 5); | ||
assert.equal(scale.y, 0); | ||
assert.equal(scale.z, 0); | ||
}); | ||
}); | ||
|
||
describe('operatorCalc with class using cachedFactory (Vector or Victor or Point or Ipoint) test.', () => { | ||
let counter = 0; | ||
class V { | ||
constructor() { | ||
counter++; | ||
} | ||
} | ||
const vFactory = cachedFactory(V); | ||
|
||
it('factory call should be cached', () => { | ||
const pos = vector(5, 6, 7); | ||
const dir = vector(1, 0, 0); | ||
operatorCalc(() => dir * vFactory() + 1); | ||
|
||
assert.equal(counter, 1); | ||
}); | ||
}); |