2020-12-31 15:50:04 -08:00
|
|
|
const p = require('../../src/prepare')
|
|
|
|
|
|
|
|
describe('Prepare', function() {
|
|
|
|
it('unnest', function() {
|
|
|
|
p.unnest({'a.b.c': 1}).should.deep.equal({a: {b: {c: 1}}})
|
|
|
|
p.unnest({'a.b.c': {
|
|
|
|
d: 2,
|
|
|
|
'e.f': 3
|
|
|
|
}}).should.deep.equal({a: {b: {c: {d: 2, e: {f: 3}}}}})
|
2021-12-12 04:23:33 -08:00
|
|
|
p.unnest({'root': [{
|
|
|
|
'a.b': 1
|
|
|
|
}]}).should.deep.equal({root: [{a: {b: 1}}]})
|
2020-12-31 15:50:04 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
it('extend', function() {
|
|
|
|
p.extend('something', undefined).should.equal('something')
|
2021-01-03 10:48:37 -08:00
|
|
|
should.equal(p.extend('something', '$unset'), undefined)
|
2020-12-31 15:50:04 -08:00
|
|
|
p.extend(undefined, 'something').should.equal('something')
|
|
|
|
p.extend(28, 'something').should.equal('something')
|
|
|
|
p.extend('something', 28).should.equal(28)
|
|
|
|
p.extend(27, 28).should.equal(28)
|
2021-01-03 10:48:37 -08:00
|
|
|
p.extend({a: 1, c: 1, d: 1}, {b: 2, c: 2, d: '$unset'}).should.deep.equal({a: 1, b: 2, c: 2})
|
2020-12-31 15:50:04 -08:00
|
|
|
p.extend([3, 2, 1], [null, 4, 5]).should.deep.equal([3, 4, 5])
|
|
|
|
})
|
|
|
|
|
|
|
|
it('inherit', function() {
|
2023-01-30 05:57:24 -08:00
|
|
|
// normal case
|
2020-12-31 15:50:04 -08:00
|
|
|
p.inherit({
|
|
|
|
a: {
|
|
|
|
x: 1,
|
|
|
|
y: 2
|
|
|
|
},
|
|
|
|
b: {
|
2021-01-03 10:48:37 -08:00
|
|
|
$extends: 'a',
|
2020-12-31 15:50:04 -08:00
|
|
|
z: 3
|
|
|
|
},
|
|
|
|
c: {
|
2021-01-03 10:48:37 -08:00
|
|
|
$extends: ['b'],
|
2020-12-31 15:50:04 -08:00
|
|
|
w: 4
|
|
|
|
}
|
|
|
|
}).c.should.deep.equal({
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3,
|
|
|
|
w: 4
|
|
|
|
})
|
2023-01-30 05:57:24 -08:00
|
|
|
// should apply to objects within arrays as well!
|
|
|
|
p.inherit({
|
|
|
|
a: {
|
|
|
|
x: 1,
|
|
|
|
y: 2
|
|
|
|
},
|
|
|
|
b: [
|
|
|
|
{
|
|
|
|
$extends: 'a',
|
|
|
|
z: 3
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}).b[0].should.deep.equal({
|
|
|
|
x: 1,
|
|
|
|
y: 2,
|
|
|
|
z: 3
|
|
|
|
})
|
2020-12-31 15:50:04 -08:00
|
|
|
})
|
2021-01-03 10:48:37 -08:00
|
|
|
|
|
|
|
it('parameterize', function() {
|
|
|
|
p.parameterize(1).should.equal(1)
|
|
|
|
|
|
|
|
p.parameterize({
|
|
|
|
unused: {
|
|
|
|
$params: ['PAR']
|
|
|
|
},
|
|
|
|
skip: {
|
|
|
|
$skip: true
|
|
|
|
}
|
|
|
|
}).should.deep.equal({})
|
|
|
|
|
|
|
|
p.parameterize({
|
|
|
|
decl: {
|
|
|
|
a: 'PAR',
|
|
|
|
$params: ['PAR'],
|
|
|
|
$args: [1]
|
|
|
|
}
|
|
|
|
}).decl.should.deep.equal({
|
2022-01-14 10:28:19 -08:00
|
|
|
a: '1'
|
|
|
|
})
|
|
|
|
|
|
|
|
p.parameterize({
|
|
|
|
decl: {
|
|
|
|
normal_use: 'PAR1',
|
|
|
|
sub: {
|
|
|
|
nested_use: 'PAR2 * 2'
|
|
|
|
},
|
|
|
|
$params: ['PAR1', 'PAR2'],
|
|
|
|
$args: ['text', 14]
|
|
|
|
}
|
|
|
|
}).decl.should.deep.equal({
|
|
|
|
normal_use: 'text',
|
|
|
|
sub: {
|
|
|
|
nested_use: '14 * 2',
|
|
|
|
}
|
2021-01-03 10:48:37 -08:00
|
|
|
})
|
|
|
|
|
|
|
|
p.parameterize.bind(this, {
|
|
|
|
decl: {
|
|
|
|
$args: [1]
|
|
|
|
}
|
|
|
|
}).should.throw('missing')
|
|
|
|
|
|
|
|
p.parameterize.bind(this, {
|
|
|
|
decl: {
|
|
|
|
$params: ['PAR1', 'PAR2'],
|
|
|
|
$args: [1]
|
|
|
|
}
|
|
|
|
}).should.throw('match')
|
|
|
|
|
|
|
|
p.parameterize.bind(this, {
|
|
|
|
decl: {
|
|
|
|
a: 'PAR',
|
|
|
|
$params: ['PAR'],
|
2022-01-14 10:28:19 -08:00
|
|
|
$args: ['in"jection']
|
2021-01-03 10:48:37 -08:00
|
|
|
}
|
|
|
|
}).should.throw('valid')
|
|
|
|
})
|
2020-12-31 15:50:04 -08:00
|
|
|
})
|