Skip to content

Instantly share code, notes, and snippets.

@batyanko
Created October 27, 2021 10:57
Show Gist options
  • Save batyanko/fbe454747a6420bb1580d50ca852da2c to your computer and use it in GitHub Desktop.
Save batyanko/fbe454747a6420bb1580d50ca852da2c to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.7+commit.e28d00a7.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./Library.sol";
contract LibraryTest is Library {
// Run all test functions in sequence, after initializing library as following.
function TestInitLibrary() public {
uint firstBook = 9781234567890;
for(uint i = 0; i < 10; i++) {
addBook(firstBook + i, 2);
}
}
// Run this from a first sender.
function TestScenario1() public {
uint[] memory books = viewBooks();
Assert.equal(books[5], uint(9781234567895), "Fifth book should be 9781234567895.");
Assert.equal(books.length, uint(10), "There should be 10 available books.");
borrowBook(9781234567895);
books = viewBooks();
Assert.equal(books[5], uint(9781234567896), "Fifth available book should now be 9781234567896, as we already got 9781234567895.");
Assert.equal(books.length, uint(9), "There should now be 9 available books left to borrow.");
borrowBook(9781234567899);
returnBook(9781234567899);
Assert.equal(books[8], uint(9781234567899), "Last available book should now be 9781234567899, as borrowed and returned it.");
Assert.equal(books.length, uint(9), "There should still be 9 available books left to borrow.");
}
// Run this from a second sender. Test independent interaction with more users.
function TestScenario2() public {
uint[] memory books = viewBooks();
Assert.equal(books[5], uint(9781234567895), "Fifth book should be 9781234567895.");
Assert.equal(books.length, uint(10), "There should be 10 available books.");
borrowBook(9781234567895);
books = viewBooks();
Assert.equal(books[5], uint(9781234567896), "Fifth available book should now be 9781234567896, as we already got 9781234567895.");
Assert.equal(books.length, uint(9), "There should now be 9 available books left to borrow.");
}
// Run this from a third sender. Test depletion of a book by previous users.
function TestScenario3() public {
uint[] memory books = viewBooks();
Assert.equal(books[5], uint(9781234567896), "Fifth book should be 9781234567896, as the previous 2 senders depleted 9781234567895.");
Assert.equal(books.length, uint(9), "There should be 9 available books.");
borrowBook(9781234567899);
}
function TestHistory() public {
Assert.equal(borrowHistory(9781234567895).length, uint(2), "9781234567895 should have been borrowed by a total of 2 users");
Assert.equal(borrowHistory(9781234567899).length, uint(2), "9781234567899 should have been borrowed by a total of 2 users");
}
// TODO Find way to test failing transactions.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment