Add filter tests
This commit is contained in:
parent
a7f333c9bc
commit
bd6b5a0ca6
3 changed files with 48 additions and 2 deletions
|
@ -11,6 +11,8 @@
|
||||||
- Template for creating them, built-in variables they can use, documentation, external links, etc.
|
- Template for creating them, built-in variables they can use, documentation, external links, etc.
|
||||||
- Also considering how (or, on which layer) they define their silks, universal mirroring behaviour, etc.
|
- Also considering how (or, on which layer) they define their silks, universal mirroring behaviour, etc.
|
||||||
- https://en.wikipedia.org/wiki/Reference_designator#Designators
|
- https://en.wikipedia.org/wiki/Reference_designator#Designators
|
||||||
|
- Kill glue
|
||||||
|
- Revamp binding??
|
||||||
|
|
||||||
### Minor
|
### Minor
|
||||||
|
|
||||||
|
|
|
@ -72,10 +72,10 @@ const simple = (exp, name, units) => {
|
||||||
value = exp
|
value = exp
|
||||||
}
|
}
|
||||||
|
|
||||||
return point => keys.some(key => comparators[op](key, value, name, units))
|
return point => keys.some(key => comparators[op](key, value, name, units)(point))
|
||||||
}
|
}
|
||||||
|
|
||||||
const complex = (config, name, units, aggregator=_and) => {
|
const complex = (config, name, units, aggregator=_or) => {
|
||||||
|
|
||||||
// default is all points
|
// default is all points
|
||||||
if (config === undefined) {
|
if (config === undefined) {
|
||||||
|
|
44
test/unit/filter.js
Normal file
44
test/unit/filter.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
const filter = require('../../src/filter').parse
|
||||||
|
const anchor = require('../../src/anchor').parse
|
||||||
|
const Point = require('../../src/point')
|
||||||
|
|
||||||
|
describe('Filter', function() {
|
||||||
|
|
||||||
|
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() {
|
||||||
|
// no points filter to no points
|
||||||
|
filter(undefined, '', undefined).should.deep.equal([])
|
||||||
|
// and undefined config doesn't filter anything
|
||||||
|
filter(undefined, '', points).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)()])
|
||||||
|
// 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')
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
Loading…
Add table
Add a link
Reference in a new issue