1
0
ergogen/test/unit/filter.js

71 lines
4.0 KiB
JavaScript
Raw Permalink Normal View History

2022-01-08 14:51:03 -08:00
const filter = require('../../src/filter').parse
const anchor = require('../../src/anchor').parse
const Point = require('../../src/point')
describe('Filter', function() {
2023-02-09 14:50:18 -08:00
it('without points', function() {
filter(undefined, '').should.deep.equal([new Point()])
filter(true, '').should.deep.equal([])
filter(false, '').should.deep.equal([])
filter({}, '').should.deep.equal([anchor({}, '', points)()])
})
2022-01-08 14:51:03 -08:00
const points = {
one: new Point(0, 1, 0, {name: 'one', tags: ['odd']}),
two: new Point(0, 2, 0, {name: 'two', tags: ['even', 'prime']}),
2023-02-09 14:50:18 -08:00
three: new Point(0, 3, 0, {name: 'three', tags: {odd: 'yes', prime: 'yupp'}}),
mirror_one: new Point(0, 1, 0, {name: 'mirror_one', tags: ['odd'], mirrored: true})
2022-01-08 14:51:03 -08:00
}
2023-02-09 14:50:18 -08:00
it('empty filter', function() {
2022-01-16 11:36:19 -08:00
// an undefined config leads to a default point
filter(undefined, '', points).should.deep.equal([new Point()])
// true shouldn't filter anything, while false should filter everything
filter(true, '', points).should.deep.equal(Object.values(points))
filter(false, '', points).should.deep.equal([])
// points should only be returned on their respective halves
2023-02-09 14:50:18 -08:00
// - so `source` is every match
filter(true, '', points, undefined, 'source').should.deep.equal(Object.values(points))
2023-02-09 14:50:18 -08:00
// - `clone` is the mirror image of every match, which maps one to mirror_one, mirror_one to one, and two/three to nothing (as they don't have mirror parts)
filter(true, '', points, undefined, 'clone').should.deep.equal([points.mirror_one, points.one])
// - and `both` is every match plus its mirror image as well
filter(true, '', points, undefined, 'both').should.deep.equal(Object.values(points))
2022-01-08 14:51:03 -08:00
// objects just propagate to anchor (and then wrap in array for consistency)
filter({}, '', points).should.deep.equal([anchor({}, '', points)()])
filter({}, '', points, undefined, 'source').should.deep.equal([anchor({}, '', points)()])
filter({}, '', points, undefined, 'clone').should.deep.equal([anchor({}, '', points)()])
2023-02-09 14:50:18 -08:00
filter({}, '', points, undefined, 'both').should.deep.equal([anchor({}, '', points)()])
})
const names = points => points.map(p => p.meta.name)
it('similar', function() {
2022-01-08 14:51:03 -08:00
// simple name string
names(filter('one', '', points)).should.deep.equal(['one'])
// simple name regex
names(filter('/^t/', '', points)).should.deep.equal(['two', 'three'])
2023-02-09 14:50:18 -08:00
// tags should count, too (one and mirror_one for the name, three for the odd tag)
names(filter('/^o/', '', points)).should.deep.equal(['one', 'three', 'mirror_one'])
2022-01-08 14:51:03 -08:00
// middle spec, should be the same as above, only explicit
2023-02-09 14:50:18 -08:00
names(filter('~ /^o/', '', points)).should.deep.equal(['one', 'three', 'mirror_one'])
2023-03-18 08:23:05 -07:00
// full spec (/n/ would normally match both "one" and "even", but on the tags level, it's just even)
2022-01-08 14:51:03 -08:00
names(filter('meta.tags ~ /n/', '', points)).should.deep.equal(['two'])
2023-03-18 08:23:05 -07:00
names(filter('meta.name,meta.tags ~ /n/', '', points)).should.deep.equal(['one', 'two', 'mirror_one'])
2022-01-08 14:51:03 -08:00
// negation
2023-02-09 14:50:18 -08:00
names(filter('meta.tags ~ -/n/', '', points)).should.deep.equal(['one', 'three', 'mirror_one'])
2023-03-18 08:23:05 -07:00
names(filter('meta.name,meta.tags ~ -/n/', '', points)).should.deep.equal(['three'])
2023-02-09 14:50:18 -08:00
// arrays OR by default at odd levels (including top level)...
2022-01-08 14:51:03 -08:00
names(filter(['one', 'two'], '', points)).should.deep.equal(['one', 'two'])
// ...and AND at even levels
names(filter([['even', 'prime']], '', points)).should.deep.equal(['two'])
// arbitrary nesting should be possible
names(filter([[['even', 'odd'], 'prime']], '', points)).should.deep.equal(['two', 'three'])
2023-03-18 08:23:05 -07:00
// invalid regexes should throw meaningful errors
filter.bind(this, '/\\/', '', points).should.throw('Invalid regex')
// anything other than string/array/object/undefined is also an error
2022-01-08 14:51:03 -08:00
filter.bind(this, 28, '', points).should.throw('Unexpected type')
})
})