Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: manipulate entire context instead of TestSetXXX #3582

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ func TestAuctionEnd(t *testing.T) {

// Auction ends
highestBid = 3
std.TestSkipHeights(500)
t.SkipHeights(500)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
25 changes: 13 additions & 12 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-13.gno
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ func TestFull(t *testing.T) {

// Send two or more types of coins
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))

t.SetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)
}

// Send less than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)
}

// Send more than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, pendingReturns.Size(), 0)
Expand All @@ -42,13 +43,13 @@ func TestFull(t *testing.T) {
{

// Send less amount than the current highest bid (current: 1)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// Send more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, highestBid, 2)
Expand All @@ -60,11 +61,11 @@ func TestFull(t *testing.T) {

// Auction ends
{
std.TestSkipHeights(150)
t.SkipHeights(150)
shouldPanic(t, AuctionEnd)
shouldEqual(t, ended, false)

std.TestSkipHeights(301)
t.SkipHeights(301)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
24 changes: 12 additions & 12 deletions docs/assets/how-to-guides/porting-solidity-to-gno/porting-6.gno
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
// Bid Function Test - Send Coin
func TestBidCoins(t *testing.T) {
// Sending two types of coins
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)

// Sending lower amount than the current highest bid
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)

// Sending more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
}

// Bid Function Test - Bid by two or more people
func TestBidCoins(t *testing.T) {
// bidder01 bidding with 1 coin
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 1)
shouldEqual(t, highestBidder, bidder01)
shouldEqual(t, pendingReturns.Size(), 0)

// bidder02 bidding with 1 coin
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// bidder02 bidding with 2 coins
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 2)
shouldEqual(t, highestBidder, bidder02)
Expand Down
51 changes: 26 additions & 25 deletions docs/how-to-guides/porting-solidity-to-gno.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,39 +387,40 @@ func Bid() {
// Bid Function Test - Send Coin
func TestBidCoins(t *testing.T) {
// Sending two types of coins
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))

t.SetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)

// Sending lower amount than the current highest bid
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)

// Sending more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
}

// Bid Function Test - Bid by two or more people
func TestBidCoins(t *testing.T) {
// bidder01 bidding with 1 coin
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 1)
shouldEqual(t, highestBidder, bidder01)
shouldEqual(t, pendingReturns.Size(), 0)

// bidder02 bidding with 1 coin
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// bidder02 bidding with 2 coins
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)
shouldEqual(t, highestBid, 2)
shouldEqual(t, highestBidder, bidder02)
Expand Down Expand Up @@ -582,7 +583,7 @@ func TestAuctionEnd(t *testing.T) {

// Auction ends
highestBid = 3
std.TestSkipHeights(500)
t.SkipHeights(500)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down Expand Up @@ -620,22 +621,22 @@ func TestFull(t *testing.T) {

// Send two or more types of coins
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}, {"test", 1}}, nil)
shouldPanic(t, Bid)
}

// Send less than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 0}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 0}}, nil)
shouldPanic(t, Bid)
}

