github.com/iotexproject/iotex-core@v1.14.1-rc1/action/protocol/registry_test.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package protocol 7 8 import ( 9 "testing" 10 11 "github.com/golang/mock/gomock" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestRegister(t *testing.T) { 16 require := require.New(t) 17 reg := NewRegistry() 18 // Case I: Normal 19 require.NoError(reg.Register("1", nil)) 20 // Case II: Protocol with ID is already registered 21 require.Error(reg.Register("1", nil)) 22 } 23 24 func TestFind(t *testing.T) { 25 ctrl := gomock.NewController(t) 26 27 require := require.New(t) 28 reg := NewRegistry() 29 p := NewMockProtocol(ctrl) 30 require.NoError(reg.Register("1", p)) 31 // Case I: Normal 32 _, ok := reg.Find("1") 33 require.True(ok) 34 // Case II: Not exist 35 _, ok = reg.Find("0") 36 require.False(ok) 37 // Case III: Registry stores the item which is not a protocol 38 require.NoError(reg.Register("2", nil)) 39 require.Nil(reg.Find("2")) 40 } 41 42 func TestAll(t *testing.T) { 43 ctrl := gomock.NewController(t) 44 require := require.New(t) 45 reg := NewRegistry() 46 p := NewMockProtocol(ctrl) 47 require.NoError(reg.Register("1", p)) 48 // Case I: Normal 49 require.Equal(1, len(reg.All())) 50 // Case II: Registry stores the item which is not a protocol 51 require.NoError(reg.Register("2", nil)) 52 all := reg.All() 53 require.Equal(2, len(all)) 54 require.Equal(all[0], p) 55 require.Nil(all[1]) 56 }