Skip to content

Commit e4dceff

Browse files
author
喵喵大人
authored
Merge pull request #85 from CatLib/feature/1.3.0
Feature/1.3.0
2 parents b1c9ed0 + c36e958 commit e4dceff

File tree

21 files changed

+1148
-239
lines changed

21 files changed

+1148
-239
lines changed

src/CatLib.Core.NetStandard/CatLib.Core.NetStandard.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="..\CatLib.Core\Support\Events\IEvent.cs" Link="Support\Events\IEvent.cs" />
100100
<Compile Include="..\CatLib.Core\Support\Exception\AssertException.cs" Link="Support\Exception\AssertException.cs" />
101101
<Compile Include="..\CatLib.Core\Support\Exception\CodeStandardException.cs" Link="Support\Exception\CodeStandardException.cs" />
102+
<Compile Include="..\CatLib.Core\Support\Exception\LogicException.cs" Link="Support\Exception\LogicException.cs" />
102103
<Compile Include="..\CatLib.Core\Support\Exception\RuntimeException.cs" Link="Support\Exception\RuntimeException.cs" />
103104
<Compile Include="..\CatLib.Core\Support\FilterChain\FilterChain.cs" Link="Support\FilterChain\FilterChain.cs" />
104105
<Compile Include="..\CatLib.Core\Support\FilterChain\IFilterChain.cs" Link="Support\FilterChain\IFilterChain.cs" />
@@ -112,8 +113,11 @@
112113
<Compile Include="..\CatLib.Core\Support\SortSet\SortSet.cs" Link="Support\SortSet\SortSet.cs" />
113114
<Compile Include="..\CatLib.Core\Support\Storage\IStorage.cs" Link="Support\Storage\IStorage.cs" />
114115
<Compile Include="..\CatLib.Core\Support\Storage\MemoryStorage.cs" Link="Support\Storage\MemoryStorage.cs" />
116+
<Compile Include="..\CatLib.Core\Support\Stream\CombineStream.cs" Link="Support\Stream\CombineStream.cs" />
115117
<Compile Include="..\CatLib.Core\Support\Stream\PipelineStream.cs" Link="Support\Stream\PipelineStream.cs" />
118+
<Compile Include="..\CatLib.Core\Support\Stream\SegmentStream.cs" Link="Support\Stream\SegmentStream.cs" />
116119
<Compile Include="..\CatLib.Core\Support\Stream\StorageStream.cs" Link="Support\Stream\StorageStream.cs" />
120+
<Compile Include="..\CatLib.Core\Support\Stream\WrapperStream.cs" Link="Support\Stream\WrapperStream.cs" />
117121
<Compile Include="..\CatLib.Core\Support\Template\IManaged.cs" Link="Support\Template\IManaged.cs" />
118122
<Compile Include="..\CatLib.Core\Support\Template\IManager.cs" Link="Support\Template\IManager.cs" />
119123
<Compile Include="..\CatLib.Core\Support\Template\ISingleManaged.cs" Link="Support\Template\ISingleManaged.cs" />

src/CatLib.Core.Tests/Support/Container/BindDataTests.cs

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,15 @@ public void CanOnRelease()
177177
public void CheckIllegalRelease()
178178
{
179179
var container = new Container();
180-
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);
180+
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", true);
181181

182182
ExceptionAssert.Throws<ArgumentNullException>(() =>
183183
{
184184
bindData.OnRelease(null);
185185
});
186186

187+
bindData.Unbind();
188+
bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);
187189
ExceptionAssert.Throws<RuntimeException>(() =>
188190
{
189191
bindData.OnRelease((obj) =>
@@ -225,33 +227,82 @@ public void TestAddOnResolvingWithExtendNoneInstance()
225227
}
226228

227229
/// <summary>
228-
/// 是否能追加到解决事件
230+
/// 检查无效的解决事件传入参数
229231
/// </summary>
230232
[TestMethod]
231-
public void CanAddOnResolving()
233+
public void CheckIllegalResolving()
232234
{
233235
var container = new Container();
234236
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);
235237

236-
bindData.OnResolving((bind, obj) => null);
238+
ExceptionAssert.Throws<ArgumentNullException>(() =>
239+
{
240+
bindData.OnResolving(null);
241+
});
242+
}
243+
244+
[TestMethod]
245+
public void TestTypeMatchOnResolving()
246+
{
247+
var container = new Container();
248+
var count = 0;
249+
container.Bind("hello", (_, __) => new ContainerHelperTests.TestTypeMatchOnResolvingClass())
250+
.OnResolving<ContainerHelperTests.ITypeMatchInterface>((instance) =>
251+
{
252+
count++;
253+
}).OnResolving<ContainerHelperTests.ITypeMatchInterface>((bindData, instance) =>
254+
{
255+
Assert.AreNotEqual(null, bindData);
256+
count++;
257+
}).OnAfterResolving<ContainerHelperTests.ITypeMatchInterface>((instance) =>
258+
{
259+
count++;
260+
}).OnAfterResolving<ContainerHelperTests.ITypeMatchInterface>((bindData, instance) =>
261+
{
262+
Assert.AreNotEqual(null, bindData);
263+
count++;
264+
}).OnAfterResolving<Container>((instance) =>
265+
{
266+
count++;
267+
});
268+
269+
container.Make("hello");
237270

238-
var data = bindData.TriggerResolving(new Container());
239-
Assert.AreEqual(null, data);
271+
Assert.AreEqual(4, count);
240272
}
241273

242-
/// <summary>
243-
/// 检查无效的解决事件传入参数
244-
/// </summary>
245274
[TestMethod]
246-
public void CheckIllegalResolving()
275+
public void TestTypeMatchOnRelease()
247276
{
248277
var container = new Container();
249-
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);
278+
var count = 0;
279+
container.Singleton("hello", (_, __) => new ContainerHelperTests.TestTypeMatchOnResolvingClass())
280+
.OnRelease<ContainerHelperTests.ITypeMatchInterface>((instance) =>
281+
{
282+
count++;
283+
}).OnRelease<Container>((instance) =>
284+
{
285+
count++;
286+
}).OnRelease<ContainerHelperTests.ITypeMatchInterface>((bindData,instance) =>
287+
{
288+
Assert.AreNotEqual(null, bindData);
289+
count++;
290+
}).OnRelease<Container>((bindData, instance) =>
291+
{
292+
Assert.AreNotEqual(null, bindData);
293+
count++;
294+
}).OnRelease<string>((bindData, instance) =>
295+
{
296+
Assert.AreNotEqual(null, bindData);
297+
count++;
298+
});
299+
container.Singleton("world", (_, __) => "hello");
250300

251-
ExceptionAssert.Throws<ArgumentNullException>(() =>
252-
{
253-
bindData.OnResolving(null);
254-
});
301+
container.Make("hello");
302+
303+
container.Release("hello");
304+
305+
Assert.AreEqual(2, count);
255306
}
256307
#endregion
257308

