Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: add tests for every shape #13

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions test/arc.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
import { assert } from 'chai';
import { Vector3 } from 'three';
import { MeshLine } from '../src/MeshLine';
import { Geometry } from '../src/index';

describe('Arc', () => {
describe('default config', () => {
it('should set default config', () => {
const arc = new Geometry.Arc();

assert.equal(arc.radius, 1);
assert.equal(arc.angle, Math.PI / 2);
assert.isFalse(arc.closed);
assert.isUndefined(arc.fill);
});

it('should have default style set', () => {
const arc = new Geometry.Arc();
const defaults = Geometry.Arrow.defaultStyle();

assert.isUndefined(arc.fill);
assert.equal(arc.stroke?.material.color.getHexString(), defaults.strokeColor.getHexString());
assert.equal(arc.stroke?.material.opacity, defaults.strokeOpacity);
assert.equal(arc.stroke?.material.width, defaults.strokeWidth);
});
});

describe('attributes', () => {
it('should have a radius, angle, and closed attributes', () => {
const arc = new Geometry.Arc();
assert.containsAllKeys(arc.getAttributes(), ['radius', 'angle', 'closed']);
assert.deepEqual(arc.getAttributes(), { radius: 1, angle: Math.PI / 2, closed: false });
});

it('should correctly return clone attributes', () => {
const arc = new Geometry.Arc(10, 8, { closed: true });
assert.deepEqual(arc.getCloneAttributes(), [10, 8, true]);
});

it('should correctly return attribute data', () => {
const arc = new Geometry.Arc();
assert.deepEqual(arc.attributeData, [
{
attribute: 'radius',
type: 'number',
default: 1,
},
{
attribute: 'angle',
type: 'angle',
default: 45,
},
{
attribute: 'closed',
type: 'boolean',
default: false,
},
]);
});
});

describe('shape', () => {
it('closed arc should start and end with a (0, 0, 0) vector 3', () => {
const closedArc = new Geometry.Arc(10, 8, { closed: true });
const openArc = new Geometry.Arc(10, 8, { closed: false });

assert.equal(closedArc.points.length, openArc.points.length + 2);
assert.deepEqual(closedArc.points.at(0)?.toArray(), [0, 0, 0]);
assert.deepEqual(closedArc.points.at(-1)?.toArray(), [0, 0, 0]);
});

it('should set correct curveEndIndices on a closed arc', () => {
const arc = new Geometry.Arc(1, Math.PI / 2, { closed: true });
assert.deepEqual(arc.curveEndIndices, [
[0, 1],
[1, 51],
[51, 52],
]);
});

it('should set correct curveEndIndices on an arc', () => {
const arc = new Geometry.Arc(1, Math.PI / 2, { closed: false });
assert.deepEqual(arc.curveEndIndices, [[0, 50]]);
});

it('should have correct points with a 0 angle', () => {
const arc = new Geometry.Arc(1, 0);
assert.deepEqual(arc.points, [new Vector3(1, 0, 0), new Vector3(1, 0, 0)]);
});

it('should return the correct dimensions', () => {
const arc = new Geometry.Arc(1, 0);
assert.deepEqual(arc.getDimensions().toArray(), [2, 2]);
});

it('should have correct points', () => {
const arc = new Geometry.Arc(1, 1);

assert.deepEqual(arc.points, [
new Vector3(1, 0, 0),
new Vector3(0.9998000066665778, 0.01999866669333308, 0),
new Vector3(0.9992001066609779, 0.03998933418663416, 0),
new Vector3(0.9982005399352042, 0.059964006479444595, 0),
new Vector3(0.9968017063026194, 0.0799146939691727, 0),
new Vector3(0.9950041652780257, 0.09983341664682815, 0),
new Vector3(0.9928086358538663, 0.11971220728891938, 0),
new Vector3(0.9902159962126371, 0.1395431146442365, 0),
new Vector3(0.9872272833756269, 0.15931820661424598, 0),
new Vector3(0.9838436927881214, 0.17902957342582418, 0),
new Vector3(0.9800665778412416, 0.1986693307950612, 0),
new Vector3(0.9758974493306055, 0.2182296230808693, 0),
new Vector3(0.9713379748520297, 0.23770262642713455, 0),
new Vector3(0.9663899781345132, 0.25708055189215506, 0),
new Vector3(0.961055438310771, 0.2763556485641137, 0),
new Vector3(0.955336489125606, 0.29552020666133955, 0),
new Vector3(0.9492354180824408, 0.31456656061611776, 0),
new Vector3(0.9427546655283462, 0.3334870921408144, 0),
new Vector3(0.9358968236779348, 0.35227423327509, 0),
new Vector3(0.9286646355765102, 0.37092046941298273, 0),
new Vector3(0.9210609940028851, 0.3894183423086506, 0),
new Vector3(0.9130889403123083, 0.40776045305957026, 0),
new Vector3(0.9047516632199634, 0.4259394650659997, 0),
new Vector3(0.8960524975255252, 0.4439481069655199, 0),
new Vector3(0.8869949227792842, 0.461779175541483, 0),
new Vector3(0.8775825618903726, 0.4794255386042031, 0),
new Vector3(0.8678191796776499, 0.49688013784373686, 0),
new Vector3(0.857708681363824, 0.5141359916531132, 0),
new Vector3(0.8472551110134161, 0.5311861979208835, 0),
new Vector3(0.8364626499151868, 0.5480239367918737, 0),
new Vector3(0.8253356149096782, 0.5646424733950355, 0),
new Vector3(0.8138784566625338, 0.5810351605373052, 0),
new Vector3(0.8020957578842924, 0.5971954413623922, 0),
new Vector3(0.789992231497365, 0.613116851973434, 0),
new Vector3(0.7775727187509278, 0.6287930240184687, 0),
new Vector3(0.7648421872844883, 0.6442176872376912, 0),
new Vector3(0.7518057291408948, 0.6593846719714734, 0),
new Vector3(0.7384685587295876, 0.6742879116281453, 0),
new Vector3(0.7248360107409049, 0.6889214451105515, 0),
new Vector3(0.7109135380122771, 0.7032794192004104, 0),
new Vector3(0.6967067093471652, 0.717356090899523, 0),
new Vector3(0.6822212072876133, 0.7311458297268961, 0),
new Vector3(0.6674628258413078, 0.7446431199708596, 0),
new Vector3(0.6524374681640516, 0.7578425628952772, 0),
new Vector3(0.6371511441985799, 0.7707388788989695, 0),
new Vector3(0.6216099682706641, 0.7833269096274836, 0),
new Vector3(0.6058201566434624, 0.7956016200363664, 0),
new Vector3(0.5897880250310978, 0.8075581004051146, 0),
new Vector3(0.5735199860724562, 0.8191915683009986, 0),
new Vector3(0.5570225467662169, 0.8304973704919708, 0),
new Vector3(0.5403023058681393, 0.8414709848078967, 0),
]);
});
});

it('should inherit from shape', () => {
const arc = new Geometry.Arc();
assert.isUndefined(arc.fill);
assert.instanceOf(arc.stroke, MeshLine);
assert.isArray(arc.curveEndIndices);
});
});
71 changes: 71 additions & 0 deletions test/arrow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { assert } from 'chai';
import { MeshLine } from '../src/MeshLine';
import { Geometry, THREE } from '../src/index';

