Showing
2 changed files
with
23 additions
and
14 deletions
@@ -132,7 +132,7 @@ extension Inst on GetInterface { | @@ -132,7 +132,7 @@ extension Inst on GetInterface { | ||
132 | /// Replace a parent instance of a class in dependency management | 132 | /// Replace a parent instance of a class in dependency management |
133 | /// with a [child] instance | 133 | /// with a [child] instance |
134 | /// - [tag] optional, if you use a [tag] to register the Instance. | 134 | /// - [tag] optional, if you use a [tag] to register the Instance. |
135 | - void replace<P, C extends P>(C child, {String? tag}) { | 135 | + void replace<P>(P child, {String? tag}) { |
136 | final info = GetInstance().getInstanceInfo<P>(tag: tag); | 136 | final info = GetInstance().getInstanceInfo<P>(tag: tag); |
137 | final permanent = (info.isPermanent ?? false); | 137 | final permanent = (info.isPermanent ?? false); |
138 | delete<P>(tag: tag, force: permanent); | 138 | delete<P>(tag: tag, force: permanent); |
@@ -10,7 +10,9 @@ class Mock { | @@ -10,7 +10,9 @@ class Mock { | ||
10 | } | 10 | } |
11 | } | 11 | } |
12 | 12 | ||
13 | -class DisposableController extends GetLifeCycle {} | 13 | +abstract class MyController extends GetLifeCycle {} |
14 | + | ||
15 | +class DisposableController extends MyController {} | ||
14 | 16 | ||
15 | // ignore: one_member_abstracts | 17 | // ignore: one_member_abstracts |
16 | abstract class Service { | 18 | abstract class Service { |
@@ -157,43 +159,50 @@ void main() { | @@ -157,43 +159,50 @@ void main() { | ||
157 | }); | 159 | }); |
158 | }); | 160 | }); |
159 | 161 | ||
160 | - test('Get.replace test for replacing temporary parent instance with child', | ||
161 | - () async { | 162 | + group('Get.replace test for replacing parent instance that is', () { |
163 | + test('temporary', () async { | ||
162 | Get.put(DisposableController()); | 164 | Get.put(DisposableController()); |
163 | - Get.replace<DisposableController, Controller>(Controller()); | 165 | + Get.replace<DisposableController>(Controller()); |
164 | final instance = Get.find<DisposableController>(); | 166 | final instance = Get.find<DisposableController>(); |
165 | expect(instance is Controller, isTrue); | 167 | expect(instance is Controller, isTrue); |
166 | expect((instance as Controller).init, greaterThan(0)); | 168 | expect((instance as Controller).init, greaterThan(0)); |
167 | }); | 169 | }); |
168 | 170 | ||
169 | - test('Get.replace test for replacing permanent parent instance with child', | ||
170 | - () async { | 171 | + test('permanent', () async { |
171 | Get.put(DisposableController(), permanent: true); | 172 | Get.put(DisposableController(), permanent: true); |
172 | - Get.replace<DisposableController, Controller>(Controller()); | 173 | + Get.replace<DisposableController>(Controller()); |
173 | final instance = Get.find<DisposableController>(); | 174 | final instance = Get.find<DisposableController>(); |
174 | expect(instance is Controller, isTrue); | 175 | expect(instance is Controller, isTrue); |
175 | expect((instance as Controller).init, greaterThan(0)); | 176 | expect((instance as Controller).init, greaterThan(0)); |
176 | }); | 177 | }); |
177 | 178 | ||
178 | - test('Get.replace test for replacing tagged temporary instance with child', | ||
179 | - () async { | 179 | + test('tagged temporary', () async { |
180 | final tag = 'tag'; | 180 | final tag = 'tag'; |
181 | Get.put(DisposableController(), tag: tag); | 181 | Get.put(DisposableController(), tag: tag); |
182 | - Get.replace<DisposableController, Controller>(Controller(), tag: tag); | 182 | + Get.replace<DisposableController>(Controller(), tag: tag); |
183 | final instance = Get.find<DisposableController>(tag: tag); | 183 | final instance = Get.find<DisposableController>(tag: tag); |
184 | expect(instance is Controller, isTrue); | 184 | expect(instance is Controller, isTrue); |
185 | expect((instance as Controller).init, greaterThan(0)); | 185 | expect((instance as Controller).init, greaterThan(0)); |
186 | }); | 186 | }); |
187 | 187 | ||
188 | - test('Get.replace test for replacing tagged parent instance with child', | ||
189 | - () async { | 188 | + test('tagged permanent', () async { |
190 | final tag = 'tag'; | 189 | final tag = 'tag'; |
191 | Get.put(DisposableController(), permanent: true, tag: tag); | 190 | Get.put(DisposableController(), permanent: true, tag: tag); |
192 | - Get.replace<DisposableController, Controller>(Controller(), tag: tag); | 191 | + Get.replace<DisposableController>(Controller(), tag: tag); |
193 | final instance = Get.find<DisposableController>(tag: tag); | 192 | final instance = Get.find<DisposableController>(tag: tag); |
194 | expect(instance is Controller, isTrue); | 193 | expect(instance is Controller, isTrue); |
195 | expect((instance as Controller).init, greaterThan(0)); | 194 | expect((instance as Controller).init, greaterThan(0)); |
196 | }); | 195 | }); |
196 | + | ||
197 | + test('a generic parent type', () async { | ||
198 | + final tag = 'tag'; | ||
199 | + Get.put<MyController>(DisposableController(), permanent: true, tag: tag); | ||
200 | + Get.replace<MyController>(Controller(), tag: tag); | ||
201 | + final instance = Get.find<MyController>(tag: tag); | ||
202 | + expect(instance is Controller, isTrue); | ||
203 | + expect((instance as Controller).init, greaterThan(0)); | ||
204 | + }); | ||
205 | + }); | ||
197 | } | 206 | } |
198 | 207 | ||
199 | class Controller extends DisposableController { | 208 | class Controller extends DisposableController { |
-
Please register or login to post a comment