2018-07-23 09:45:01 +02:00
|
|
|
const { describe, it } = require('mocha')
|
2018-06-05 09:24:47 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const u = require('./payments.utils')
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
;['embed', 'p2ms', 'p2pk', 'p2pkh', 'p2sh', 'p2wpkh', 'p2wsh'].forEach(p => {
|
|
|
|
describe(p, () => {
|
2018-12-28 16:00:52 +01:00
|
|
|
let fn
|
2019-01-04 10:33:02 +01:00
|
|
|
let payment = require('../src/payments/' + p)
|
2018-12-28 16:00:52 +01:00
|
|
|
if (p === 'embed') {
|
|
|
|
fn = payment.p2data
|
|
|
|
} else {
|
|
|
|
fn = payment[p]
|
|
|
|
}
|
2018-06-05 09:24:47 +02:00
|
|
|
const fixtures = require('./fixtures/' + p)
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.valid.forEach((f, i) => {
|
|
|
|
it(f.description + ' as expected', () => {
|
2018-07-03 14:33:04 +02:00
|
|
|
const args = u.preform(f.arguments)
|
2018-06-05 09:24:47 +02:00
|
|
|
const actual = fn(args, f.options)
|
2018-07-03 14:33:04 +02:00
|
|
|
|
2018-06-05 09:24:47 +02:00
|
|
|
u.equate(actual, f.expected, f.arguments)
|
|
|
|
})
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it(f.description + ' as expected (no validation)', () => {
|
2018-07-03 14:33:04 +02:00
|
|
|
const args = u.preform(f.arguments)
|
2018-06-05 09:24:47 +02:00
|
|
|
const actual = fn(args, Object.assign({}, f.options, {
|
|
|
|
validate: false
|
|
|
|
}))
|
|
|
|
|
|
|
|
u.equate(actual, f.expected, f.arguments)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.forEach(f => {
|
|
|
|
it('throws ' + f.exception + (f.description ? ('for ' + f.description) : ''), () => {
|
2018-06-05 09:24:47 +02:00
|
|
|
const args = u.preform(f.arguments)
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
assert.throws(() => {
|
2018-06-05 09:24:47 +02:00
|
|
|
fn(args, f.options)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// cross-verify dynamically too
|
|
|
|
if (!fixtures.dynamic) return
|
|
|
|
const { depends, details } = fixtures.dynamic
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
details.forEach(f => {
|
2018-06-05 09:24:47 +02:00
|
|
|
const detail = u.preform(f)
|
|
|
|
const disabled = {}
|
2019-04-09 08:09:50 +02:00
|
|
|
if (f.disabled) f.disabled.forEach(k => { disabled[k] = true })
|
2018-06-05 09:24:47 +02:00
|
|
|
|
|
|
|
for (let key in depends) {
|
|
|
|
if (key in disabled) continue
|
|
|
|
const dependencies = depends[key]
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
dependencies.forEach(dependency => {
|
2018-06-05 09:24:47 +02:00
|
|
|
if (!Array.isArray(dependency)) dependency = [dependency]
|
|
|
|
|
|
|
|
const args = {}
|
2019-04-09 08:09:50 +02:00
|
|
|
dependency.forEach(d => { u.from(d, detail, args) })
|
2018-06-05 09:24:47 +02:00
|
|
|
const expected = u.from(key, detail)
|
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it(f.description + ', ' + key + ' derives from ' + JSON.stringify(dependency), () => {
|
2018-06-05 09:24:47 +02:00
|
|
|
u.equate(fn(args), expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|