From 9f35564348ebc3ee8c34ada78b46658a85390a1b Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:18:19 +0100 Subject: [PATCH 1/7] test: test retrieve --- test/JustCounter.js | 74 +++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index 708e8ed..29e371f 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -2,41 +2,49 @@ const { loadFixture } = require("@nomicfoundation/hardhat-network-helpers"); const { expect } = require("chai"); describe("JustCounter Test Suite", function () { - // define loadFixture - // fixtures can return anything you consider useful for your tests - const deployTokenFixture = async () => { - const [owner, addr1, addr2] = await ethers.getSigners(); - const JustCounter = await ethers.deployContract("JustCounter"); - return { JustCounter, owner, addr1, addr2 }; - } + // define loadFixture + // fixtures can return anything you consider useful for your tests + const deployTokenFixture = async () => { + const [owner, addr1, addr2] = await ethers.getSigners(); + const JustCounter = await ethers.deployContract("JustCounter"); + return { JustCounter, owner, addr1, addr2 }; + }; - describe("Post Deployment State Variables", async () => { - it("Should return state variables", async () => { - const { JustCounter } = await loadFixture(deployTokenFixture); - expect(await JustCounter.count()).to.equal(0); - expect(await JustCounter.underCount()).to.equal(0); - }) - }) - - describe.only("State Variables Changes", async () => { - it("Should store number", async () => { - let amount = 5 - // get loadFixture variables - const { JustCounter } = await loadFixture(deployTokenFixture); - // get current state variable count - let count1 = await JustCounter.count() - console.log("count before state change___", count1) - // write assertion statement for count1 - expect(count1).to.equal(0); - - // store count (transaction) - await JustCounter.store(amount) - let count2 = await JustCounter.count() - // write assertion statement for count after store txn - expect(count2).to.equal(amount); - }) - }) + describe("Post Deployment State Variables", async () => { + it("Should return state variables", async () => { + const { JustCounter } = await loadFixture(deployTokenFixture); + expect(await JustCounter.count()).to.equal(0); + expect(await JustCounter.underCount()).to.equal(0); + }); + }); + describe("State Variables Changes", async () => { + it("Should store number", async () => { + let amount = 5; + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable count + let count1 = await JustCounter.count(); + console.log("count before state change___", count1); + // write assertion statement for count1 + expect(count1).to.equal(0); + // store count (transaction) + await JustCounter.store(amount); + let count2 = await JustCounter.count(); + // write assertion statement for count after store txn + expect(count2).to.equal(amount); + }); + it.only("Should retrieve number", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable count + let count = await JustCounter.retrieve(); + console.log("count before state change___", count); + // write assertion statement for count + expect(count).to.be.instanceOf(ethers.BigNumber); + expect(count).to.equal(0); + }); + }); }); From c082061c1a156c5e09b2698635c56711944a41a0 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:23:22 +0100 Subject: [PATCH 2/7] test: test increaseCount() --- test/JustCounter.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index 29e371f..43ac0be 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -36,7 +36,7 @@ describe("JustCounter Test Suite", function () { expect(count2).to.equal(amount); }); - it.only("Should retrieve number", async () => { + it("Should retrieve number", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable count @@ -46,5 +46,16 @@ describe("JustCounter Test Suite", function () { expect(count).to.be.instanceOf(ethers.BigNumber); expect(count).to.equal(0); }); + + it.only("Should increase count", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable count + console.log("count before state change___", JustCounter.count()); + // increase count + await JustCounter.increaseCount(); + // write assertion statement for count + expect(await JustCounter.count()).to.equal(1); + }); }); }); From 56107703e034888f85f930d2e62da0acbb33cabd Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:30:03 +0100 Subject: [PATCH 3/7] refac: modify decreaseCount() test: test decreaseCount() --- contracts/JustCounter.sol | 23 ++++++++++++----------- test/JustCounter.js | 13 ++++++++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/contracts/JustCounter.sol b/contracts/JustCounter.sol index bfc667e..76efdd2 100644 --- a/contracts/JustCounter.sol +++ b/contracts/JustCounter.sol @@ -8,7 +8,6 @@ pragma solidity >=0.8.2 <0.9.0; * @custom:dev-run-script ./scripts/deploy_with_ethers.ts */ contract JustCounter { - uint256 public count; int256 public underCount; @@ -21,19 +20,22 @@ contract JustCounter { } /** - * @dev Return value + * @dev Return value * @return value of 'number' */ - function retrieve() public view returns(uint256) { + function retrieve() public view returns (uint256) { return count; } - function increaseCount() public { count += 1; } - function decreaseCount() public { + function decreaseCount() public { + if (count == 0) { + return; + } + count -= 1; } @@ -43,16 +45,15 @@ contract JustCounter { return false; } - function increaseUnderCount() public { - underCount += 1; + underCount += 1; } function decreaseUnderCount() public { - underCount -= 1; + underCount -= 1; } - function getUnderCount() public view returns (int256){ + function getUnderCount() public view returns (int256) { return underCount; - } -} \ No newline at end of file + } +} diff --git a/test/JustCounter.js b/test/JustCounter.js index 43ac0be..2f6c0f9 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -47,7 +47,7 @@ describe("JustCounter Test Suite", function () { expect(count).to.equal(0); }); - it.only("Should increase count", async () => { + it("Should increase count", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable count @@ -57,5 +57,16 @@ describe("JustCounter Test Suite", function () { // write assertion statement for count expect(await JustCounter.count()).to.equal(1); }); + + it.only("Should decrease count", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable count + console.log("count before state change___", JustCounter.count()); + // increase count + await JustCounter.decreaseCount(); + // write assertion statement for count + expect(await JustCounter.count()).to.equal(0); + }); }); }); From 802ede9b1246c9bce30bbb4ba066740d54d8acae Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:51:13 +0100 Subject: [PATCH 4/7] test: test isCountEven() --- test/JustCounter.js | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index 2f6c0f9..41974f3 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -58,15 +58,32 @@ describe("JustCounter Test Suite", function () { expect(await JustCounter.count()).to.equal(1); }); - it.only("Should decrease count", async () => { + it("Should decrease count", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable count console.log("count before state change___", JustCounter.count()); - // increase count + // decrease count await JustCounter.decreaseCount(); // write assertion statement for count expect(await JustCounter.count()).to.equal(0); }); + + it.only("Should check even count", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable count + console.log("count before state change___", JustCounter.count()); + // check even count + let isCountEven = await JustCounter.isCountEven(); + // write assertion statement for count + expect(isCountEven).to.equal(true); + // increase count + await JustCounter.increaseCount(); + // check even count + isCountEven = await JustCounter.isCountEven(); + // write assertion statement for count + expect(isCountEven).to.equal(false); + }); }); }); From bf569cf6a0f59c556c45799fc5c5aed30ede9818 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:54:22 +0100 Subject: [PATCH 5/7] test: test increaseUnderCount() --- test/JustCounter.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index 41974f3..d3ff3ec 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -69,7 +69,7 @@ describe("JustCounter Test Suite", function () { expect(await JustCounter.count()).to.equal(0); }); - it.only("Should check even count", async () => { + it("Should check even count", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable count @@ -85,5 +85,19 @@ describe("JustCounter Test Suite", function () { // write assertion statement for count expect(isCountEven).to.equal(false); }); + + it.only("Should increase underCount", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable underCount + console.log( + "underCount before state change___", + JustCounter.underCount() + ); + // increase underCount + await JustCounter.increaseUnderCount(); + // write assertion statement for underCount + expect(await JustCounter.underCount()).to.equal(1); + }); }); }); From 1d0a12fd16e850a90a77ad6516fc92902985d252 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:55:26 +0100 Subject: [PATCH 6/7] test: test decreaseUnderCount --- test/JustCounter.js | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index d3ff3ec..378cc98 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -86,7 +86,7 @@ describe("JustCounter Test Suite", function () { expect(isCountEven).to.equal(false); }); - it.only("Should increase underCount", async () => { + it("Should increase underCount", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable underCount @@ -99,5 +99,19 @@ describe("JustCounter Test Suite", function () { // write assertion statement for underCount expect(await JustCounter.underCount()).to.equal(1); }); + + it.only("Should decrease underCount", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable underCount + console.log( + "underCount before state change___", + JustCounter.underCount() + ); + // increase underCount + await JustCounter.decreaseUnderCount(); + // write assertion statement for underCount + expect(await JustCounter.underCount()).to.equal(-1); + }); }); }); From d04b5fef2aa1395ca8694582ba0ff5bda8fbcb74 Mon Sep 17 00:00:00 2001 From: EjembiEmmanuel Date: Mon, 15 Apr 2024 12:57:54 +0100 Subject: [PATCH 7/7] test: test getUnderCount() --- test/JustCounter.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/JustCounter.js b/test/JustCounter.js index 378cc98..1c492af 100644 --- a/test/JustCounter.js +++ b/test/JustCounter.js @@ -36,7 +36,7 @@ describe("JustCounter Test Suite", function () { expect(count2).to.equal(amount); }); - it("Should retrieve number", async () => { + it("Should retrieve count", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable count @@ -100,7 +100,7 @@ describe("JustCounter Test Suite", function () { expect(await JustCounter.underCount()).to.equal(1); }); - it.only("Should decrease underCount", async () => { + it("Should decrease underCount", async () => { // get loadFixture variables const { JustCounter } = await loadFixture(deployTokenFixture); // get current state variable underCount @@ -108,10 +108,21 @@ describe("JustCounter Test Suite", function () { "underCount before state change___", JustCounter.underCount() ); - // increase underCount + // decrease underCount await JustCounter.decreaseUnderCount(); // write assertion statement for underCount expect(await JustCounter.underCount()).to.equal(-1); }); + + it.only("Should retrieve underCount", async () => { + // get loadFixture variables + const { JustCounter } = await loadFixture(deployTokenFixture); + // get current state variable underCount + let underCount = await JustCounter.getUnderCount(); + console.log("count before state change___", underCount); + // write assertion statement for count + expect(underCount).to.be.instanceOf(ethers.BigNumber); + expect(underCount).to.equal(0); + }); }); });