Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Feb 2, 2024
1 parent 192caf0 commit fc618eb
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
10 changes: 3 additions & 7 deletions contracts/extensions/ERC721AQueryable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,14 @@ abstract contract ERC721AQueryable is ERC721A, IERC721AQueryable {
start = _startTokenId();
}
uint256 nextTokenId = _nextTokenId();
uint256 stopLimit = _sequentialUpTo() != type(uint256).max ? type(uint256).max : nextTokenId;
uint256 stopLimit = _sequentialUpTo() != type(uint256).max ? stop : nextTokenId;
// Set `stop = min(stop, stopLimit)`.
if (stop >= stopLimit) {
stop = stopLimit;
}
uint256[] memory tokenIds;
uint256 tokenIdsMaxLength = balanceOf(owner);
bool startLtStop = start < stop;
assembly {
// Set `tokenIdsMaxLength` to zero if `start` is less than `stop`.
tokenIdsMaxLength := mul(tokenIdsMaxLength, startLtStop)
}
uint256 tokenIdsMaxLength;
if (start < stop) tokenIdsMaxLength = balanceOf(owner);
if (tokenIdsMaxLength != 0) {
// Set `tokenIdsMaxLength = min(balanceOf(owner), stop - start)`,
// to cater for cases where `balanceOf(owner)` is too big.
Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/ERC721ASpotMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ contract ERC721ASpotMock is StartTokenIdHelper, SequentialUpToHelper, ERC721AQue
return _exists(tokenId);
}

function getOwnershipOf(uint256 index) public view returns (TokenOwnership memory) {
return _ownershipOf(index);
}

function safeMintSpot(address to, uint256 tokenId) public {
_safeMintSpot(to, tokenId);
}
Expand Down
35 changes: 29 additions & 6 deletions test/extensions/ERC721ASpot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ describe('ERC721ASpot', function () {
this.erc721aSpot = await deployContract('ERC721ASpotMock', args);
});

it('_mintSpot emits a Transfer event', async function () {
await expect(this.erc721aSpot.safeMintSpot(this.addr1.address, 20))
.to.emit(this.erc721aSpot, 'Transfer')
.withArgs(ZERO_ADDRESS, this.addr1.address, 20);
});

it('increases _totalSpotMinted, totalSupply', async function () {
await this.erc721aSpot.safeMint(this.addr1.address, 5);
expect(await this.erc721aSpot.totalSpotMinted()).to.eq(0);
Expand All @@ -75,29 +81,37 @@ describe('ERC721ASpot', function () {
});

it('tokensOfOwnerIn', async function () {
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295)).to.eql([]);

await this.erc721aSpot.safeMint(this.addr1.address, 5);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14].map(BigNumber.from));

await this.erc721aSpot.safeMintSpot(this.addr1.address, 21);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14, 21].map(BigNumber.from));

await this.erc721aSpot.safeMintSpot(this.addr1.address, 31);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14, 21, 31].map(BigNumber.from));

await this.erc721aSpot.safeMintSpot(this.addr1.address, 22);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14, 21, 22, 31].map(BigNumber.from));

await this.erc721aSpot.safeMint(this.addr1.address, 5);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 22, 31].map(BigNumber.from));

await this.erc721aSpot.safeMintSpot(this.addr1.address, 20);
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 50))
expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 4294967295))
.to.eql([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 31].map(BigNumber.from));

expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 32))
.to.eql([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 31].map(BigNumber.from));

expect(await this.erc721aSpot.tokensOfOwnerIn(this.addr1.address, 0, 31))
.to.eql([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22].map(BigNumber.from));
});

it('explicitOwnershipOf', async function () {
Expand Down Expand Up @@ -175,6 +189,15 @@ describe('ERC721ASpot', function () {
await this.erc721aSpot.safeMintSpot(this.addr1.address, 30);
});

it.only('sets ownership correctly', async function () {
const tokenIds = [10,11,12,13,14,20,30];
for (let i = 0; i < 35; ++i) {
const tx = this.erc721aSpot.getOwnershipOf(i);
if (tokenIds.includes(i)) await tx;
else await expect(tx).to.be.revertedWith('OwnerQueryForNonexistentToken');
}
});

it('reduces balanceOf, totalSupply', async function () {
expect(await this.erc721aSpot.balanceOf(this.addr1.address)).to.eq(7);
await this.erc721aSpot.connect(this.addr1).burn(10);
Expand Down

0 comments on commit fc618eb

Please sign in to comment.