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 d7e7c50
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 7 deletions.
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
}
36 changes: 35 additions & 1 deletion selectors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
package chain_selectors

import "testing"
import (
"testing"
)

func TestNoSameChainSelectorsAreGenerated(t *testing.T) {
chainSelectors := map[uint64]bool{}

for k, v := range evmChainIdToChainSelector {
if _, exist := chainSelectors[v]; exist {
t.Errorf("Chain Selectors should be unique. Selector %d is duplicated for chain %d", v, k)
}
chainSelectors[v] = true
}
}

func TestNoOverlapBetweenRealAndTestChains(t *testing.T) {
for k, _ := range selectorsMap {
if _, exist := testSelectorsMap[k]; exist {
t.Errorf("Chain %d is duplicated between real and test chains", k)
}
}
}

func TestBothSelectorsYmlAndTestSelectorsYmlAreValid(t *testing.T) {
optimismGoerliSelector, err := SelectorFromChainId(420)
Expand Down Expand Up @@ -74,3 +95,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 d7e7c50

Please sign in to comment.