Skip to content

Commit

Permalink
add initial go tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Oct 26, 2023
1 parent e4fcf22 commit 74898bf
Show file tree
Hide file tree
Showing 8 changed files with 2,188 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: test
test:
$(MAKE) generate -C lib/go
$(MAKE) test -C lib/go

.PHONY: ci
ci:
$(MAKE) ci -C lib/go
11 changes: 11 additions & 0 deletions lib/go/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.PHONY: test
test:
$(MAKE) test -C contracts

.PHONY: generate
generate:
$(MAKE) generate -C contracts

.PHONY: ci
ci:
$(MAKE) ci -C contracts
15 changes: 15 additions & 0 deletions lib/go/contracts/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: test
test:
go test ./...

.PHONY: generate
generate:
go generate

.PHONY: check-tidy
check-tidy: generate
go mod tidy
git diff --exit-code

.PHONY: ci
ci: check-tidy test
69 changes: 69 additions & 0 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package contracts

//go:generate go run github.com/kevinburke/go-bindata/go-bindata -prefix ../../../contracts -o internal/assets/assets.go -pkg assets -nometadata -nomemcopy ../../../contracts

import (
"regexp"

_ "github.com/kevinburke/go-bindata"

"github.com/onflow/flow-go-sdk"

"github.com/onflow/random-coin-toss/lib/go/contracts/internal/assets"
)

var (
placeholderNonFungibleToken = regexp.MustCompile(`"NonFungibleToken"`)
placeholderFungibleToken = regexp.MustCompile(`"FungibleToken"`)
placeholderResolver = regexp.MustCompile(`"ViewResolver"`)
placeholderMetadataViews = regexp.MustCompile(`"MetadataViews"`)
placeholderRandomBeaconHistory = regexp.MustCompile(`"RandomBeaconHistory"`)
placeholderPseudoRandomGenerator = regexp.MustCompile(`"PseudoRandomGenerator"`)
placeholderCoinToss = regexp.MustCompile(`"CoinToss"`)

)

const (
filenameNonFungibleToken = "utility/NonFungibleToken.cdc"
filenameFungibleToken = "utility/FungibleToken.cdc"
filenameResolver = "utility/ViewResolver.cdc"
filenameMetadataViews = "utility/MetadataViews.cdc"
filenameRandomBeaconHistory = "RandomBeaconHistory.cdc"
filenamePseudoRandomGenerator = "PseudoRandomGenerator.cdc"
filenameCoinToss = "CoinToss.cdc"
)

// NonFungibleToken returns the NonFungibleToken contract interface.
func NonFungibleToken() []byte {
code := assets.MustAssetString(filenameNonFungibleToken)
return []byte(code)
}

// FungibleToken returns the FungibleToken contract interface.
func FungibleToken() []byte {
return assets.MustAsset(filenameFungibleToken)
}

func Resolver() []byte {
code := assets.MustAssetString(filenameResolver)

return []byte(code)
}

func MetadataViews(ftAddress, nftAddress, resolverAddress flow.Address) []byte {
code := assets.MustAssetString(filenameMetadataViews)

code = placeholderFungibleToken.ReplaceAllString(code, "0x"+ftAddress.String())
code = placeholderNonFungibleToken.ReplaceAllString(code, "0x"+nftAddress.String())
code = placeholderResolver.ReplaceAllString(code, "0x"+resolverAddress.String())

return []byte(code)
}

func RandomBeaconHistory() []byte {
return assets.MustAsset(filenameRandomBeaconHistory)
}

func PseudoRandomGenerator() []byte {
return assets.MustAsset(filenamePseudoRandomGenerator)
}
50 changes: 50 additions & 0 deletions lib/go/contracts/contracts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package contracts_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/onflow/flow-go-sdk/test"

"github.com/onflow/random-coin-toss/lib/go/contracts"
)

const addrA = "0x0A"

func TestNonFungibleTokenContract(t *testing.T) {
contract := contracts.NonFungibleToken()
assert.NotNil(t, contract)
}

func TestFungibleTokenContract(t *testing.T) {
contract := contracts.FungibleToken()
assert.NotNil(t, contract)
}

func TestViewResolver(t *testing.T) {
addresses := test.AddressGenerator()
contract := contracts.ViewResolver()
assert.NotNil(t, contract)
}

func TestMetadataViewsContract(t *testing.T) {
addresses := test.AddressGenerator()
addressA := addresses.New()
addressB := addresses.New()
addressC := addresses.New()
contract := contracts.MetadataViews(addressA, addressB, addressC)
assert.NotNil(t, contract)
}

func TestRandomBeaconHistory(t *testing.T) {
addresses := test.AddressGenerator()
contract := contracts.RandomBeaconHistory()
assert.NotNil(t, contract)
}

func TestRandomBeaconHistory(t *testing.T) {
addresses := test.AddressGenerator()
contract := contracts.PseudoRandomGenerator()
assert.NotNil(t, contract)
}
9 changes: 9 additions & 0 deletions lib/go/contracts/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/onflow/random-coin_toss/lib/go/contracts

go 1.16

require (
github.com/kevinburke/go-bindata v3.22.0+incompatible
github.com/onflow/flow-go-sdk v0.41.7-stable-cadence
github.com/stretchr/testify v1.8.2
)
1,705 changes: 1,705 additions & 0 deletions lib/go/contracts/go.sum

Large diffs are not rendered by default.

321 changes: 321 additions & 0 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

0 comments on commit 74898bf

Please sign in to comment.