Skip to content

Commit

Permalink
refactor(test): Refactoring fixture() pattern to before/beforeEach pa…
Browse files Browse the repository at this point in the history
…ttern for tests (#451)

* refactor libbridgedata tests

* fixed bug where since describe block is async, mocha doesn't run tests

* converted all bridge related tests from fixture to before/beforeEach aside from Bridge.test.ts

* refactored L1 unit tests from fixtures to before/beforeEach

* refactor remaining L2, thirdparty test fixtures to before/beforeEach

* simplify

* Apply suggestions from code review

Co-authored-by: David <104078303+davidtaikocha@users.noreply.github.com>

* fix

Co-authored-by: David <104078303+davidtaikocha@users.noreply.github.com>
  • Loading branch information
RogerLamTd and davidtaikocha committed Dec 18, 2022
1 parent f9dd4e0 commit 19de694
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 596 deletions.
7 changes: 4 additions & 3 deletions packages/protocol/test/ConfigManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { expect } from "chai"
import { ConfigManager } from "../typechain"
const hre = require("hardhat")
const ethers = hre.ethers

describe("ConfigManager", function () {
let configManager: any
let testKey: any
let testName: any
let configManager: ConfigManager
let testKey: string
let testName: string

before(async function () {
configManager = await (
Expand Down
36 changes: 16 additions & 20 deletions packages/protocol/test/L1/TaikoL1.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { expect } from "chai"
import { ethers } from "hardhat"
import { TaikoL1 } from "../../typechain"

describe("TaikoL1", function () {
async function deployTaikoL1Fixture() {
// Deploying addressManager Contract
let taikoL1: TaikoL1
let genesisHash: string

beforeEach(async function () {
const addressManager = await (
await ethers.getContractFactory("AddressManager")
).deploy()
Expand Down Expand Up @@ -39,46 +42,39 @@ describe("TaikoL1", function () {
await ethers.getContractFactory("V1Verifying")
).deploy()

const TaikoL1Factory = await ethers.getContractFactory("TaikoL1", {
libraries: {
V1Verifying: v1Verifying.address,
V1Proposing: v1Proposing.address,
V1Proving: v1Proving.address,
},
})

const genesisHash = randomBytes32()
const taikoL1 = await TaikoL1Factory.deploy()
genesisHash = randomBytes32()
taikoL1 = await (
await ethers.getContractFactory("TaikoL1", {
libraries: {
V1Verifying: v1Verifying.address,
V1Proposing: v1Proposing.address,
V1Proving: v1Proving.address,
},
})
).deploy()
await taikoL1.init(addressManager.address, genesisHash)

return { taikoL1, genesisHash }
}
})

describe("getLatestSyncedHeader()", async function () {
it("should be genesisHash because no headers have been synced", async function () {
const { taikoL1, genesisHash } = await deployTaikoL1Fixture()
const hash = await taikoL1.getLatestSyncedHeader()
expect(hash).to.be.eq(genesisHash)
})
})

describe("getSyncedHeader()", async function () {
it("should revert because header number has not been synced", async function () {
const { taikoL1 } = await deployTaikoL1Fixture()
await expect(taikoL1.getSyncedHeader(1)).to.be.revertedWith("L1:id")
})

it("should return appropraite hash for header", async function () {
const { taikoL1, genesisHash } = await deployTaikoL1Fixture()
const hash = await taikoL1.getSyncedHeader(0)
expect(hash).to.be.eq(genesisHash)
})
})

describe("getBlockProvers()", async function () {
it("should return empty list when there is no proof for that block", async function () {
const { taikoL1 } = await deployTaikoL1Fixture()

const provers = await taikoL1.getBlockProvers(
Math.ceil(Math.random() * 1024),
randomBytes32()
Expand Down
62 changes: 17 additions & 45 deletions packages/protocol/test/L1/TkoToken.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,28 @@ import {
ERC20_BURN_AMOUNT_EXCEEDED,
ERC20_TRANSFER_AMOUNT_EXCEEDED,
} from "../constants/errors"
import { BigNumber } from "ethers"

describe("TokenVault", function () {
async function deployTkoTokenFixture() {
const [owner, nonOwner, protoBroker] = await ethers.getSigners()
let owner: any
let nonOwner: any
let protoBroker: any
let token: TkoToken
let amountMinted: BigNumber

before(async function () {
;[owner, nonOwner, protoBroker] = await ethers.getSigners()
})

// Deploying addressManager Contract
beforeEach(async function () {
const addressManager: AddressManager = await (
await ethers.getContractFactory("AddressManager")
).deploy()
await addressManager.init()

const TkoTokenFactory = await ethers.getContractFactory("TkoToken")

const token: TkoToken = await TkoTokenFactory.connect(owner).deploy()

token = await (await ethers.getContractFactory("TkoToken"))
.connect(owner)
.deploy()
await token.init(addressManager.address)

const { chainId } = await ethers.provider.getNetwork()
Expand All @@ -29,44 +36,28 @@ describe("TokenVault", function () {
`${chainId}.proto_broker`,
protoBroker.address
)
const amountMinted = ethers.utils.parseEther("100")

amountMinted = ethers.utils.parseEther("100")
await token.connect(protoBroker).mint(owner.address, amountMinted)

const ownerBalance = await token.balanceOf(owner.address)
expect(ownerBalance).to.be.eq(amountMinted)

return {
owner,
nonOwner,
token,
addressManager,
amountMinted,
protoBroker,
}
}
})

describe("mint()", async () => {
it("throws when to is equal to the zero address", async () => {
const { token, protoBroker } = await deployTkoTokenFixture()

await expect(
token.connect(protoBroker).mint(ethers.constants.AddressZero, 1)
).to.be.revertedWith("TKO: invalid address")
})

it("throws when minter is not the protoBroker", async () => {
const { owner, token, amountMinted, nonOwner } =
await deployTkoTokenFixture()

await expect(
token.connect(owner).mint(nonOwner.address, amountMinted.add(1))
).to.be.revertedWith(ADDRESS_RESOLVER_DENIED)
})

it("succeeds", async () => {
const { token, amountMinted, nonOwner, protoBroker } =
await deployTkoTokenFixture()

const originalBalance = await token.balanceOf(nonOwner.address)

await token
Expand All @@ -82,26 +73,18 @@ describe("TokenVault", function () {

describe("burn()", async () => {
it("throws when to is equal to the zero address", async () => {
const { token, protoBroker } = await deployTkoTokenFixture()

await expect(
token.connect(protoBroker).burn(ethers.constants.AddressZero, 1)
).to.be.revertedWith("TKO: invalid address")
})

it("throws when burner is not the protoBroker", async () => {
const { owner, token, amountMinted, nonOwner } =
await deployTkoTokenFixture()

await expect(
token.connect(owner).burn(nonOwner.address, amountMinted.add(1))
).to.be.revertedWith(ADDRESS_RESOLVER_DENIED)
})

it("throws when account balance is < amount requested to burn", async () => {
const { owner, protoBroker, token, amountMinted } =
await deployTkoTokenFixture()

await expect(
token
.connect(protoBroker)
Expand All @@ -110,9 +93,6 @@ describe("TokenVault", function () {
})

it("succeeds", async () => {
const { token, amountMinted, owner, protoBroker } =
await deployTkoTokenFixture()

const originalBalance = await token.balanceOf(owner.address)

await token.connect(protoBroker).burn(owner.address, amountMinted)
Expand All @@ -126,17 +106,12 @@ describe("TokenVault", function () {

describe("transfer()", async () => {
it("throws when to is equal to the contract address", async () => {
const { owner, token } = await deployTkoTokenFixture()

await expect(
token.connect(owner).transfer(token.address, 1)
).to.be.revertedWith("TKO: invalid to")
})

it("throws when transfer is > user's amount", async () => {
const { owner, token, amountMinted, nonOwner } =
await deployTkoTokenFixture()

await expect(
token
.connect(owner)
Expand All @@ -145,9 +120,6 @@ describe("TokenVault", function () {
})

it("succeeds", async () => {
const { owner, token, amountMinted, nonOwner } =
await deployTkoTokenFixture()

const originalBalance = await token.balanceOf(nonOwner.address)

await token.connect(owner).transfer(nonOwner.address, amountMinted)
Expand Down
26 changes: 12 additions & 14 deletions packages/protocol/test/L2/TaikoL2.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { expect } from "chai"
import { ethers } from "hardhat"
import { TaikoL2 } from "../../typechain"

describe("TaikoL2", function () {
async function deployTaikoL2Fixture() {
// Deploying addressManager Contract
let taikoL2: TaikoL2

beforeEach(async function () {
const addressManager = await (
await ethers.getContractFactory("AddressManager")
).deploy()
Expand All @@ -14,19 +16,17 @@ describe("TaikoL2", function () {
await ethers.getContractFactory("LibTxDecoder")
).deploy()

const taikoL2Factory = await ethers.getContractFactory("TaikoL2", {
libraries: {
LibTxDecoder: libTxDecoder.address,
},
})
const taikoL2 = await taikoL2Factory.deploy(addressManager.address)

return { taikoL2 }
}
taikoL2 = await (
await ethers.getContractFactory("TaikoL2", {
libraries: {
LibTxDecoder: libTxDecoder.address,
},
})
).deploy(addressManager.address)
})

describe("anchor()", async function () {
it("should revert since ancestor hashes not written", async function () {
const { taikoL2 } = await deployTaikoL2Fixture()
await expect(
taikoL2.anchor(Math.ceil(Math.random() * 1024), randomBytes32())
).to.be.revertedWith("L2:publicInputHash")
Expand All @@ -35,15 +35,13 @@ describe("TaikoL2", function () {

describe("getLatestSyncedHeader()", async function () {
it("should be 0 because no headers have been synced", async function () {
const { taikoL2 } = await deployTaikoL2Fixture()
const hash = await taikoL2.getLatestSyncedHeader()
expect(hash).to.be.eq(ethers.constants.HashZero)
})
})

describe("getSyncedHeader()", async function () {
it("should be 0 because header number has not been synced", async function () {
const { taikoL2 } = await deployTaikoL2Fixture()
const hash = await taikoL2.getSyncedHeader(1)
expect(hash).to.be.eq(ethers.constants.HashZero)
})
Expand Down

0 comments on commit 19de694

Please sign in to comment.