Skip to content

Commit

Permalink
Exposing function for test returning only test chainIds
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusz-sekara committed Sep 27, 2023
1 parent db5f26f commit 111d4d5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 8 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/smartcontractkit/chain-selectors

go 1.20

require gopkg.in/yaml.v3 v3.0.1
require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
gopkg.in/yaml.v3 v3.0.1
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
20 changes: 14 additions & 6 deletions selectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ var selectorsYml []byte
//go:embed test_selectors.yml
var testSelectorsYml []byte

var selectorsMap = parseYml(selectorsYml)
var testSelectorsMap = parseYml(testSelectorsYml)

var evmChainIdToChainSelector = loadAllSelectors()

func loadAllSelectors() map[uint64]uint64 {
selectors := parseYml(selectorsYml)
testSelectors := parseYml(testSelectorsYml)

output := make(map[uint64]uint64, len(selectors)+len(testSelectors))
for k, v := range selectors {
output := make(map[uint64]uint64, len(selectorsMap)+len(testSelectorsMap))
for k, v := range selectorsMap {
output[k] = v
}
for k, v := range testSelectors {
for k, v := range testSelectorsMap {
output[k] = v
}
return output
Expand Down Expand Up @@ -66,3 +66,11 @@ func SelectorFromChainId(chainId uint64) (uint64, error) {
}
return 0, fmt.Errorf("chain selector not found for chain %d", chainId)
}

func TestChainIds() []uint64 {
chainIds := make([]uint64, 0, len(testSelectorsMap))
for k := range testSelectorsMap {
chainIds = append(chainIds, k)
}
return chainIds
}
34 changes: 33 additions & 1 deletion selectors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
package chain_selectors

import "testing"
import (
"testing"

"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
)

func TestNoOverlapBetweenRealAndTestSelectors(t *testing.T) {
testChainSelectors := maps.Values(testSelectorsMap)

for k, v := range selectorsMap {
if _, exist := testSelectorsMap[k]; exist {
t.Error("Chain Ids should not overlap between real and test networks")
}

if slices.Contains(testChainSelectors, v) {
t.Error("Chain Selectors should not overlap between real and test networks")
}
}
}

func TestBothSelectorsYmlAndTestSelectorsYmlAreValid(t *testing.T) {
optimismGoerliSelector, err := SelectorFromChainId(420)
Expand Down Expand Up @@ -74,3 +93,16 @@ func TestSelectorFromChainId(t *testing.T) {
t.Error("Should return correct chain selector id")
}
}

func TestTestChainIds(t *testing.T) {
chainIds := TestChainIds()
if len(chainIds) != len(testSelectorsMap) {
t.Error("Should return correct number of test chain ids")
}

for _, chainId := range chainIds {
if _, exist := testSelectorsMap[chainId]; !exist {
t.Error("Should return correct test chain ids")
}
}
}

0 comments on commit 111d4d5

Please sign in to comment.