Skip to content
This repository has been archived by the owner on Jan 11, 2022. It is now read-only.

rewrote tests to typescript, improved typings #39

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ package-lock.json


dist
.nyc_output
13 changes: 13 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"reporter": [
"lcov",
"text"
],
"include": [
"src/**/*.ts"
],
"extension": [
".ts"
],
"all": true
}
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
},
"scripts": {
"build": "tsc",
"coverage": "istanbul cover _mocha",
"coveralls": "npm run coverage && coveralls <coverage/lcov.info",
"coverage": "nyc mocha --reporter spec --require ts-node/register 'test/index.ts'",
"coveralls": "npm run coverage && nyc report --reporter=text-lcov | coveralls",
"lint": "standard",
"test": "istanbul test mocha -- --reporter spec",
"test": "mocha --reporter spec --require ts-node/register 'test/index.ts'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot make it work with 'test/index.ts' on windows cmd. I need to remove the quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, i dont use windows these days i think it's 100% fine in this case to remove the quotes. (it's not when there is a glob pattern inside)

"webtest": "browserify test/max.js | testling -u"
},
"repository": {
Expand Down Expand Up @@ -39,13 +39,15 @@
},
"devDependencies": {
"@types/bn.js": "^4.11.3",
"@types/mocha": "^5.2.5",
"@types/node": "^10.12.2",
"bn.js": "^4.11.1",
"nyc": "^13.1.0",
"coveralls": "^2.11.4",
"ethereumjs-testing": "git+https://github.com/ethereumjs/ethereumjs-testing.git",
"istanbul": "^0.4.2",
"mocha": "^2.3.4",
"mocha": "4.1.0",
"standard": "^6.0.8",
"ts-node": "^7.0.1",
"typescript": "^3.1.6"
},
"bin": {
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function encodeLength(len: number, offset: number): Buffer {
* @param stream - Is the input a stream (false by default)
* @returns - returns decode Array of Buffers containg the original message
**/
export function decode(input: Buffer, stream?: boolean): Buffer;
export function decode(input: Buffer[], stream?: boolean): Buffer[];
krzkaczor marked this conversation as resolved.
Show resolved Hide resolved
export function decode(input: RLPInput, stream: boolean = false): Buffer[] | Buffer | RLPDecoded {
if (!input || (<any>input).length === 0) {
return Buffer.from([]);
Expand Down Expand Up @@ -259,5 +261,6 @@ function isNumber(input: RLPInput): input is number {

// Check if an input is a BigNumber
function isBN(input: RLPInput): input is BN {
return !!input.hasOwnProperty('toArray');
if (!input) return false;
return !!input.hasOwnProperty('toArray');
krzkaczor marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import BN from 'bn.js';

export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray;
export type RLPInput = Buffer | string | number | Uint8Array | BN | RLPObject | RLPArray | null;

export interface RLPArray extends Array<RLPInput> {};
interface RLPObject {
Expand Down
30 changes: 16 additions & 14 deletions test/index.js → test/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const assert = require('assert')
const RLP = require('../dist/index.js')
const BN = require('bn.js')
const testing = require('ethereumjs-testing')
import * as assert from "assert";
import * as RLP from "../src";
const BN = require('bn.js');
const testing = require('ethereumjs-testing');

describe('invalid rlps', function () {
it('should not crash on an invalid rlp', function () {
Expand Down Expand Up @@ -113,11 +113,12 @@ describe('RLP decoding (int):', function () {
})

describe('strings over 55 bytes long', function () {
var testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion'
testString = Buffer.from(testString)
var encoded = null
const testString = 'This function takes in a data, convert it to buffer if not, and a length for recursion'
const testBuffer = Buffer.from(testString)
let encoded:Buffer

it('should encode it', function () {
encoded = RLP.encode(testString)
encoded = RLP.encode(testBuffer)
assert.equal(encoded[0], 184)
assert.equal(encoded[1], 86)
})
Expand All @@ -129,17 +130,18 @@ describe('strings over 55 bytes long', function () {
})

describe('list over 55 bytes long', function () {
var testString = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9']
var encoded = null
const testString = ['This', 'function', 'takes', 'in', 'a', 'data', 'convert', 'it', 'to', 'buffer', 'if', 'not', 'and', 'a', 'length', 'for', 'recursion', 'a1', 'a2', 'a3', 'ia4', 'a5', 'a6', 'a7', 'a8', 'ba9']
let encoded: Buffer

it('should encode it', function () {
encoded = RLP.encode(testString)
})

it('should decode', function () {
var decoded = RLP.decode(encoded)
for (var i = 0; i < decoded.length; i++) {
decoded[i] = decoded[i].toString()
const decodedBuffer = RLP.decode(encoded)
const decoded: string[] = []
for (var i = 0; i < decodedBuffer.length; i++) {
decoded[i] = decodedBuffer[i].toString()
}
assert.deepEqual(decoded, testString)
})
Expand Down Expand Up @@ -169,7 +171,7 @@ describe('nested lists:', function () {
]
]
]
var encoded
var encoded: Buffer
it('encode a nested list', function () {
encoded = RLP.encode(nestedList)
assert.deepEqual(encoded, Buffer.from([0xc7, 0xc0, 0xc1, 0xc0, 0xc3, 0xc0, 0xc1, 0xc0]))
Expand Down