tests: reduce setup-code duplication
This commit is contained in:
parent
95911c5dde
commit
3f53b528a8
2 changed files with 55 additions and 75 deletions
test
1
test/fixtures/transaction_builder.json
vendored
1
test/fixtures/transaction_builder.json
vendored
|
@ -168,6 +168,7 @@
|
||||||
{
|
{
|
||||||
"description": "Incomplete transaction w/ prevTxScript defined",
|
"description": "Incomplete transaction w/ prevTxScript defined",
|
||||||
"exception": "Transaction is not complete",
|
"exception": "Transaction is not complete",
|
||||||
|
"alwaysThrows": true,
|
||||||
"inputs": [
|
"inputs": [
|
||||||
{
|
{
|
||||||
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
"txId": "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
|
||||||
|
|
|
@ -9,6 +9,44 @@ var TransactionBuilder = require('../src/transaction_builder')
|
||||||
|
|
||||||
var fixtures = require('./fixtures/transaction_builder')
|
var fixtures = require('./fixtures/transaction_builder')
|
||||||
|
|
||||||
|
function construct(txb, f, sign) {
|
||||||
|
f.inputs.forEach(function(input) {
|
||||||
|
var prevTxScript
|
||||||
|
|
||||||
|
if (input.prevTxScript) {
|
||||||
|
prevTxScript = Script.fromASM(input.prevTxScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
||||||
|
})
|
||||||
|
|
||||||
|
f.outputs.forEach(function(output) {
|
||||||
|
var script = Script.fromASM(output.script)
|
||||||
|
|
||||||
|
txb.addOutput(script, output.value)
|
||||||
|
})
|
||||||
|
|
||||||
|
if (sign === undefined || sign) {
|
||||||
|
f.inputs.forEach(function(input, index) {
|
||||||
|
var redeemScript
|
||||||
|
|
||||||
|
if (input.redeemScript) {
|
||||||
|
redeemScript = Script.fromASM(input.redeemScript)
|
||||||
|
}
|
||||||
|
|
||||||
|
input.privKeys.forEach(function(wif) {
|
||||||
|
var privKey = ECKey.fromWIF(wif)
|
||||||
|
|
||||||
|
txb.sign(index, privKey, redeemScript)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: add support for locktime/version in TransactionBuilder API
|
||||||
|
if (f.version !== undefined) txb.tx.version = f.version
|
||||||
|
if (f.locktime !== undefined) txb.tx.locktime = f.locktime
|
||||||
|
}
|
||||||
|
|
||||||
describe('TransactionBuilder', function() {
|
describe('TransactionBuilder', function() {
|
||||||
var privAddress, privScript
|
var privAddress, privScript
|
||||||
var prevTx, prevTxHash
|
var prevTx, prevTxHash
|
||||||
|
@ -135,21 +173,7 @@ describe('TransactionBuilder', function() {
|
||||||
|
|
||||||
fixtures.invalid.sign.forEach(function(f) {
|
fixtures.invalid.sign.forEach(function(f) {
|
||||||
it('throws on ' + f.exception + ' (' + f.description + ')', function() {
|
it('throws on ' + f.exception + ' (' + f.description + ')', function() {
|
||||||
f.inputs.forEach(function(input) {
|
construct(txb, f, false)
|
||||||
var prevTxScript
|
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.outputs.forEach(function(output) {
|
|
||||||
var script = Script.fromASM(output.script)
|
|
||||||
|
|
||||||
txb.addOutput(script, output.value)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.inputs.forEach(function(input, index) {
|
f.inputs.forEach(function(input, index) {
|
||||||
var redeemScript
|
var redeemScript
|
||||||
|
@ -178,39 +202,7 @@ describe('TransactionBuilder', function() {
|
||||||
describe('build', function() {
|
describe('build', function() {
|
||||||
fixtures.valid.build.forEach(function(f) {
|
fixtures.valid.build.forEach(function(f) {
|
||||||
it('builds \"' + f.description + '\"', function() {
|
it('builds \"' + f.description + '\"', function() {
|
||||||
f.inputs.forEach(function(input) {
|
construct(txb, f)
|
||||||
var prevTxScript
|
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.outputs.forEach(function(output) {
|
|
||||||
var script = Script.fromASM(output.script)
|
|
||||||
|
|
||||||
txb.addOutput(script, output.value)
|
|
||||||
})
|
|
||||||
|
|
||||||
f.inputs.forEach(function(input, index) {
|
|
||||||
var redeemScript
|
|
||||||
|
|
||||||
if (input.redeemScript) {
|
|
||||||
redeemScript = Script.fromASM(input.redeemScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
input.privKeys.forEach(function(wif) {
|
|
||||||
var privKey = ECKey.fromWIF(wif)
|
|
||||||
|
|
||||||
txb.sign(index, privKey, redeemScript)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
// FIXME: add support for locktime/version in TransactionBuilder API
|
|
||||||
if (f.version !== undefined) txb.tx.version = f.version
|
|
||||||
if (f.locktime !== undefined) txb.tx.locktime = f.locktime
|
|
||||||
|
|
||||||
var tx = txb.build()
|
var tx = txb.build()
|
||||||
assert.equal(tx.toHex(), f.txHex)
|
assert.equal(tx.toHex(), f.txHex)
|
||||||
|
@ -218,40 +210,27 @@ describe('TransactionBuilder', function() {
|
||||||
})
|
})
|
||||||
|
|
||||||
fixtures.invalid.build.forEach(function(f) {
|
fixtures.invalid.build.forEach(function(f) {
|
||||||
it('throws on ' + f.exception + ' (' + f.description + ')', function() {
|
describe('for ' + f.description, function() {
|
||||||
f.inputs.forEach(function(input) {
|
beforeEach(function() {
|
||||||
var prevTxScript
|
if (f.txHex) {
|
||||||
|
var tx = Transaction.fromHex(f.txHex)
|
||||||
|
txb = TransactionBuilder.fromTransaction(tx)
|
||||||
|
|
||||||
if (input.prevTxScript) {
|
} else {
|
||||||
prevTxScript = Script.fromASM(input.prevTxScript)
|
construct(txb, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
txb.addInput(input.txId, input.vout, input.sequence, prevTxScript)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
f.outputs.forEach(function(output) {
|
it('throws on ' + f.exception, function() {
|
||||||
var script = Script.fromASM(output.script)
|
assert.throws(function() {
|
||||||
|
txb.build()
|
||||||
txb.addOutput(script, output.value)
|
}, new RegExp(f.exception))
|
||||||
})
|
})
|
||||||
|
|
||||||
f.inputs.forEach(function(input, index) {
|
if (f.alwaysThrows) return
|
||||||
var redeemScript
|
it('doesn\'t throw if building incomplete', function() {
|
||||||
|
txb.buildIncomplete()
|
||||||
if (input.redeemScript) {
|
|
||||||
redeemScript = Script.fromASM(input.redeemScript)
|
|
||||||
}
|
|
||||||
|
|
||||||
input.privKeys.forEach(function(wif) {
|
|
||||||
var privKey = ECKey.fromWIF(wif)
|
|
||||||
|
|
||||||
txb.sign(index, privKey, redeemScript)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
assert.throws(function() {
|
|
||||||
txb.build()
|
|
||||||
}, new RegExp(f.exception))
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Reference in a new issue