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

Earl plugin #408

Closed
wants to merge 5 commits into from
Closed
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 waffle-earl/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../.eslintrc.json')
7 changes: 7 additions & 0 deletions waffle-earl/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
process.env.NODE_ENV = 'test'
module.exports = {
require: 'ts-node/register',
timeout: 50000,
spec: 'test/**/*.test.ts',
file: 'test/setup.ts'
}
5 changes: 5 additions & 0 deletions waffle-earl/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
src
test
.eslintrc.js
tsconfig.build.json
tsconfig.json
7 changes: 7 additions & 0 deletions waffle-earl/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2020 Ethworks sp z o.o.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 changes: 40 additions & 0 deletions waffle-earl/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[![CircleCI](https://circleci.com/gh/EthWorks/Waffle.svg?style=svg)](https://circleci.com/gh/EthWorks/Waffle)
[![](https://img.shields.io/npm/v/@ethereum-waffle/chai.svg)](https://www.npmjs.com/package/@ethereum-waffle/chai)

![Ethereum Waffle](https://raw.githubusercontent.com/EthWorks/Waffle/master/docs/source/logo.png)

# @ethereum-waffle/chai

A sweet set of chai matchers for your blockchain testing needs.

## Installation

In the current version of waffle (v2.x.x) you will install this package as a dependency of the main waffle package - `ethereum-waffle`.

```
yarn add --dev ethereum-waffle
npm install --save-dev ethereum-waffle
```

If you want to use this package directly please install it via:
```
yarn add --dev @ethereum-waffle/chai
npm install --save-dev @ethereum-waffle/chai
```

## Usage
```ts
import { expect, use } from "chai";
import { waffleChai } from "@ethereum-waffle/chai";
import { bigNumberify } from "ethers/utils";

use(chaiAsPromised);

expect(bigNumberify("6")).to.be.gt(0);
```

## Feature overview

**NOTE**: You do not need to use this package directly. You can install it through the main package (`ethereum-waffle`) and use it instead.

Read more [in the documentation](https://ethereum-waffle.readthedocs.io/en/latest/matchers.html).
47 changes: 47 additions & 0 deletions waffle-earl/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@ethereum-waffle/earl",
"description": "A sweet set of earl matchers for your blockchain testing needs.",
"version": "3.2.1",
"author": "Marek Kirejczyk <account@ethworks.io> (http://ethworks.io)",
"repository": "git@github.com:EthWorks/Waffle.git",
"private": false,
"license": "MIT",
"keywords": [
"ethereum",
"smart-contracts",
"solidity",
"testing",
"javascript",
"typescript",
"library"
],
"homepage": "https://github.com/EthWorks/Waffle",
"bugs": {
"url": "https://github.com/EthWorks/Waffle/issues"
},
"main": "dist/cjs/index.js",
"module": "dist/esm/index.ts",
"types": "types.d.ts",
"scripts": {
"prepublishOnly": "yarn build",
"test": "mocha",
"lint": "eslint '{src,test}/**/*.ts'",
"lint:fix": "eslint --fix '{src,test}/**/*.ts'",
"build": "rimraf ./dist && yarn build:esm && yarn build:cjs",
"build:esm": "tsc -p tsconfig.build.json --outDir dist/esm --module ES6",
"build:cjs": "tsc -p tsconfig.build.json --outDir dist/cjs --declaration false"
},
"engines": {
"node": ">=10.0"
},
"dependencies": {
"@ethereum-waffle/provider": "^3.2.1",
"ethers": "^5.0.0"
},
"devDependencies": {
"earljs": "^0.1.6"
},
"peerDependencies": {
"earljs": "^0.1.6"
}
}
Empty file added waffle-earl/src/getControl.ts
Empty file.
5 changes: 5 additions & 0 deletions waffle-earl/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {loadPlugin} from 'earljs/internals';
import {plugin} from './plugin';

export {plugin};
loadPlugin(plugin);
25 changes: 25 additions & 0 deletions waffle-earl/src/matchers/aHexString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {Matcher} from 'earljs/internals';
import {isHexString} from '../utils';

export class HexStringMatcher extends Matcher {
constructor(private length?: number) {
super();
}

check(value: unknown) {
if (typeof value !== 'string') {
return false;
}
return isHexString(value, this.length);
}

toString() {
return this.length !== undefined
? `HexString(${this.length})`
: 'HexString';
}

static make(length?: number): string {
return new HexStringMatcher(length) as any;
}
}
19 changes: 19 additions & 0 deletions waffle-earl/src/matchers/aPrivateKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Matcher} from 'earljs/internals';
import {isPrivateKey} from '../utils';

export class PrivateKeyMatcher extends Matcher {
check(value: unknown) {
if (typeof value !== 'string') {
return false;
}
return isPrivateKey(value);
}

toString() {
return 'PrivateKey';
}

static make(): string {
return new PrivateKeyMatcher() as any;
}
}
19 changes: 19 additions & 0 deletions waffle-earl/src/matchers/anAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {Matcher} from 'earljs/internals';
import {isAddress} from '../utils';

export class AddressMatcher extends Matcher {
check(value: unknown) {
if (typeof value !== 'string') {
return false;
}
return isAddress(value);
}

toString() {
return 'Address';
}

static make(): string {
return new AddressMatcher() as any;
}
}
22 changes: 22 additions & 0 deletions waffle-earl/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {HexStringMatcher} from './matchers/aHexString';
import {AddressMatcher} from './matchers/anAddress';
import {PrivateKeyMatcher} from './matchers/aPrivateKey';
import {toBeAHexString} from './validators/toBeAHexString';
import {toBeAnAddress} from './validators/toBeAnAddress';
import {toBeAPrivateKey} from './validators/toBeAPrivateKey';
import {toEmit} from './validators/toEmit';

export const plugin = {
validators: {
toBeAHexString,
toBeAnAddress,
toBeAPrivateKey,
toEmit
},
matchers: {
aHexString: HexStringMatcher.make,
anAddress: AddressMatcher.make,
aPrivateKey: PrivateKeyMatcher.make
},
smartEqRules: []
};
22 changes: 22 additions & 0 deletions waffle-earl/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {utils} from 'ethers';
import {Expectation, Control} from 'earljs/internals';

export function getControl<T>(expectation: Expectation<T>): Control<T> {
return expectation['getControl']();
}

const HEX_REGEX = /^0x[0-9-a-fA-F]*$/;

export function isAddress(value: string) {
return HEX_REGEX.test(value) && utils.isAddress(value);
}

export function isHexString(value: string, length?: number) {
const isHex = HEX_REGEX.test(value);
const correctLength = length === undefined || value.length === length + 2;
return isHex && correctLength;
}

export function isPrivateKey(value: string) {
return isHexString(value, 64);
}
23 changes: 23 additions & 0 deletions waffle-earl/src/validators/toBeAHexString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {Expectation} from 'earljs/internals';
import {getControl, isHexString} from '../utils';

export function toBeAHexString(
this: Expectation<string>,
length?: number
): void {
const ctrl = getControl(this);

if (length !== undefined) {
ctrl.assert({
success: isHexString(ctrl.actual, length),
reason: `String "${ctrl.actual}" is not a hex string of length ${length}!`,
negatedReason: `String "${ctrl.actual}" is a hex string of length ${length}!`
});
} else {
ctrl.assert({
success: isHexString(ctrl.actual),
reason: `String "${ctrl.actual}" is not a hex string!`,
negatedReason: `String "${ctrl.actual}" is a hex string!`
});
}
}
12 changes: 12 additions & 0 deletions waffle-earl/src/validators/toBeAPrivateKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Expectation} from 'earljs/internals';
import {getControl, isPrivateKey} from '../utils';

export function toBeAPrivateKey(this: Expectation<string>): void {
const ctrl = getControl(this);

ctrl.assert({
success: isPrivateKey(ctrl.actual),
reason: `String "${ctrl.actual}" is not a private key!`,
negatedReason: `String "${ctrl.actual}" is a private key!`
});
}
12 changes: 12 additions & 0 deletions waffle-earl/src/validators/toBeAnAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import {Expectation} from 'earljs/internals';
import {getControl, isAddress} from '../utils';

export function toBeAnAddress(this: Expectation<string>): void {
const ctrl = getControl(this);

ctrl.assert({
success: isAddress(ctrl.actual),
reason: `String "${ctrl.actual}" is not an ethereum address!`,
negatedReason: `String "${ctrl.actual}" is an ethereum address!`
});
}
69 changes: 69 additions & 0 deletions waffle-earl/src/validators/toEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {Expectation, smartEq} from 'earljs/internals';
import {Contract, providers} from 'ethers';
import {LogDescription} from 'ethers/lib/utils';
import {getControl} from '../utils';

export async function toEmit(
this: Expectation<
| providers.TransactionResponse
| providers.TransactionReceipt
| Promise<providers.TransactionResponse | providers.TransactionReceipt>
>,
contract: Contract,
event: string,
args?: any[]
): Promise<void> {
const ctrl = getControl(this);

let hasEvent = false;
try {
contract.interface.getEvent(event);
hasEvent = true;
} catch {}
if (!hasEvent) {
const message =
`Event ${event} does not exist on the provided contract!\n` +
'Make sure you have compiled the latest version.';
ctrl.assert({
success: ctrl.isNegated,
reason: message,
negatedReason: message
});
}

const tx = await Promise.resolve(ctrl.actual);
const receipt = isResponse(tx) ? await tx.wait() : tx;

const events: LogDescription[] = [];
for (const log of receipt.logs) {
if (log.address === contract.address) {
try {
events.push(contract.interface.parseLog(log));
} catch {}
}
}

if (!args) {
ctrl.assert({
success: events.some((x) => x.name === event),
reason: `Event ${event} was not emitted by the provided contract.`,
negatedReason: `Event ${event} was emitted by the provided contract.`
});
} else {
const relevant = events
.filter((x) => x.name === event)
.map((x) => [...x.args]);
const success = relevant.some((x) => smartEq(x, args).result === 'success');
ctrl.assert({
success: success,
reason: `Event ${event} was not emitted by the provided contract TODO: ARGS.`,
negatedReason: `Event ${event} was emitted by the provided contract TODO: ARGS.`
});
}
}

function isResponse(
value: providers.TransactionResponse | providers.TransactionReceipt
): value is providers.TransactionResponse {
return 'wait' in value;
}
20 changes: 20 additions & 0 deletions waffle-earl/test/contracts/Calls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const CALLS_SOURCE = `
pragma solidity ^0.6.0;

contract Calls {
function callWithoutParameter() pure public {}

function callWithParameter(uint param) public {}

function callWithParameters(uint param1, uint param2) public {}
}
`;

export const CALLS_ABI = [
'function callWithoutParameter() public',
'function callWithParameter(uint param) public',
'function callWithParameters(uint param1, uint param2) public'
];

// eslint-disable-next-line max-len
export const CALLS_BYTECODE = '608060405234801561001057600080fd5b5060e88061001f6000396000f3fe6080604052348015600f57600080fd5b5060043610603c5760003560e01c8063270f7979146041578063586a7e23146049578063c3e86c6614607e575b600080fd5b604760a9565b005b607c60048036036040811015605d57600080fd5b81019080803590602001909291908035906020019092919050505060ab565b005b60a760048036036020811015609257600080fd5b810190808035906020019092919050505060af565b005b565b5050565b5056fea2646970667358221220042e49619d2f4371b311b491637d1c6a9c9ad3c55696a6a77435579e3f1baf6b64736f6c63430006000033';
Loading