1
0
ergogen/test/unit/anchor.js

186 lines
5.2 KiB
JavaScript
Raw Permalink Normal View History

2021-07-18 07:03:45 -07:00
const {parse} = require('../../src/anchor')
2021-05-22 08:58:26 -07:00
const Point = require('../../src/point')
const {check} = require('../helpers/point')
describe('Anchor', function() {
const points = {
o: new Point(0, 0, 0, {label: 'o'}),
2022-02-27 02:11:45 -08:00
ten: new Point(10, 10, -90, {label: 'ten'}),
mirror_ten: new Point(-10, 10, 90, {mirrored: true})
2021-05-22 08:58:26 -07:00
}
it('params', function() {
// an empty anchor definition leads to the default point
check(
2021-07-18 07:03:45 -07:00
parse({}, 'name')(),
2021-05-22 08:58:26 -07:00
[0, 0, 0, {}]
)
2022-02-27 02:11:45 -08:00
// single reference
2021-05-22 08:58:26 -07:00
check(
2022-02-27 02:11:45 -08:00
parse({ref: 'o'}, 'name', points)(),
[0, 0, 0, {label: 'o'}]
2021-05-22 08:58:26 -07:00
)
// default point can be overridden
check(
2022-02-27 02:11:45 -08:00
parse({}, 'name', {}, new Point(1, 1))(),
2021-05-22 08:58:26 -07:00
[1, 1, 0, {}]
)
2022-02-27 02:11:45 -08:00
// mirrored references can be forced
check(
parse({ref: 'ten'}, 'name', points, undefined, true)(),
[-10, 10, 90, {mirrored: true}]
)
2021-05-22 08:58:26 -07:00
})
2022-02-27 02:11:45 -08:00
it('recursive', function() {
// recursive references are supported (keeping metadata)
2021-05-22 08:58:26 -07:00
check(
2022-02-27 02:11:45 -08:00
parse({
ref: {
ref: 'o',
shift: [2, 2]
}
}, 'name', points)(),
[2, 2, 0, {label: 'o'}]
2021-05-22 08:58:26 -07:00
)
2022-02-27 02:11:45 -08:00
})
it('aggregate', function() {
2021-05-22 08:58:26 -07:00
// average of multiple references (metadata gets ignored)
check(
2022-02-27 02:11:45 -08:00
parse({
aggregate: {
parts: ['o', 'ten']
}
}, 'name', points)(),
[5, 5, -45, {}]
2021-05-22 08:58:26 -07:00
)
// empty parts
check(
parse({
aggregate: {
}
}, 'name', points)(),
[0, 0, 0, {}]
)
// can't have aggregate and ref together
parse({
aggregate: {
parts: ['o', 'ten']
},
ref : 'ten'
}, 'name', points).should.throw()
2021-05-22 08:58:26 -07:00
})
it('shift', function() {
// normal shift
check(
2021-07-18 07:03:45 -07:00
parse({shift: [1, 1]}, 'name')(),
2021-05-22 08:58:26 -07:00
[1, 1, 0, {}]
)
// shift should respect mirrored points (and invert along the x axis)
check(
2022-02-27 02:11:45 -08:00
parse({ref: 'mirror_ten', shift: [1, 1]}, 'name', points)(),
[-11, 9, 90, {mirrored: true}]
2021-05-22 08:58:26 -07:00
)
})
it('orient', function() {
// an orient by itself is equal to rotation
check(
2021-07-18 07:03:45 -07:00
parse({orient: 10}, 'name')(),
2021-05-22 08:58:26 -07:00
[0, 0, 10, {}]
)
// orient acts before shifting
// so when we orient to the right, an upward shift goes to the right
check(
2021-07-18 07:03:45 -07:00
parse({orient: -90, shift: [0, 1]}, 'name')(),
2021-05-22 08:58:26 -07:00
[1, 0, -90, {}]
)
// orient towards another point (and then move a diagonal to get to [1, 1])
check(
parse({orient: 'ten', shift: [0, Math.SQRT2]}, 'name', points)(),
[1, 1, -45, {}]
)
2021-05-22 08:58:26 -07:00
})
it('rotate', function() {
// basic rotation
check(
2021-07-18 07:03:45 -07:00
parse({rotate: 10}, 'name')(),
2021-05-22 08:58:26 -07:00
[0, 0, 10, {}]
)
// rotate acts *after* shifting
// so even tho we rotate to the right, an upward shift does go upward
check(
2021-07-18 07:03:45 -07:00
parse({shift: [0, 1], rotate: -90}, 'name')(),
2021-05-22 08:58:26 -07:00
[0, 1, -90, {}]
)
// rotate towards another point
check(
parse({rotate: {shift: [-1, -1]}}, 'name')(),
[0, 0, 135, {}]
)
2021-05-22 08:58:26 -07:00
})
it('affect', function() {
// affect can restrict which point fields (x, y, r) are affected by the transformations
check(
2021-07-18 07:03:45 -07:00
parse({orient: -90, shift: [0, 1], rotate: 10, affect: 'r'}, 'name')(),
2021-05-22 08:58:26 -07:00
[0, 0, -80, {}]
)
check(
2021-07-18 07:03:45 -07:00
parse({orient: -90, shift: [0, 1], rotate: 10, affect: 'xy'}, 'name')(),
2021-05-22 08:58:26 -07:00
[1, 0, 0, {}]
)
// affects can also be arrays (example same as above)
check(
2021-07-18 07:03:45 -07:00
parse({orient: -90, shift: [0, 1], rotate: 10, affect: ['x', 'y']}, 'name')(),
2021-05-22 08:58:26 -07:00
[1, 0, 0, {}]
)
})
it('resist', function() {
const p = new Point(0, 0, 0, {mirrored: true}) // origin, but mirrored
// resistance should be correctly propagated for shifts
check(
parse({shift: [1, 1]}, 'name', {}, p)(),
[-1, 1, 0, {mirrored: true}]
)
check(
parse({shift: [1, 1], resist: true}, 'name', {}, p)(),
[1, 1, 0, {mirrored: true}]
)
// ...and orients/rotations too
check(
parse({rotate: 10}, 'name', {}, p)(),
[0, 0, -10, {mirrored: true}]
)
check(
parse({rotate: 10, resist: true}, 'name', {}, p)(),
[0, 0, 10, {mirrored: true}]
)
})
2022-02-27 02:11:45 -08:00
it('string', function() {
// basic string form
check(
parse('ten', 'name', points)(),
[10, 10, -90, {label: 'ten'}]
)
})
2021-05-22 08:58:26 -07:00
it('array', function() {
// basic multi-anchor
check(
2021-07-18 07:03:45 -07:00
parse([
2021-05-22 08:58:26 -07:00
{shift: [1, 1]},
{rotate: 10}
], 'name')(),
[1, 1, 10, {}]
)
})
})