Skip to content

Commit c5b8795

Browse files
committed
Add basic features
1 parent 6de579c commit c5b8795

File tree

5 files changed

+91
-57
lines changed

5 files changed

+91
-57
lines changed

script/Counter.s.sol

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/CommitmentDevice.sol

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.24;
3+
4+
contract CommitmentDevice {
5+
error CommitmentDevice__IndexOutOfBounds(uint256 index);
6+
7+
uint256 public number;
8+
string[] public commitments;
9+
bool[] private _hasAchieved;
10+
uint256 public numberOfCommitments;
11+
12+
constructor() {
13+
number = 0;
14+
numberOfCommitments = 0;
15+
}
16+
17+
function setNumber(uint256 newNumber) public {
18+
number = newNumber;
19+
}
20+
21+
function increment() public {
22+
number++;
23+
}
24+
25+
function addCommitment(string memory commitment) public {
26+
commitments.push(commitment);
27+
_hasAchieved.push(false); // Is this needed? Can remove if gas saved.
28+
numberOfCommitments++;
29+
}
30+
31+
function editCommitment(uint256 index, string memory newCommitment) public {
32+
if (index < 0 || index >= numberOfCommitments) revert CommitmentDevice__IndexOutOfBounds(index);
33+
commitments[index] = newCommitment;
34+
}
35+
36+
function removeCommitment(uint256 index) public {
37+
if (index < 0 || index >= numberOfCommitments) revert CommitmentDevice__IndexOutOfBounds(index);
38+
commitments[index] = commitments[numberOfCommitments - 1];
39+
commitments.pop();
40+
_hasAchieved[index] = _hasAchieved[numberOfCommitments - 1];
41+
_hasAchieved.pop();
42+
numberOfCommitments--;
43+
}
44+
45+
function _hasAchievedCommitment(uint256 index) public view returns (bool) {
46+
if (index < 0 || index >= numberOfCommitments) revert CommitmentDevice__IndexOutOfBounds(index);
47+
return _hasAchieved[index];
48+
}
49+
}

src/Counter.sol

Lines changed: 0 additions & 14 deletions
This file was deleted.

test/CommitmentDevice.sol

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.24;
3+
4+
import {Test, console} from "forge-std/Test.sol";
5+
import {CommitmentDevice} from "src/CommitmentDevice.sol";
6+
7+
contract CounterTest is Test {
8+
CommitmentDevice public c;
9+
10+
function setUp() public {
11+
c = new CommitmentDevice();
12+
// c.setNumber(0);
13+
}
14+
15+
function test_Increment() public {
16+
c.increment();
17+
assertEq(c.number(), 1);
18+
}
19+
20+
function testFuzz_SetNumber(uint256 x) public {
21+
c.setNumber(x);
22+
assertEq(c.number(), x);
23+
}
24+
25+
function test_AddCommitment() public {
26+
c.addCommitment("commitment1");
27+
assertEq(c.numberOfCommitments(), 1);
28+
}
29+
30+
function test_EditCommitment() public {
31+
c.addCommitment("commitment1");
32+
c.editCommitment(0, "commitment2");
33+
assertEq(c.numberOfCommitments(), 1);
34+
assertEq(c.commitments(0), "commitment2");
35+
}
36+
37+
function test_RemoveCommitment() public {
38+
c.addCommitment("commitment1");
39+
c.removeCommitment(0);
40+
assertEq(c.numberOfCommitments(), 0);
41+
}
42+
}

test/Counter.t.sol

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)