@@ -36,6 +36,9 @@ contract ERC721DynamicTraitsTest is Test {
3636 bytes32 key = bytes32 ("testKey " );
3737 bytes32 value = bytes32 ("foo " );
3838 uint256 tokenId = 12345 ;
39+
40+ // Register the trait key before using it.
41+ token.registerTraitKey (key);
3942 token.mint (address (this ), tokenId);
4043
4144 vm.expectEmit (true , true , true , true );
@@ -47,16 +50,23 @@ contract ERC721DynamicTraitsTest is Test {
4750 }
4851
4952 function testOnlyOwnerCanSetValues () public {
53+ bytes32 key = bytes32 ("test " );
54+ // Register the trait key before testing access control.
55+ token.registerTraitKey (key);
56+
5057 address alice = makeAddr ("alice " );
5158 vm.prank (alice);
5259 vm.expectRevert (abi.encodeWithSelector (Ownable.OwnableUnauthorizedAccount.selector , alice));
53- token.setTrait (0 , bytes32 ( " test " ) , bytes32 ("test " ));
60+ token.setTrait (0 , key , bytes32 ("test " ));
5461 }
5562
5663 function testSetTrait_Unchanged () public {
5764 bytes32 key = bytes32 ("testKey " );
5865 bytes32 value = bytes32 ("foo " );
5966 uint256 tokenId = 1 ;
67+
68+ // Register the trait key before using it.
69+ token.registerTraitKey (key);
6070 token.mint (address (this ), tokenId);
6171
6272 token.setTrait (tokenId, key, value);
@@ -70,6 +80,10 @@ contract ERC721DynamicTraitsTest is Test {
7080 bytes32 value1 = bytes32 ("foo " );
7181 bytes32 value2 = bytes32 ("bar " );
7282 uint256 tokenId = 1 ;
83+
84+ // Register the trait keys before using them.
85+ token.registerTraitKey (key1);
86+ token.registerTraitKey (key2);
7387 token.mint (address (this ), tokenId);
7488
7589 token.setTrait (tokenId, key1, value1);
@@ -99,6 +113,9 @@ contract ERC721DynamicTraitsTest is Test {
99113 bytes32 value = bytes32 (uint256 (1 ));
100114 uint256 tokenId = 1 ;
101115
116+ // Register the trait key so we can test token non-existence.
117+ token.registerTraitKey (key);
118+
102119 vm.expectRevert (abi.encodeWithSelector (IERC721Errors .ERC721NonexistentToken .selector , tokenId));
103120 token.setTrait (tokenId, key, value);
104121
@@ -112,6 +129,9 @@ contract ERC721DynamicTraitsTest is Test {
112129 function testGetTraitValue_DefaultZeroValue () public {
113130 bytes32 key = bytes32 ("testKey " );
114131 uint256 tokenId = 1 ;
132+
133+ // Register the trait key before using it.
134+ token.registerTraitKey (key);
115135 token.mint (address (this ), tokenId);
116136
117137 bytes32 value = token.getTraitValue (tokenId, key);
@@ -120,4 +140,26 @@ contract ERC721DynamicTraitsTest is Test {
120140 bytes32 [] memory values = token.getTraitValues (tokenId, Solarray.bytes32s (key));
121141 assertEq (values[0 ], bytes32 (0 ), "should return bytes32(0) " );
122142 }
143+
144+ function testGetTraitValue_UnregisteredTraitKey () public {
145+ bytes32 key = bytes32 ("unregisteredKey " );
146+ uint256 tokenId = 1 ;
147+ token.mint (address (this ), tokenId);
148+
149+ vm.expectRevert (abi.encodeWithSelector (IERC7496 .TraitDoesNotExist.selector , key));
150+ token.getTraitValue (tokenId, key);
151+
152+ vm.expectRevert (abi.encodeWithSelector (IERC7496 .TraitDoesNotExist.selector , key));
153+ token.getTraitValues (tokenId, Solarray.bytes32s (key));
154+ }
155+
156+ function testSetTrait_UnregisteredTraitKey () public {
157+ bytes32 key = bytes32 ("unregisteredKey " );
158+ bytes32 value = bytes32 ("foo " );
159+ uint256 tokenId = 1 ;
160+ token.mint (address (this ), tokenId);
161+
162+ vm.expectRevert (abi.encodeWithSelector (IERC7496 .TraitDoesNotExist.selector , key));
163+ token.setTrait (tokenId, key, value);
164+ }
123165}
0 commit comments