describe('Arrow', () => {
function getTestPoints(): [start: THREE.Vector3, end: THREE.Vector3] {
return [new THREE.Vector3(-1, -1, 0), new THREE.Vector3(1, 1, 0)];
}

describe('default config', () => {
it('should set arrow to true', () => {
const arrowOne = new Geometry.Arrow(...getTestPoints());
assert.isTrue(arrowOne.arrow);

// @ts-expect-error invalid config
const arrowTwo = new Geometry.Arrow(...getTestPoints(), { arrow: false });
assert.isTrue(arrowTwo.arrow);
});

it('should have default style set', () => {
const arrow = new Geometry.Arrow(...getTestPoints());
const defaults = Geometry.Arrow.defaultStyle();

assert.equal(arrow.fill?.material.color.getHexString(), defaults.fillColor.getHexString());
assert.equal(arrow.fill?.material.opacity, defaults.fillOpacity);
assert.isTrue(arrow.fill?.material.transparent);
assert.equal(
arrow.stroke?.material.color.getHexString(),
defaults.strokeColor.getHexString(),
);
assert.equal(arrow.stroke?.material.opacity, defaults.strokeOpacity);
assert.equal(arrow.stroke?.material.width, defaults.strokeWidth);
});
});

describe('attributes', () => {
it('should have a start and end attribute', () => {
const arrow = new Geometry.Arrow(...getTestPoints());
assert.containsAllKeys(arrow.getAttributes(), ['start', 'end']);
});
});

describe('shape', () => {
it('should set start and end correctly', () => {
const arrow = new Geometry.Arrow(...getTestPoints());

assert.deepEqual(arrow.start.toArray(), [-1, -1, 0]);
assert.deepEqual(arrow.end.toArray(), [1, 1, 0]);
});

it.skip('should correctly reshape', () => {
const arrow = new Geometry.Arrow(...getTestPoints());

arrow.reshape(new THREE.Vector3(-2, -2, 0), new THREE.Vector3(2, 2, 0), {
fillColor: new THREE.Color('#C458A5'),
});

assert.deepEqual(arrow.start.toArray(), [-2, -2, 0]);
assert.deepEqual(arrow.end.toArray(), [2, 2, 0]);
assert.equal(arrow.fill?.material.color.getHexString(), 'c458a5');
});
});

it('should inherit from shape', () => {
const arrow = new Geometry.Arrow(...getTestPoints());
assert.instanceOf(arrow.fill, THREE.Mesh);
assert.instanceOf(arrow.stroke, MeshLine);
assert.isArray(arrow.curveEndIndices);
assert.isBoolean(arrow.arrow);
});
});
141 changes: 141 additions & 0 deletions test/circle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { assert } from 'chai';
import { Vector3 } from 'three';
import { MeshLine } from '../src/MeshLine';
import { Geometry, THREE } from '../src/index';

