github.com/pavlo67/common@v0.5.3/common/joiner/joiner_runtime/runtime_test.go (about) 1 package joiner_runtime 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/require" 8 9 "github.com/pavlo67/common/common/joiner" 10 ) 11 12 func TestInterface(t *testing.T) { 13 joinerOp := New(nil, nil) 14 15 const keyA1 joiner.InterfaceKey = "KeyA1" 16 structA1 := &StructA{} 17 18 const keyA2 joiner.InterfaceKey = "KeyA2" 19 structA2 := &StructA{} 20 21 joinerOp.Join(structA1, keyA1) 22 joinerOp.Join(structA2, keyA2) 23 24 structA1Joined, ok := joinerOp.Interface(keyA1).(InterfaceA) 25 require.True(t, ok) 26 require.Equal(t, structA1, structA1Joined) 27 } 28 29 //func TestComponentsAll(t *testing.T) { 30 // joiner := New() 31 // 32 // const textA1 = "StructA.TypeKey()" 33 // const keyA1 HandlerKey = "KeyA1" 34 // structA1 := &StructA{text: textA1} 35 // structA3 := &StructA{text: textA1} 36 // 37 // const keyA2 HandlerKey = "KeyA2" 38 // structA2 := &StructA{} 39 // 40 // const keyB1 HandlerKey = "KeyB1" 41 // structB1 := &StructB{} 42 // 43 // joiner.Join(structA1, keyA1) 44 // joiner.Join(structA3, keyA1) 45 // joiner.Join(structB1, keyB1) 46 // joiner.Join(structA2, keyA2) 47 // 48 // routes := joiner.ComponentsAll(keyA1) 49 // require.Equal(t, 2, len(routes)) 50 // 51 // for _, component := range routes { 52 // require.Equal(t, keyA1, component.Key) 53 // 54 // interfaceA, ok := component.Interface.(InterfaceA) 55 // require.True(t, ok) 56 // require.NotNil(t, interfaceA) 57 // 58 // text := interfaceA.ActionA() 59 // require.Equal(t, textA1, text) 60 // } 61 // 62 // require.Equal(t, 1, structA1.NumActionA) 63 // require.Equal(t, 1, structA3.NumActionA) 64 // require.Equal(t, 0, structA2.NumActionA) 65 //} 66 67 func TestComponentsAllWithSignature(t *testing.T) { 68 joinerOp := New(nil, nil) 69 70 const textA1 = "StructA.TypeKey()" 71 const keyA1 joiner.InterfaceKey = "KeyA1" 72 structA1 := &StructA{text: textA1} 73 74 const keyA2 joiner.InterfaceKey = "KeyA2" 75 structA2 := &StructA{text: textA1} 76 77 const keyA3 joiner.InterfaceKey = "KeyA3" 78 structA3 := &StructA{text: textA1} 79 80 const keyB1 joiner.InterfaceKey = "KeyB1" 81 structB1 := &StructB{} 82 83 joinerOp.Join(structA1, keyA1) 84 joinerOp.Join(structA3, keyA3) 85 joinerOp.Join(structB1, keyB1) 86 joinerOp.Join(structA2, keyA2) 87 88 components := joinerOp.InterfacesAll((*InterfaceA)(nil)) 89 require.Equal(t, 3, len(components)) 90 91 for _, component := range components { 92 interfaceA, ok := component.Interface.(InterfaceA) 93 require.True(t, ok) 94 require.NotNil(t, interfaceA) 95 96 text := interfaceA.ActionA() 97 require.Equal(t, textA1, text) 98 } 99 100 require.Equal(t, 1, structA1.NumActionA) 101 require.Equal(t, 1, structA3.NumActionA) 102 require.Equal(t, 1, structA2.NumActionA) 103 } 104 105 func TestCloseAll(t *testing.T) { 106 joinerOp := New(nil, nil) 107 108 const textA1 = "StructA.TypeKey()" 109 const keyA1 joiner.InterfaceKey = "KeyA1" 110 structA1 := &StructA{text: textA1} 111 112 const keyA2 joiner.InterfaceKey = "KeyA2" 113 structA2 := &StructA{text: textA1} 114 115 const keyA3 joiner.InterfaceKey = "KeyA3" 116 structA3 := &StructA{text: textA1} 117 118 const keyB1 joiner.InterfaceKey = "KeyB1" 119 structB1 := &StructB{} 120 121 joinerOp.Join(structA1, keyA1) 122 joinerOp.Join(structA3, keyA3) 123 joinerOp.Join(structB1, keyB1) 124 joinerOp.Join(structA2, keyA2) 125 126 joinerOp.CloseAll() 127 128 require.Equal(t, 1, structA1.NumClose) 129 require.Equal(t, 1, structA2.NumClose) 130 require.Equal(t, 1, structA3.NumClose) 131 require.Equal(t, 1, structB1.NumClose) 132 } 133 134 // InterfaceA (includes Closer) ----------------------------------------------------------------------------------------------------- 135 136 type InterfaceA interface { 137 ActionA() string 138 } 139 140 type StructA struct { 141 NumActionA, NumClose int 142 text string 143 } 144 145 var _ InterfaceA = &StructA{} 146 var _ joiner.Closer = &StructA{} 147 148 func (s *StructA) ActionA() string { 149 s.NumActionA++ 150 fmt.Println("StructA.TypeKey()") 151 return s.text 152 } 153 154 func (s *StructA) Close() error { 155 s.NumClose++ 156 fmt.Println("StructA.Close()") 157 return nil 158 } 159 160 // InterfaceB (includes Closer) ----------------------------------------------------------------------------------------------------- 161 162 type InterfaceB interface { 163 ActionB() string 164 } 165 166 type StructB struct { 167 NumActionB, NumClose int 168 } 169 170 var _ InterfaceB = &StructB{} 171 var _ joiner.Closer = &StructB{} 172 173 func (s *StructB) ActionB() string { 174 s.NumActionB++ 175 fmt.Println("StructB.TypeKey()") 176 return "StructB.TypeKey()" 177 } 178 179 func (s *StructB) Close() error { 180 s.NumClose++ 181 fmt.Println("StructB.Close()") 182 return nil 183 }