Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/CatLib.Core.NetStandard/CatLib.Core.NetStandard.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
<Compile Include="..\CatLib.Core\Support\Events\IEvent.cs" Link="Support\Events\IEvent.cs" />
<Compile Include="..\CatLib.Core\Support\Exception\AssertException.cs" Link="Support\Exception\AssertException.cs" />
<Compile Include="..\CatLib.Core\Support\Exception\CodeStandardException.cs" Link="Support\Exception\CodeStandardException.cs" />
<Compile Include="..\CatLib.Core\Support\Exception\LogicException.cs" Link="Support\Exception\LogicException.cs" />
<Compile Include="..\CatLib.Core\Support\Exception\RuntimeException.cs" Link="Support\Exception\RuntimeException.cs" />
<Compile Include="..\CatLib.Core\Support\FilterChain\FilterChain.cs" Link="Support\FilterChain\FilterChain.cs" />
<Compile Include="..\CatLib.Core\Support\FilterChain\IFilterChain.cs" Link="Support\FilterChain\IFilterChain.cs" />
Expand All @@ -112,8 +113,11 @@
<Compile Include="..\CatLib.Core\Support\SortSet\SortSet.cs" Link="Support\SortSet\SortSet.cs" />
<Compile Include="..\CatLib.Core\Support\Storage\IStorage.cs" Link="Support\Storage\IStorage.cs" />
<Compile Include="..\CatLib.Core\Support\Storage\MemoryStorage.cs" Link="Support\Storage\MemoryStorage.cs" />
<Compile Include="..\CatLib.Core\Support\Stream\CombineStream.cs" Link="Support\Stream\CombineStream.cs" />
<Compile Include="..\CatLib.Core\Support\Stream\PipelineStream.cs" Link="Support\Stream\PipelineStream.cs" />
<Compile Include="..\CatLib.Core\Support\Stream\SegmentStream.cs" Link="Support\Stream\SegmentStream.cs" />
<Compile Include="..\CatLib.Core\Support\Stream\StorageStream.cs" Link="Support\Stream\StorageStream.cs" />
<Compile Include="..\CatLib.Core\Support\Stream\WrapperStream.cs" Link="Support\Stream\WrapperStream.cs" />
<Compile Include="..\CatLib.Core\Support\Template\IManaged.cs" Link="Support\Template\IManaged.cs" />
<Compile Include="..\CatLib.Core\Support\Template\IManager.cs" Link="Support\Template\IManager.cs" />
<Compile Include="..\CatLib.Core\Support\Template\ISingleManaged.cs" Link="Support\Template\ISingleManaged.cs" />
Expand Down
81 changes: 66 additions & 15 deletions src/CatLib.Core.Tests/Support/Container/BindDataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,15 @@ public void CanOnRelease()
public void CheckIllegalRelease()
{
var container = new Container();
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);
var bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", true);

ExceptionAssert.Throws<ArgumentNullException>(() =>
{
bindData.OnRelease(null);
});

bindData.Unbind();
bindData = container.Bind("CheckIllegalRelease", (app, param) => "hello world", false);
ExceptionAssert.Throws<RuntimeException>(() =>
{
bindData.OnRelease((obj) =>
Expand Down Expand Up @@ -225,33 +227,82 @@ public void TestAddOnResolvingWithExtendNoneInstance()
}

/// <summary>
/// 是否能追加到解决事件
/// 检查无效的解决事件传入参数
/// </summary>
[TestMethod]
public void CanAddOnResolving()
public void CheckIllegalResolving()
{
var container = new Container();
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);

bindData.OnResolving((bind, obj) => null);
ExceptionAssert.Throws<ArgumentNullException>(() =>
{
bindData.OnResolving(null);
});
}

[TestMethod]
public void TestTypeMatchOnResolving()
{
var container = new Container();
var count = 0;
container.Bind("hello", (_, __) => new ContainerHelperTests.TestTypeMatchOnResolvingClass())
.OnResolving<ContainerHelperTests.ITypeMatchInterface>((instance) =>
{
count++;
}).OnResolving<ContainerHelperTests.ITypeMatchInterface>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
}).OnAfterResolving<ContainerHelperTests.ITypeMatchInterface>((instance) =>
{
count++;
}).OnAfterResolving<ContainerHelperTests.ITypeMatchInterface>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
}).OnAfterResolving<Container>((instance) =>
{
count++;
});

