github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/model/flow/role_test.go (about) 1 package flow_test 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/onflow/flow-go/model/flow" 11 ) 12 13 func TestRoleJSON(t *testing.T) { 14 r := flow.RoleCollection 15 bz, err := json.Marshal(r) 16 assert.NoError(t, err) 17 assert.Equal(t, fmt.Sprintf("\"%v\"", r), string(bz)) 18 var actual flow.Role 19 err = json.Unmarshal(bz, &actual) 20 assert.NoError(t, err) 21 assert.Equal(t, r, actual) 22 } 23 24 // TestRoleList_Contains evaluates correctness of Contains method of RoleList. 25 func TestRoleList_Contains(t *testing.T) { 26 roleList := flow.RoleList{flow.RoleConsensus, flow.RoleVerification} 27 28 // asserts Contains returns true for roles in the list 29 assert.True(t, roleList.Contains(flow.RoleConsensus)) 30 assert.True(t, roleList.Contains(flow.RoleVerification)) 31 32 // asserts Contains returns false for roles not in the list 33 assert.False(t, roleList.Contains(flow.RoleAccess)) 34 assert.False(t, roleList.Contains(flow.RoleExecution)) 35 assert.False(t, roleList.Contains(flow.RoleCollection)) 36 37 } 38 39 // TestRoleList_Union evaluates correctness of Union method of RoleList. 40 func TestRoleList_Union(t *testing.T) { 41 this := flow.RoleList{flow.RoleConsensus, flow.RoleVerification} 42 other := flow.RoleList{flow.RoleConsensus, flow.RoleExecution} 43 44 union := this.Union(other) 45 46 // asserts length of role lists 47 assert.Len(t, union, 3) 48 assert.Len(t, this, 2) 49 assert.Len(t, other, 2) 50 51 // asserts content of role lists 52 // this 53 assert.Contains(t, this, flow.RoleConsensus) 54 assert.Contains(t, this, flow.RoleVerification) 55 // other 56 assert.Contains(t, other, flow.RoleConsensus) 57 assert.Contains(t, other, flow.RoleExecution) 58 // union 59 assert.Contains(t, union, flow.RoleConsensus) 60 assert.Contains(t, union, flow.RoleVerification) 61 assert.Contains(t, union, flow.RoleExecution) 62 } 63 64 // TestRoleList_ID evaluates that corresponding identifier of a role list is unique with its content, and 65 // does not depend on the order of its element 66 func TestRoleList_ID(t *testing.T) { 67 // identifier of a role list should be the same as long as its element are the same 68 // regardless of order of its elements 69 this := flow.RoleList{flow.RoleConsensus, flow.RoleVerification} 70 shuffled := flow.RoleList{flow.RoleVerification, flow.RoleConsensus} 71 thisID := this.ID() 72 shuffledID := shuffled.ID() 73 assert.Equal(t, thisID, shuffledID) 74 75 // lists with distinct elements should have distinct identifiers 76 other := flow.RoleList{flow.RoleExecution, flow.RoleVerification} 77 otherID := other.ID() 78 assert.NotEqual(t, thisID, otherID) 79 }