Skip to content

Commit

Permalink
Adding tests for Pausable
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Stefanov <[email protected]>
  • Loading branch information
stefan-stefanooov committed Nov 6, 2023
1 parent 271fa2e commit 44503c4
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contracts/solidity/errors/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ contract Errors {
function tryCatchWithErrorMessageRevert(string memory message) external returns (int value, bool success) {
try errorsExternal.revertWithErrorMessage(message) returns (bool v) {
return (1, v);
} catch Error(string memory message) {
emit Result(0, message);
} catch Error(string memory _message) {
emit Result(0, _message);
}
}

Expand Down
24 changes: 24 additions & 0 deletions contracts/solidity/oz/Pausable/Pausable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "@openzeppelin/contracts/utils/Pausable.sol";

contract PausableTest is Pausable {
string public message;

function setPausedMessage(string calldata _message) external whenNotPaused {
message = _message;
}

function getPausedMessage() external view whenPaused returns (string memory) {
return message;
}

function pause() external {
_pause();
}

function unpause() external {
_unpause();
}
}
101 changes: 101 additions & 0 deletions test/solidity/oz/pausable/pausable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

const { expect } = require('chai')
const { ethers } = require('hardhat')

describe('@solidityequiv3 Pausable', function () {
let signers,wallet
let contract
const CALL_EXCEPTION = 'CALL_EXCEPTION'

before(async function () {
signers = await ethers.getSigners()
wallet = signers[0]

const factory = await ethers.getContractFactory('PausableTest')
contract = await factory.deploy()
await contract.deployed()
})

it('should BE able to call function "setPausedMessage" with "whenNotPaused" modifier when unpaused', async function () {
const tx = await contract.setPausedMessage("Hello World")
await tx.wait()
const messate = await contract.message()

expect(messate).to.equal("Hello World")
})

it('should NOT be able to call function "setPausedMessage" with "whenNotPaused" modifier when paused', async function () {
await contract.pause()
const tx = await contract.setPausedMessage("Hello World")

expect(tx.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should BE able to call function "getPausedMessage" with "whenNotPaused" modifier when unpaused', async function () {
expect(await contract.getPausedMessage()).to.be.equal("Hello World")
})

it('should NOT be able to call function "getPausedMessage" with "whenNotPaused" modifier when paused', async function () {
await contract.unpause()

expect(contract.getPausedMessage()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should fire event when Paused', async function () {
const tx = await contract.pause()
const rec = await tx.wait()
const event = rec.events[0]
const account = event.args.account


expect(event.event).to.be.equal("Paused")
expect(account).to.be.equal(wallet.address)
})

it('should fire event when Unpaused', async function () {
const tx = await contract.unpause()
const rec = await tx.wait()
const event = rec.events[0]
const account = event.args.account


expect(event.event).to.be.equal("Unpaused")
expect(account).to.be.equal(wallet.address)
})

it('should Not be able to pause when paused', async function () {
const tx = await contract.pause()
await tx.wait()
const tx2 = await contract.pause()

expect(tx2.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

it('should Not be able to Unpause when Unpaused', async function () {
const tx = await contract.unpause()
await tx.wait()
const tx2 = await contract.unpause()

expect(tx2.wait()).to.eventually.be.rejected.and.have.property('code', CALL_EXCEPTION)
})

})

0 comments on commit 44503c4

Please sign in to comment.