Merge pull request #61 from isocolsky/cli_tests
improve parsing of m-of-n on wallet creation
This commit is contained in:
commit
88bcef1ff3
|
@ -19,7 +19,12 @@ var walletName = args[0];
|
|||
var copayerName = args[2] || process.env.USER;
|
||||
var network = program.testnet ? 'testnet' : 'livenet';
|
||||
|
||||
var mn = utils.parseMN(args[1]);
|
||||
var mn;
|
||||
try {
|
||||
mn = utils.parseMN(args[1]);
|
||||
} catch (ex) {
|
||||
die(ex);
|
||||
}
|
||||
|
||||
var client = utils.getClient(program);
|
||||
client.createWallet(walletName, copayerName, mn[0], mn[1], network, function(err, secret) {
|
||||
|
|
|
@ -10,17 +10,17 @@ var die = Utils.die = function(err) {
|
|||
}
|
||||
};
|
||||
|
||||
Utils.parseMN = function(MN) {
|
||||
if (!MN)
|
||||
die('No m-n parameter');
|
||||
var mn = MN.split('-');
|
||||
Utils.parseMN = function(text) {
|
||||
if (!text) throw new Error('No m-n parameter');
|
||||
|
||||
var m = parseInt(mn[0]);
|
||||
var n = parseInt(mn[1]);
|
||||
var regex = /^(\d+)(-|of|-of-)?(\d+)$/i;
|
||||
var match = regex.exec(text.trim());
|
||||
|
||||
if (!m || !n) {
|
||||
die('Bad m-n parameter:' + MN);
|
||||
}
|
||||
if (!match || match.length === 0) throw new Error('Invalid m-n parameter');
|
||||
|
||||
var m = parseInt(match[1]);
|
||||
var n = parseInt(match[3]);
|
||||
if (m > n) throw new Error('Invalid m-n parameter');
|
||||
|
||||
return [m, n];
|
||||
};
|
||||
|
|
|
@ -4,9 +4,56 @@ var _ = require('lodash');
|
|||
var chai = require('chai');
|
||||
var sinon = require('sinon');
|
||||
var should = chai.should();
|
||||
var CliUtils = require('./cli-utils');
|
||||
var CliUtils = require('../cli-utils');
|
||||
|
||||
describe('CliUtils', function() {
|
||||
describe('#parseMN', function() {
|
||||
it('should successfully parse m & n', function() {
|
||||
var texts = {
|
||||
'1-1': [1, 1],
|
||||
'1-of-1': [1, 1],
|
||||
'1of1': [1, 1],
|
||||
'1-OF-2': [1, 2],
|
||||
'1OF2': [1, 2],
|
||||
' 2-2': [2, 2],
|
||||
'2-3 ': [2, 3],
|
||||
'10-10': [10, 10],
|
||||
'10-of-10': [10, 10],
|
||||
};
|
||||
_.each(texts, function(expected, text) {
|
||||
var result = CliUtils.parseMN(text);
|
||||
result.should.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
it('should fail to parse incorrect m & n', function() {
|
||||
var texts = [
|
||||
'',
|
||||
' ',
|
||||
'1',
|
||||
'x-1',
|
||||
'1-x',
|
||||
'of-1-1',
|
||||
'2-2-of',
|
||||
'1-1-1',
|
||||
' 1_1 ',
|
||||
'2-1',
|
||||
'2-of-1',
|
||||
'-1-2',
|
||||
'1--2',
|
||||
'x-of-2',
|
||||
];
|
||||
_.each(texts, function(text) {
|
||||
var valid = true;
|
||||
try {
|
||||
CliUtils.parseMN(text);
|
||||
} catch (e) {
|
||||
valid = false;
|
||||
}
|
||||
valid.should.be.false;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#parseAmount', function() {
|
||||
it('should successfully parse amounts', function() {
|
||||
var texts = {
|
||||
|
@ -56,7 +103,7 @@ describe('CliUtils', function() {
|
|||
_.each(texts, function(text) {
|
||||
var valid = true;
|
||||
try {
|
||||
var amount = CliUtils.parseAmount(text);
|
||||
CliUtils.parseAmount(text);
|
||||
} catch (e) {
|
||||
valid = false;
|
||||
}
|
Loading…
Reference in New Issue