// Send more than the highest bid
{
std.TestSetOrigCaller(bidder01)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder01))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, pendingReturns.Size(), 0)
Expand All @@ -647,13 +648,13 @@ func TestFull(t *testing.T) {
{

// Send less amount than the current highest bid (current: 1)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 1}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 1}}, nil)
shouldPanic(t, Bid)

// Send more amount than the current highest bid (exceeded)
std.TestSetOrigCaller(bidder02)
std.TestSetOrigSend(std.Coins{{"ugnot", 2}}, nil)
t.SetPreviousRealm(std.NewUserRealm(bidder02))
t.SetOriginSend(std.Coins{{"ugnot", 2}}, nil)
shouldNoPanic(t, Bid)

shouldEqual(t, highestBid, 2)
Expand All @@ -665,11 +666,11 @@ func TestFull(t *testing.T) {

// Auction ends
{
std.TestSkipHeights(150)
t.SkipHeights(150)
shouldPanic(t, AuctionEnd)
shouldEqual(t, ended, false)

std.TestSkipHeights(301)
t.SkipHeights(301)
shouldNoPanic(t, AuctionEnd)
shouldEqual(t, ended, true)

Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/p/demo/entropy/entropy_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestInstanceValue(t *testing.T) {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}

std.TestSkipHeights(1)
t.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue(t, differentHeightEntropy)

Expand All @@ -44,7 +44,7 @@ func TestInstanceValue64(t *testing.T) {
t.Errorf("should have the same result: new=%s, base=%s", sameHeightResult, baseResult)
}

std.TestSkipHeights(1)
t.SkipHeights(1)
differentHeightEntropy := New()
differentHeightResult := computeValue64(t, differentHeightEntropy)

Expand Down
3 changes: 2 additions & 1 deletion examples/gno.land/p/demo/entropy/z_filetest.gno
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"std"
"testing"

"gno.land/p/demo/entropy"
)
Expand All @@ -27,7 +28,7 @@ func main() {
println(r.Value())
println(r.Value64())

std.TestSkipHeights(1)
testing.SkipHeights(1)
println("---")
r = entropy.New()
println(r.Value())
Expand Down
4 changes: 2 additions & 2 deletions examples/gno.land/p/demo/grc/grc20/tellers_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ func TestCallerTeller(t *testing.T) {
checkBalances(1000, 0, 0)
checkAllowances(0, 0, 0, 0, 0, 0)

std.TestSetOrigCaller(alice)
t.SetPreviousRealm(std.NewUserRealm((alice)))
urequire.NoError(t, teller.Approve(bob, 600))
checkBalances(1000, 0, 0)
checkAllowances(600, 0, 0, 0, 0, 0)

std.TestSetOrigCaller(bob)
t.SetPreviousRealm(std.NewUserRealm((bob)))
urequire.Error(t, teller.TransferFrom(alice, carl, 700))
checkBalances(1000, 0, 0)
checkAllowances(600, 0, 0, 0, 0, 0)
Expand Down
6 changes: 3 additions & 3 deletions examples/gno.land/p/demo/grc/grc721/basic_nft_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestSetTokenURI(t *testing.T) {
addr2 := std.Address("g1us8428u2a5satrlxzagqqa5m6vmuze025anjlj")
tokenURI := "http://example.com/token"

std.TestSetOrigCaller(std.Address(addr1)) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

dummy.mint(addr1, TokenID("1"))
_, derr := dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI))
Expand All @@ -269,13 +269,13 @@ func TestSetTokenURI(t *testing.T) {
_, err := dummy.SetTokenURI(TokenID("3"), TokenURI(tokenURI))
uassert.ErrorIs(t, err, ErrInvalidTokenId)

std.TestSetOrigCaller(std.Address(addr2)) // addr2
t.SetPreviousRealm(std.NewUserRealm(addr2)) // addr2

_, cerr := dummy.SetTokenURI(TokenID("1"), TokenURI(tokenURI)) // addr2 trying to set URI for token 1
uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)

// Test case: Retrieving TokenURI
std.TestSetOrigCaller(std.Address(addr1)) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

dummyTokenURI, err := dummy.TokenURI(TokenID("1"))
uassert.NoError(t, err, "TokenURI error")
Expand Down
6 changes: 3 additions & 3 deletions examples/gno.land/p/demo/grc/grc721/grc721_metadata_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestSetMetadata(t *testing.T) {
youtubeURL := "test"

// Set the original caller to addr1
std.TestSetOrigCaller(addr1) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

// Mint a new token for addr1
dummy.mint(addr1, TokenID("1"))
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestSetMetadata(t *testing.T) {
uassert.ErrorIs(t, err, ErrInvalidTokenId)

// Set the original caller to addr2
std.TestSetOrigCaller(addr2) // addr2
t.SetPreviousRealm(std.NewUserRealm(addr2)) // addr2

// Try to set metadata for token 1 from addr2 (should fail)
cerr := dummy.SetTokenMetadata(TokenID("1"), Metadata{
Expand All @@ -88,7 +88,7 @@ func TestSetMetadata(t *testing.T) {
uassert.ErrorIs(t, cerr, ErrCallerIsNotOwner)

// Set the original caller back to addr1
std.TestSetOrigCaller(addr1) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

// Retrieve metadata for token 1
dummyMetadata, err := dummy.TokenMetadata(TokenID("1"))
Expand Down
6 changes: 3 additions & 3 deletions examples/gno.land/p/demo/grc/grc721/grc721_royalty_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestSetTokenRoyalty(t *testing.T) {
salePrice := uint64(1000)
expectRoyaltyAmount := uint64(100)

std.TestSetOrigCaller(addr1) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

dummy.mint(addr1, TokenID("1"))

Expand All @@ -38,7 +38,7 @@ func TestSetTokenRoyalty(t *testing.T) {
})
uassert.ErrorIs(t, derr, ErrInvalidTokenId)

std.TestSetOrigCaller(addr2) // addr2
t.SetPreviousRealm(std.NewUserRealm(addr2)) // addr2

cerr := dummy.SetTokenRoyalty(TokenID("1"), RoyaltyInfo{
PaymentAddress: paymentAddress,
Expand All @@ -61,7 +61,7 @@ func TestSetTokenRoyalty(t *testing.T) {
uassert.ErrorIs(t, perr, ErrInvalidRoyaltyPercentage)

// Test case: Retrieving Royalty Info
std.TestSetOrigCaller(addr1) // addr1
t.SetPreviousRealm(std.NewUserRealm(addr1)) // addr1

dummyPaymentAddress, dummyRoyaltyAmount, rerr := dummy.RoyaltyInfo(TokenID("1"), salePrice)
uassert.NoError(t, rerr, "RoyaltyInfo error")
Expand Down
3 changes: 1 addition & 2 deletions examples/gno.land/p/demo/membstore/membstore_test.gno
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package membstore

import (
"testing"

"std"
"testing"

"gno.land/p/demo/testutils"
"gno.land/p/demo/uassert"
Expand Down
Loading
Loading