ergogen/test/unit/filter.js
Luke Kershaw e0eb43566f
Expand test coverage (#77)
* ignore line endings in cli tests
* ignore line endings in integration tests
* expand code coverage for `choc` footprint
* expand code coverage for `chocmini` footprint
* expand code coverage for `mx` footprint
* expand code coverage for `pad` footprint
* expand code coverage for rest of footprints
* expand code coverage for `anchor.js`
* expand code coverage for `units.js`
* expand code coverage for `points.js`
* expand code coverage for `filter.js`
* expand code coverage for `outlines.js`
* expand code coverage for `pcbs.js`
* expand code coverage for `ergogen.js`
* expand code coverage for `kle.js`
* more code coverage for `outlines.js`
* expand code coverage for `cases.js`
2023-01-23 10:02:08 +01:00

58 lines
3.2 KiB
JavaScript

const filter = require('../../src/filter').parse
const anchor = require('../../src/anchor').parse
const Point = require('../../src/point')
describe('Filter', function() {
it('empty', function() {
filter(undefined, '').should.deep.equal([new Point()])
filter(true, '').should.deep.equal([])
filter(false, '').should.deep.equal([])
})
const points = {
one: new Point(0, 1, 0, {name: 'one', tags: ['odd']}),
two: new Point(0, 2, 0, {name: 'two', tags: ['even', 'prime']}),
three: new Point(0, 3, 0, {name: 'three', tags: {odd: 'yes', prime: 'yupp'}})
}
const names = points => points.map(p => p.meta.name)
it('similar', function() {
// 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
filter(true, '', points, undefined, 'source').should.deep.equal(Object.values(points))
filter(true, '', points, undefined, 'clone').should.deep.equal([])
filter(true, '', points, undefined, 'both').should.deep.equal(Object.values(points))
// 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)()])
filter({}, '', points, undefined, 'both').should.deep.equal([anchor({}, '', points)(), anchor({}, '', points)()])
// simple name string
names(filter('one', '', points)).should.deep.equal(['one'])
// simple name regex
names(filter('/^t/', '', points)).should.deep.equal(['two', 'three'])
// tags should count, too (one for the name, three for the odd tag)
names(filter('/^o/', '', points)).should.deep.equal(['one', 'three'])
// middle spec, should be the same as above, only explicit
names(filter('~ /^o/', '', points)).should.deep.equal(['one', 'three'])
// full spec (n would normally match both one and even, but on the tags level, it's just even)
names(filter('meta.tags ~ /n/', '', points)).should.deep.equal(['two'])
// negation
names(filter('meta.tags ~ -/n/', '', points)).should.deep.equal(['one', 'three'])
// arrays OR by default at odd levels levels (including top level)...
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'])
// anything other than string/array/object/undefined is an error
filter.bind(this, 28, '', points).should.throw('Unexpected type')
})
})