Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit c5de9c2

Browse files
authored
Merge pull request #5346 from trufflesuite/test-optimize
Use test fixtures in encoder and compile-solidity
2 parents 6c444b2 + ff1f62f commit c5de9c2

File tree

14 files changed

+247
-38
lines changed

14 files changed

+247
-38
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["../../../../../.eslintrc.truffle.json"]
3+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pragma solidity >=0.4.22 <0.7.0;
2+
3+
library ConvertLib{
4+
function convert(uint amount,uint conversionRate) public pure returns (uint convertedAmount)
5+
{
6+
return amount * conversionRate;
7+
}
8+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
pragma solidity >=0.4.22 <0.7.0;
2+
3+
import "./ConvertLib.sol";
4+
5+
// This is just a simple example of a coin-like contract.
6+
// It is not standards compatible and cannot be expected to talk to other
7+
// coin/token contracts. If you want to create a standards-compliant
8+
// token, see: https://github.com/ConsenSys/Tokens. Cheers!
9+
10+
contract MetaCoin {
11+
mapping (address => uint) balances;
12+
13+
event Transfer(address indexed _from, address indexed _to, uint256 _value);
14+
15+
constructor() public {
16+
balances[tx.origin] = 10000;
17+
}
18+
19+
function sendCoin(address receiver, uint amount) public returns(bool sufficient) {
20+
if (balances[msg.sender] < amount) return false;
21+
balances[msg.sender] -= amount;
22+
balances[receiver] += amount;
23+
emit Transfer(msg.sender, receiver, amount);
24+
return true;
25+
}
26+
27+
function getBalanceInEth(address addr) public view returns(uint){
28+
return ConvertLib.convert(getBalance(addr),2);
29+
}
30+
31+
function getBalance(address addr) public view returns(uint) {
32+
return balances[addr];
33+
}
34+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const ConvertLib = artifacts.require("ConvertLib");
2+
const MetaCoin = artifacts.require("MetaCoin");
3+
4+
module.exports = function (deployer) {
5+
deployer.deploy(ConvertLib);
6+
deployer.link(ConvertLib, MetaCoin);
7+
deployer.deploy(MetaCoin);
8+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
pragma solidity >=0.4.22 <0.7.0;
2+
3+
import "truffle/Assert.sol";
4+
import "truffle/DeployedAddresses.sol";
5+
import "../contracts/MetaCoin.sol";
6+
7+
contract TestMetacoin {
8+
9+
function testInitialBalanceUsingDeployedContract() public {
10+
MetaCoin meta = MetaCoin(DeployedAddresses.MetaCoin());
11+
12+
uint expected = 10000;
13+
14+
Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
15+
}
16+
17+
function testInitialBalanceWithNewMetaCoin() public {
18+
MetaCoin meta = new MetaCoin();
19+
20+
uint expected = 10000;
21+
22+
Assert.equal(meta.getBalance(tx.origin), expected, "Owner should have 10000 MetaCoin initially");
23+
}
24+
25+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
const MetaCoin = artifacts.require("MetaCoin");
2+
3+
contract("MetaCoin", function (accounts) {
4+
it("should put 10000 MetaCoin in the first account", function () {
5+
return MetaCoin.deployed()
6+
.then(function (instance) {
7+
return instance.getBalance.call(accounts[0]);
8+
})
9+
.then(function (balance) {
10+
assert.equal(
11+
balance.valueOf(),
12+
10000,
13+
"10000 wasn't in the first account"
14+
);
15+
});
16+
});
17+
it("should call a function that depends on a linked library", function () {
18+
var meta;
19+
var metaCoinBalance;
20+
var metaCoinEthBalance;
21+
22+
return MetaCoin.deployed()
23+
.then(function (instance) {
24+
meta = instance;
25+
return meta.getBalance.call(accounts[0]);
26+
})
27+
.then(function (outCoinBalance) {
28+
metaCoinBalance = parseInt(outCoinBalance);
29+
return meta.getBalanceInEth.call(accounts[0]);
30+
})
31+
.then(function (outCoinBalanceEth) {
32+
metaCoinEthBalance = parseInt(outCoinBalanceEth);
33+
})
34+
.then(function () {
35+
assert.equal(
36+
metaCoinEthBalance,
37+
2 * metaCoinBalance,
38+
"Library function returned unexpected function, linkage may be broken"
39+
);
40+
});
41+
});
42+
it("should send coin correctly", function () {
43+
var meta;
44+
45+
// Get initial balances of first and second account.
46+
var account_one = accounts[0];
47+
var account_two = accounts[1];
48+
49+
var account_one_starting_balance;
50+
var account_two_starting_balance;
51+
var account_one_ending_balance;
52+
var account_two_ending_balance;
53+
54+
var amount = 10;
55+
56+
return MetaCoin.deployed()
57+
.then(function (instance) {
58+
meta = instance;
59+
return meta.getBalance.call(account_one);
60+
})
61+
.then(function (balance) {
62+
account_one_starting_balance = parseInt(balance);
63+
return meta.getBalance.call(account_two);
64+
})
65+
.then(function (balance) {
66+
account_two_starting_balance = parseInt(balance);
67+
return meta.sendCoin(account_two, amount, { from: account_one });
68+
})
69+
.then(function () {
70+
return meta.getBalance.call(account_one);
71+
})
72+
.then(function (balance) {
73+
account_one_ending_balance = parseInt(balance);
74+
return meta.getBalance.call(account_two);
75+
})
76+
.then(function (balance) {
77+
account_two_ending_balance = parseInt(balance);
78+
79+
assert.equal(
80+
account_one_ending_balance,
81+
account_one_starting_balance - amount,
82+
"Amount wasn't correctly taken from the sender"
83+
);
84+
assert.equal(
85+
account_two_ending_balance,
86+
account_two_starting_balance + amount,
87+
"Amount wasn't correctly sent to the receiver"
88+
);
89+
});
90+
});
91+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
networks: {
3+
development: {
4+
host: "localhost",
5+
port: 8545,
6+
network_id: "*", // Match any network id
7+
gas: 5000000
8+
}
9+
},
10+
compilers: {
11+
solc: {
12+
settings: {
13+
optimizer: {
14+
enabled: true, // Default: false
15+
runs: 200 // Default: 200
16+
}
17+
}
18+
}
19+
}
20+
};

packages/compile-solidity/test/profiler/index.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
11
const assert = require("chai").assert;
22
const fs = require("fs-extra");
3-
const glob = require("glob");
4-
const { default: Box } = require("@truffle/box");
3+
const path = require("path");
4+
const tmp = require("tmp");
55
const Profiler = require("../../dist/profiler");
66
const { Resolver } = require("@truffle/resolver");
77
const Artifactor = require("@truffle/artifactor");
8+
const TruffleConfig = require("@truffle/config");
9+
10+
function createSandbox(source) {
11+
if (!fs.existsSync(source)) {
12+
throw new Error(`Sandbox failed: source: ${source} does not exist`);
13+
}
14+
15+
const tempDir = tmp.dirSync({ unsafeCleanup: true });
16+
fs.copySync(source, tempDir.name);
17+
const config = TruffleConfig.load(
18+
path.join(tempDir.name, "truffle-config.js"),
19+
{}
20+
);
21+
return config;
22+
}
823

924
describe("profiler", function () {
1025
var config;
1126

12-
before("Create a sandbox", async function () {
13-
config = await Box.sandbox("default");
27+
before("Create a sandbox", function () {
28+
config = createSandbox(
29+
path.join(__dirname, "..", "fixture", "default-box")
30+
);
1431
config.resolver = new Resolver(config);
1532
config.artifactor = new Artifactor(config.contracts_build_directory);
1633
config.network = "development";
1734
});
1835

19-
after("Cleanup tmp files", function (done) {
20-
glob("tmp-*", (err, files) => {
21-
if (err) done(err);
22-
files.forEach(file => fs.removeSync(file));
23-
done();
24-
});
25-
});
26-
2736
it("profiles example project successfully", async function () {
2837
const { allSources, compilationTargets } = await Profiler.requiredSources(
2938
config.with({

packages/encoder/test/fixture/bare-box/contracts/.gitkeep

Whitespace-only changes.

packages/encoder/test/fixture/bare-box/migrations/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)