Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

grc721_royalty_test.gno

2.01 Kb · 71 lines
 1package grc721
 2
 3import (
 4	"testing"
 5
 6	"gno.land/p/nt/testutils/v0"
 7	"gno.land/p/nt/uassert/v0"
 8)
 9
10func newDummyRoyaltyNFT(_ int, rlm realm) *royaltyNFT {
11	var nft *royaltyNFT
12	func(cur realm) {
13		nft = NewNFTWithRoyalty(0, cur, dummyNFTName, dummyNFTSymbol)
14	}(cross(rlm))
15	return nft
16}
17
18func TestSetTokenRoyalty(cur realm, t *testing.T) {
19	dummy := newDummyRoyaltyNFT(0, cur)
20	uassert.True(t, dummy != nil, "should not be nil")
21
22	addr1 := testutils.TestAddress("alice")
23	addr2 := testutils.TestAddress("bob")
24
25	paymentAddress := testutils.TestAddress("john")
26	percentage := int64(10) // 10%
27
28	salePrice := int64(1000)
29	expectRoyaltyAmount := int64(100)
30
31	dummy.mint(addr1, TokenID("1"))
32
33	derr := dummy.SetTokenRoyalty(addr1, TokenID("1"), RoyaltyInfo{
34		PaymentAddress: paymentAddress,
35		Percentage:     percentage,
36	})
37	uassert.NoError(t, derr, "Should not result in error")
38
39	// Test case: Invalid token ID
40	err := dummy.SetTokenRoyalty(addr1, TokenID("3"), RoyaltyInfo{
41		PaymentAddress: paymentAddress,
42		Percentage:     percentage,
43	})
44	uassert.ErrorIs(t, err, ErrInvalidTokenId)
45
46	// Test case: Invalid payment address
47	aerr := dummy.SetTokenRoyalty(addr1, TokenID("4"), RoyaltyInfo{
48		PaymentAddress: address("###"), // invalid address
49		Percentage:     percentage,
50	})
51	uassert.ErrorIs(t, aerr, ErrInvalidRoyaltyPaymentAddress)
52
53	// Test case: Invalid percentage
54	perr := dummy.SetTokenRoyalty(addr1, TokenID("5"), RoyaltyInfo{
55		PaymentAddress: paymentAddress,
56		Percentage:     int64(200), // over maxRoyaltyPercentage
57	})
58	uassert.ErrorIs(t, perr, ErrInvalidRoyaltyPercentage)
59
60	// addr2 is not the owner.
61	cerr := dummy.SetTokenRoyalty(addr2, TokenID("1"), RoyaltyInfo{
62		PaymentAddress: paymentAddress,
63		Percentage:     percentage,
64	})
65	uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)
66
67	dummyPaymentAddress, dummyRoyaltyAmount, rerr := dummy.RoyaltyInfo(TokenID("1"), salePrice)
68	uassert.NoError(t, rerr, "RoyaltyInfo error")
69	uassert.Equal(t, paymentAddress, dummyPaymentAddress)
70	uassert.Equal(t, expectRoyaltyAmount, dummyRoyaltyAmount)
71}