describe('Circle', () => {
describe('default config', () => {
it('should set defaults', () => {
const circle = new Geometry.Circle();

assert.equal(circle.radius, 1);
assert.equal(circle.angle, Math.PI * 2);
assert.isFalse(circle.closed);
});

it('should have default style set', () => {
const circle = new Geometry.Circle();
const defaults = Geometry.Arrow.defaultStyle();

assert.equal(circle.fill?.material.color.getHexString(), defaults.fillColor.getHexString());
assert.equal(circle.fill?.material.opacity, defaults.fillOpacity);
assert.isTrue(circle.fill?.material.transparent);
assert.equal(
circle.stroke?.material.color.getHexString(),
defaults.strokeColor.getHexString(),
);
assert.equal(circle.stroke?.material.opacity, defaults.strokeOpacity);
assert.equal(circle.stroke?.material.width, defaults.strokeWidth);
});
});

describe('attributes', () => {
it('should have a radius, angle, and closed attributes', () => {
const circle = new Geometry.Circle(10);
assert.containsAllKeys(circle.getAttributes(), ['radius', 'angle', 'closed']);
assert.deepEqual(circle.getAttributes(), { radius: 10, angle: Math.PI * 2, closed: false });
});

it('should correctly return clone attributes', () => {
const circle = new Geometry.Circle(10);
assert.deepEqual(circle.getCloneAttributes(), [10]);
});

it('should correctly return attribute data', () => {
const circle = new Geometry.Circle();
assert.deepEqual(circle.attributeData, [
{
attribute: 'radius',
type: 'number',
default: 1,
},
]);
});
});

describe('shape', () => {
it('should set correct curveEndIndices on an circle', () => {
const circle = new Geometry.Circle(1);
assert.deepEqual(circle.curveEndIndices, [[0, 50]]);
});

it('should return the correct dimensions', () => {
const circle = new Geometry.Circle(1);
assert.deepEqual(circle.getDimensions().toArray(), [2, 2]);
});

it('should have correct points', () => {
const circle = new Geometry.Circle(1);

assert.deepEqual(circle.points, [
new Vector3(1, 0, 0),
new Vector3(0.9921147013144779, 0.12533323356430426, 0),
new Vector3(0.9685831611286311, 0.2486898871648548, 0),
new Vector3(0.9297764858882513, 0.368124552684678, 0),
new Vector3(0.8763066800438636, 0.4817536741017153, 0),
new Vector3(0.8090169943749475, 0.5877852522924731, 0),
new Vector3(0.7289686274214116, 0.6845471059286886, 0),
new Vector3(0.6374239897486899, 0.7705132427757891, 0),
new Vector3(0.5358267949789968, 0.844327925502015, 0),
new Vector3(0.4257792915650728, 0.9048270524660195, 0),
new Vector3(0.3090169943749477, 0.9510565162951535, 0),
new Vector3(0.18738131458572493, 0.9822872507286886, 0),
new Vector3(0.06279051952931375, 0.9980267284282716, 0),
new Vector3(-0.06279051952931296, 0.9980267284282716, 0),
new Vector3(-0.18738131458572418, 0.9822872507286887, 0),
new Vector3(-0.30901699437494695, 0.9510565162951538, 0),
new Vector3(-0.4257792915650723, 0.9048270524660197, 0),
new Vector3(-0.5358267949789964, 0.8443279255020152, 0),
new Vector3(-0.6374239897486897, 0.7705132427757893, 0),
new Vector3(-0.7289686274214117, 0.6845471059286885, 0),
new Vector3(-0.8090169943749477, 0.5877852522924728, 0),
new Vector3(-0.8763066800438638, 0.4817536741017148, 0),
new Vector3(-0.9297764858882517, 0.3681245526846773, 0),
new Vector3(-0.9685831611286313, 0.24868988716485396, 0),
new Vector3(-0.992114701314478, 0.1253332335643032, 0),
new Vector3(-1, -1.2098029496354525e-15, 0),
new Vector3(-0.9921147013144777, -0.12533323356430562, 0),
new Vector3(-0.9685831611286307, -0.2486898871648563, 0),
new Vector3(-0.9297764858882508, -0.3681245526846796, 0),
new Vector3(-0.8763066800438627, -0.48175367410171693, 0),
new Vector3(-0.8090169943749462, -0.5877852522924748, 0),
new Vector3(-0.72896862742141, -0.6845471059286903, 0),
new Vector3(-0.6374239897486882, -0.7705132427757905, 0),
new Vector3(-0.5358267949789948, -0.8443279255020163, 0),
new Vector3(-0.42577929156507055, -0.9048270524660205, 0),
new Vector3(-0.30901699437494506, -0.9510565162951543, 0),
new Vector3(-0.18738131458572202, -0.9822872507286892, 0),
new Vector3(-0.06279051952931054, -0.9980267284282718, 0),
new Vector3(0.06279051952931639, -0.9980267284282713, 0),
new Vector3(0.18738131458572777, -0.982287250728688, 0),
new Vector3(0.3090169943749506, -0.9510565162951525, 0),
new Vector3(0.4257792915650758, -0.904827052466018, 0),
new Vector3(0.5358267949789998, -0.8443279255020131, 0),
new Vector3(0.6374239897486927, -0.7705132427757867, 0),
new Vector3(0.7289686274214142, -0.6845471059286857, 0),
new Vector3(0.8090169943749499, -0.5877852522924697, 0),
new Vector3(0.8763066800438657, -0.48175367410171144, 0),
new Vector3(0.9297764858882531, -0.3681245526846737, 0),
new Vector3(0.9685831611286323, -0.2486898871648502, 0),
new Vector3(0.9921147013144784, -0.12533323356429937, 0),
new Vector3(1, 5.084141158371281e-15, 0),
]);
});
});

describe('inheritance', () => {
it('from shape', () => {
const circle = new Geometry.Circle();
assert.instanceOf(circle.fill, THREE.Mesh);
assert.instanceOf(circle.stroke, MeshLine);
assert.isArray(circle.curveEndIndices);
});

it('from arc', () => {
const circle = new Geometry.Circle();
assert.equal(circle.radius, 1);
assert.equal(circle.angle, Math.PI * 2);
assert.deepEqual(circle.curveEndIndices, [[0, 50]]);
});
});
});
Loading