Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for coin.js #1150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions test/coin-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,69 @@ describe('Coin', function() {
assert(fmt.includes('coinbase'));
assert(fmt.includes('script'));
});

it('should validate input types', function() {
assert.throws(() => new Coin().fromOptions({ version: '1' }));
assert.throws(() => new Coin().fromOptions({ height: -2 }));
assert.throws(() => new Coin().fromOptions({ value: -1 }));
assert.throws(() => new Coin().fromOptions({ coinbase: 'true' }));
assert.throws(() => new Coin().fromOptions({ hash: 'abc' }));
assert.throws(() => new Coin().fromOptions({ index: -1 }));
});

it('should correctly calculate depth', () => {
const coin = new Coin({
version: 1,
height: 100,
value: 5000000000,
script: Buffer.from('76a91476a04053bda0a88bda5177b86a15c3b29f5598788ac', 'hex'),
coinbase: false,
hash: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
index: 0
});

assert.strictEqual(coin.getDepth(99), 0);
assert.strictEqual(coin.getDepth(100), 1);
assert.strictEqual(coin.getDepth(101), 2);
});

it('should correctly serialize and deserialize to/from JSON', () => {
const coin = new Coin({
version: 1,
height: 100,
value: 5000000000,
script: Buffer.from('76a91476a04053bda0a88bda5177b86a15c3b29f5598788ac', 'hex'),
coinbase: false,
hash: Buffer.from('0000000000000000000000000000000000000000000000000000000000000000', 'hex'),
index: 0
});

const json = coin.toJSON();
const coin2 = Coin.fromJSON(json);

assert.deepStrictEqual(coin.toRaw(), coin2.toRaw());
});


it('should create a coin from options', function() {
const options = {
version: 1,
height: 42,
value: 1000000,
script: Buffer.from('a914d7f6d1c6e2d6eeb2ae2f88a52f032cbbf5e5b5c987', 'hex'),
coinbase: false,
hash: Buffer.from('0'.repeat(64), 'hex'),
index: 0
};

const coin = Coin.fromOptions(options);

assert.strictEqual(coin.version, options.version);
assert.strictEqual(coin.height, options.height);
assert.strictEqual(coin.value, options.value);
assert.deepStrictEqual(coin.script.toRaw(), options.script);
assert.strictEqual(coin.coinbase, options.coinbase);
assert.deepStrictEqual(coin.hash, options.hash);
assert.strictEqual(coin.index, options.index);
});
});