From fc618eb7a708c95956c74f6e8600b74c03b61331 Mon Sep 17 00:00:00 2001 From: Vectorized Date: Fri, 2 Feb 2024 09:08:41 +0000 Subject: [PATCH] Add tests --- contracts/extensions/ERC721AQueryable.sol | 10 ++----- contracts/mocks/ERC721ASpotMock.sol | 4 +++ test/extensions/ERC721ASpot.test.js | 35 +++++++++++++++++++---- 3 files changed, 36 insertions(+), 13 deletions(-) diff --git a/contracts/extensions/ERC721AQueryable.sol b/contracts/extensions/ERC721AQueryable.sol index 7936b4f9..19802428 100644 --- a/contracts/extensions/ERC721AQueryable.sol +++ b/contracts/extensions/ERC721AQueryable.sol @@ -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. diff --git a/contracts/mocks/ERC721ASpotMock.sol b/contracts/mocks/ERC721ASpotMock.sol index af341485..3a764a86 100644 --- a/contracts/mocks/ERC721ASpotMock.sol +++ b/contracts/mocks/ERC721ASpotMock.sol @@ -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); } diff --git a/test/extensions/ERC721ASpot.test.js b/test/extensions/ERC721ASpot.test.js index 5c858560..9067bf19 100644 --- a/test/extensions/ERC721ASpot.test.js +++ b/test/extensions/ERC721ASpot.test.js @@ -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); @@ -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 () { @@ -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);