src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,143 @@ public void TestSetAlias()
354354
Assert.AreEqual("abc", container.Make<string>());
355355
}
356356

357+
public class TestOnResolvingClass
358+
{
359+
public string Name;
360+
public TestOnResolvingClass()
361+
{
362+
363+
}
364+
}
365+
366+
[TestMethod]
367+
public void TestOnResolving()
368+
{
369+
var container = new Container();
370+
container.Singleton<TestOnResolvingClass>().OnResolving((instance) =>
371+
{
372+
var cls = (instance) as TestOnResolvingClass;
373+
Assert.AreEqual(null, cls.Name);
374+
cls.Name = "123";
375+
});
376+
377+
container.OnResolving((instance) =>
378+
{
379+
var cls = (instance) as TestOnResolvingClass;
380+
Assert.AreEqual("123", cls.Name);
381+
cls.Name = "222";
382+
});
383+
384+
Assert.AreEqual("222", container.Make<TestOnResolvingClass>().Name);
385+
}
386+
387+
[TestMethod]
388+
public void TestExtend()
389+
{
390+
var container = new Container();
391+
container.Extend<string>((instance) => instance + " world");
392+
container.Bind<string>(() => "hello");
393+
Assert.AreEqual("hello world", container.Make<string>());
394+
}
395+
396+
[TestMethod]
397+
public void TestExtendContainer()
398+
{
399+
var container = new Container();
400+
container.Extend<string>((instance, _) =>
401+
{
402+
Assert.AreSame(container, _);
403+
return instance + " world";
404+
});
405+
container.Bind<string>(() => "hello");
406+
Assert.AreEqual("hello world", container.Make<string>());
407+
}
408+
409+
public interface ITypeMatchInterface
410+
{
411+
412+
}
413+
414+
public class TestTypeMatchOnResolvingClass : ITypeMatchInterface
415+
{
416+
417+
}
418+
419+
[TestMethod]
420+
public void TestTypeMatchOnResolving()
421+
{
422+
var container = new Container();
423+
container.Bind("hello", (_, __) => new TestTypeMatchOnResolvingClass());
424+
container["world"] = "hello";
425+
var count = 0;
426+
container.OnResolving<ITypeMatchInterface>((instance) =>
427+
{
428+
count++;
429+
});
430+
431+
container.OnResolving<ITypeMatchInterface>((bindData, instance) =>
432+
{
433+
Assert.AreNotEqual(null, bindData);
434+
count++;
435+
});
436+
437+
container.OnAfterResolving<ITypeMatchInterface>((instance) =>
438+
{
439+
count++;
440+
});
441+
442+
container.OnAfterResolving<ITypeMatchInterface>((bindData,instance) =>
443+
{
444+
Assert.AreNotEqual(null, bindData);
445+
count++;
446+
});
447+
448+
container.Make("hello");
449+
container.Make("world");
450+
451+
Assert.AreEqual(4, count);
452+
}
453+
454+
[TestMethod]
455+
public void TestTypeMatchOnRelease()
456+
{
457+
var container = new Container();
458+
container.Singleton("hello", (_, __) => new TestTypeMatchOnResolvingClass());
459+
container.Singleton("world", (_, __) => "hello");
460+
var count = 0;
461+
var stringCount = 0;
462+
container.OnRelease<ITypeMatchInterface>((instance) =>
463+
{
464+
count++;
465+
});
466+
467+
container.OnRelease<ITypeMatchInterface>((bindData, instance) =>
468+
{
469+
Assert.AreNotEqual(null, bindData);
470+
count++;
471+
});
472+
473+
container.OnRelease<string>((instance) =>
474+
{
475+
stringCount++;
476+
});
477+
478+
container.OnRelease<string>((bindData, instance) =>
479+
{
480+
Assert.AreNotEqual(null, bindData);
481+
stringCount++;
482+
});
483+
484+
container.Make("hello");
485+
container.Make("world");
486+
487+
container.Release("hello");
488+
container.Release("world");
489+
490+
Assert.AreEqual(2, count);
491+
Assert.AreEqual(2, stringCount);
492+
}
493+
357494
/// <summary>
358495
/// 生成容器
359496
/// </summary>

0 commit comments

Comments
 (0)