container.Make("hello");

var data = bindData.TriggerResolving(new Container());
Assert.AreEqual(null, data);
Assert.AreEqual(4, count);
}

/// <summary>
/// 检查无效的解决事件传入参数
/// </summary>
[TestMethod]
public void CheckIllegalResolving()
public void TestTypeMatchOnRelease()
{
var container = new Container();
var bindData = new BindData(container, "CanAddOnResolving", (app, param) => "hello world", false);
var count = 0;
container.Singleton("hello", (_, __) => new ContainerHelperTests.TestTypeMatchOnResolvingClass())
.OnRelease<ContainerHelperTests.ITypeMatchInterface>((instance) =>
{
count++;
}).OnRelease<Container>((instance) =>
{
count++;
}).OnRelease<ContainerHelperTests.ITypeMatchInterface>((bindData,instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
}).OnRelease<Container>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
}).OnRelease<string>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
});
container.Singleton("world", (_, __) => "hello");

ExceptionAssert.Throws<ArgumentNullException>(() =>
{
bindData.OnResolving(null);
});
container.Make("hello");

container.Release("hello");

Assert.AreEqual(2, count);
}
#endregion

Expand Down
137 changes: 137 additions & 0 deletions src/CatLib.Core.Tests/Support/Container/ContainerHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,143 @@ public void TestSetAlias()
Assert.AreEqual("abc", container.Make<string>());
}

public class TestOnResolvingClass
{
public string Name;
public TestOnResolvingClass()
{

}
}

[TestMethod]
public void TestOnResolving()
{
var container = new Container();
container.Singleton<TestOnResolvingClass>().OnResolving((instance) =>
{
var cls = (instance) as TestOnResolvingClass;
Assert.AreEqual(null, cls.Name);
cls.Name = "123";
});

container.OnResolving((instance) =>
{
var cls = (instance) as TestOnResolvingClass;
Assert.AreEqual("123", cls.Name);
cls.Name = "222";
});

Assert.AreEqual("222", container.Make<TestOnResolvingClass>().Name);
}

[TestMethod]
public void TestExtend()
{
var container = new Container();
container.Extend<string>((instance) => instance + " world");
container.Bind<string>(() => "hello");
Assert.AreEqual("hello world", container.Make<string>());
}

[TestMethod]
public void TestExtendContainer()
{
var container = new Container();
container.Extend<string>((instance, _) =>
{
Assert.AreSame(container, _);
return instance + " world";
});
container.Bind<string>(() => "hello");
Assert.AreEqual("hello world", container.Make<string>());
}

public interface ITypeMatchInterface
{

}

public class TestTypeMatchOnResolvingClass : ITypeMatchInterface
{

}

[TestMethod]
public void TestTypeMatchOnResolving()
{
var container = new Container();
container.Bind("hello", (_, __) => new TestTypeMatchOnResolvingClass());
container["world"] = "hello";
var count = 0;
container.OnResolving<ITypeMatchInterface>((instance) =>
{
count++;
});

container.OnResolving<ITypeMatchInterface>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
});

container.OnAfterResolving<ITypeMatchInterface>((instance) =>
{
count++;
});

container.OnAfterResolving<ITypeMatchInterface>((bindData,instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
});

container.Make("hello");
container.Make("world");

Assert.AreEqual(4, count);
}

[TestMethod]
public void TestTypeMatchOnRelease()
{
var container = new Container();
container.Singleton("hello", (_, __) => new TestTypeMatchOnResolvingClass());
container.Singleton("world", (_, __) => "hello");
var count = 0;
var stringCount = 0;
container.OnRelease<ITypeMatchInterface>((instance) =>
{
count++;
});

container.OnRelease<ITypeMatchInterface>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
count++;
});

container.OnRelease<string>((instance) =>
{
stringCount++;
});

container.OnRelease<string>((bindData, instance) =>
{
Assert.AreNotEqual(null, bindData);
stringCount++;
});

container.Make("hello");
container.Make("world");

container.Release("hello");
container.Release("world");

Assert.AreEqual(2, count);
Assert.AreEqual(2, stringCount);
}

/// <summary>
/// 生成容器
/// </summary>
Expand Down
Loading