From d364ec148a42d82eaba7a3e4adad1b160d359f66 Mon Sep 17 00:00:00 2001 From: Rohan Dahibhate <109022906+Rohan-Dah@users.noreply.github.com> Date: Sat, 6 May 2023 20:16:49 +0530 Subject: [PATCH] Added unit tests for coin,js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Line 52: Each assertion here will throw an error if input type is not valid. For example, version must be a number Line 61: Here we are calculating number of confirmations for a transaction output based on current block height. In the case used here, if the coin height is 100 and current block height is 99, the depth should be 0. If the current block height is 101, the depth should be 2. Line 77: Here firstly a coin object is created with all the valid attributes such as version, height etc. It then converts the coin object to JSON format using .toJSON() and then creates new coin object by converting JSON back using .fromJSON. This makes sure serialization and deserialization functions are working properly. Line 95: Here we are creating a new coin object using the input object ‘options’. Here assert statements are checking if coin objects are having correct values for each property. --- test/coin-test.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/coin-test.js b/test/coin-test.js index c6e25a8ff..79f5ac14d 100644 --- a/test/coin-test.js +++ b/test/coin-test.js @@ -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); + }); });