Initial Commit

This commit is contained in:
2026-03-16 14:38:46 +02:00
commit b8f7327a21
2327 changed files with 253610 additions and 0 deletions
@@ -0,0 +1,62 @@
// In order to run this, install dotMemoryPeek through nuget and then change this to 1
#if false
using System;
using System.Diagnostics;
using JetBrains.dotMemoryUnit;
using ModestTree;
using NUnit.Framework;
using Assert=ModestTree.Assert;
using System.Linq;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestAllocs : ZenjectUnitTestFixture
{
interface IFoo
{
}
public class Foo1 : IFoo
{
}
public class Foo2 : IFoo
{
}
[Test]
[DotMemoryUnit(CollectAllocations=true)]
public void TryStuff()
{
Container.Bind<IFoo>().To<Foo1>().AsSingle();
Container.TryResolve<IFoo>();
Log.Info("Starting memory check");
var point1 = dotMemory.Check();
Container.TryResolve<IFoo>();
dotMemory.Check(memory =>
{
var traffic = memory.GetTrafficFrom(point1).Where(x => x.Namespace.Like("Zenject"));
var bytesAllocated = traffic.AllocatedMemory.SizeInBytes;
if (bytesAllocated != 0)
{
Log.Info("Found unnecessary memory allocations ({0} kb) in Container.Resolve. Allocated Types: \n{1}",
(float)bytesAllocated / 1024f, traffic.GroupByType().OrderByDescending(x => x.AllocatedMemoryInfo.SizeInBytes)
.Select(x => "{0} bytes: ({1})".Fmt(x.AllocatedMemoryInfo.SizeInBytes, x.Type.PrettyName())).Join("\n"));
}
});
Log.Info("Done memory check");
}
}
}
#endif
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6935919ae38269447a54f963952ce929
timeCreated: 1519817043
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,158 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Bindings.Singletons
{
[TestFixture]
public class TestAsSingle : ZenjectUnitTestFixture
{
[Test]
public void TestAsSingleThrows()
{
Container.Bind<Foo>().AsSingle();
Container.Bind<Foo>().AsSingle();
Assert.Throws(() => Container.FlushBindings());
}
[Test]
public void TestAsSingleAndTransientThrows()
{
Container.Bind<Foo>().AsSingle();
Container.Bind<Foo>().AsTransient();
Assert.Throws(() => Container.FlushBindings());
}
[Test]
public void TestAsSingleAndResolveNoThrow()
{
Container.Bind<Foo>().AsSingle();
Container.Bind<IFoo>().To<Foo>().FromResolve();
Assert.IsEqual(Container.Resolve<IFoo>(), Container.Resolve<Foo>());
}
[Test]
public void TestToSingleMethod1()
{
Container.Bind<Foo>().AsSingle();
Assert.Throws(() =>
{
Container.Bind<Foo>().FromMethod(container => new Foo()).AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromInstance(new Foo()).AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromIFactory(b => b.To<FooFactory>().AsCached()).AsSingle();
Container.FlushBindings();
});
}
[Test]
public void TestToSingleMethod()
{
Container.Bind<Foo>().FromMethod(container => new Foo()).AsSingle();
Assert.Throws(() =>
{
Container.Bind<Foo>().AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromInstance(new Foo()).AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromIFactory(b => b.To<FooFactory>().AsCached()).AsSingle();
Container.FlushBindings();
});
}
[Test]
public void TestToSingleInstance()
{
Container.Bind<Foo>().FromInstance(new Foo()).AsSingle();
Assert.Throws(() =>
{
Container.Bind<Foo>().AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromMethod(container => new Foo()).AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromIFactory(b => b.To<FooFactory>().AsCached()).AsSingle();
Container.FlushBindings();
});
}
[Test]
public void TestToSingleFactory()
{
Container.Bind<Foo>().FromIFactory(b => b.To<FooFactory>().AsCached()).AsSingle();
Assert.Throws(() =>
{
Container.Bind<Foo>().AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromMethod(container => new Foo()).AsSingle();
Container.FlushBindings();
});
Assert.Throws(() =>
{
Container.Bind<Foo>().FromInstance(new Foo()).AsSingle();
Container.FlushBindings();
});
}
class Bar
{
public Foo GetFoo()
{
return new Foo();
}
}
interface IFoo
{
}
class Foo : IFoo
{
}
class FooFactory : IFactory<Foo>
{
public Foo Create()
{
return new Foo();
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6b3c12a3bc805fb47b7dd01a4c8f100f
timeCreated: 1520670682
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,79 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestBindCallbacks : ZenjectUnitTestFixture
{
public class Foo
{
[Inject]
public int Value2
{
get; set;
}
public string Value
{
get; set;
}
public class Factory : PlaceholderFactory<Foo>
{
}
public class Pool : MemoryPool<Foo>
{
}
}
[Test]
public void Test1()
{
Container.BindInstance(5).WhenInjectedInto<Foo>();
Container.Bind<Foo>().AsSingle().OnInstantiated<Foo>((ctx, f) =>
{
Assert.IsEqual(f.Value2, 5);
f.Value = "asdf";
});
var foo = Container.Resolve<Foo>();
Assert.IsEqual(foo.Value, "asdf");
}
[Test]
public void TestFactory1()
{
Container.BindInstance(5).WhenInjectedInto<Foo>();
Container.BindFactory<Foo, Foo.Factory>().OnInstantiated<Foo>((ctx, f) =>
{
Assert.IsEqual(f.Value2, 5);
f.Value = "asdf";
});
var foo = Container.Resolve<Foo.Factory>().Create();
Assert.IsEqual(foo.Value, "asdf");
}
[Test]
public void TestMemoryPool1()
{
Container.BindInstance(5).WhenInjectedInto<Foo>();
Container.BindMemoryPool<Foo, Foo.Pool>().OnInstantiated<Foo>((ctx, f) =>
{
Assert.IsEqual(f.Value2, 5);
f.Value = "asdf";
});
var foo = Container.Resolve<Foo.Pool>().Spawn();
Assert.IsEqual(foo.Value, "asdf");
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 1a3977987fbe0944c8f7d3879818e79c
timeCreated: 1535263051
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,82 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestCircularDependencies : ZenjectUnitTestFixture
{
[Test]
public void TestThrows()
{
Container.Bind<Foo1>().AsSingle();
Container.Bind<Bar1>().AsSingle();
Assert.Throws(() => Container.Resolve<Foo1>());
Assert.Throws(() => Container.Resolve<Bar1>());
}
public class Foo1
{
public Foo1(Bar1 bar)
{
}
}
public class Bar1
{
public Bar1(Foo1 foo)
{
}
}
[Test]
public void TestPostInject()
{
Container.Bind<Foo2>().AsSingle();
Container.Bind<Bar2>().AsSingle();
Assert.IsNotNull(Container.Resolve<Foo2>());
Assert.IsNotNull(Container.Resolve<Bar2>());
}
public class Foo2
{
[Inject]
public void Init(Bar2 bar)
{
}
}
public class Bar2
{
[Inject]
public void Init(Foo2 foo)
{
}
}
[Test]
public void TestField()
{
Container.Bind<Foo3>().AsSingle();
Container.Bind<Bar3>().AsSingle();
Assert.IsNotNull(Container.Resolve<Foo3>().Bar);
Assert.IsNotNull(Container.Resolve<Bar3>().Foo);
}
public class Foo3
{
[Inject]
public Bar3 Bar;
}
public class Bar3
{
[Inject]
public Foo3 Foo;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 42f3a9d60487be74a953056c109c0f8f
timeCreated: 1461708050
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,47 @@
using System.Linq;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestClearCacheProvider : ZenjectUnitTestFixture
{
public interface IFoo
{
}
public class Foo1 : IFoo
{
}
public class Foo2 : IFoo
{
}
// For issue https://github.com/modesttree/Zenject/issues/441
[Test]
public void Test1()
{
Container.Bind<IFoo>().To<Foo1>().AsSingle();
Assert.That(Container.Resolve<IFoo>() is Foo1);
var context = new InjectContext(Container, typeof(IFoo));
var provider = Container.AllProviders.OfType<CachedProvider>()
.Where(x => x.GetInstanceType(context) == typeof(Foo1)).Single();
Assert.IsEqual(provider.NumInstances, 1);
provider.ClearCache();
Assert.IsEqual(provider.NumInstances, 0);
Container.Rebind<IFoo>().To<Foo2>().AsSingle();
Assert.That(Container.Resolve<IFoo>() is Foo2);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 60fe740f61f8eb340988d68157f7ec18
timeCreated: 1526286932
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,64 @@
using System;
using NUnit.Framework;
using Zenject.Internal;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestCustomInjectAttribute : ZenjectUnitTestFixture
{
public class InjectCustomAttribute : Attribute
{
}
class Bar
{
}
[NoReflectionBaking]
class Foo
{
[InjectCustom]
public Bar BarField = null;
public Foo(Bar barParam)
{
BarParam = barParam;
}
public Bar BarParam;
public Bar BarMethod;
[InjectCustom]
public Bar BarProperty
{
get; private set;
}
[InjectCustom]
public void Construct(Bar bar)
{
BarMethod = bar;
}
}
[Test]
public void Test1()
{
ReflectionTypeAnalyzer.AddCustomInjectAttribute(typeof(InjectCustomAttribute));
Container.Bind<Bar>().AsSingle();
Container.Bind<Foo>().AsSingle();
var foo = Container.Resolve<Foo>();
var bar = Container.Resolve<Bar>();
Assert.IsEqual(foo.BarProperty, bar);
Assert.IsEqual(foo.BarField, bar);
Assert.IsEqual(foo.BarMethod, bar);
Assert.IsEqual(foo.BarParam, bar);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 483591f22e80cb944b470afb280c1260
timeCreated: 1535696025
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,87 @@
using System;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestDecoratorValidation
{
public interface ISaveHandler
{
void Save();
}
public class SaveHandler : ISaveHandler
{
public void Save()
{
}
}
public class SaveDecorator1 : ISaveHandler
{
readonly ISaveHandler _handler;
public SaveDecorator1(ISaveHandler handler)
{
_handler = handler;
}
public void Save()
{
_handler.Save();
}
}
DiContainer Container
{
get; set;
}
[SetUp]
public void Setup()
{
Container = new DiContainer(true);
Container.Settings = new ZenjectSettings(ValidationErrorResponses.Throw);
}
public class Foo
{
public Foo(ISaveHandler saveHandler)
{
}
}
[Test]
public void TestSuccess1()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
Container.Bind<Foo>().AsSingle().NonLazy();
Container.ResolveRoots();
}
[Test]
public void TestFail1()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>().FromResolve(Guid.NewGuid());
Container.Bind<Foo>().AsSingle().NonLazy();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestFail2()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().FromResolve(Guid.NewGuid()).AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
Container.Bind<Foo>().AsSingle().NonLazy();
Assert.Throws(() => Container.ResolveRoots());
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 4ea46e52a0fbff6478bff334331e6aaf
timeCreated: 1534318715
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,236 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestDecorators : ZenjectUnitTestFixture
{
static int CallCounter;
public interface ISaveHandler
{
void Save();
}
public class SaveHandler : ISaveHandler
{
public SaveHandler()
{
NumInstances++;
}
public static int CallCount
{
get; set;
}
public static int NumInstances
{
get; set;
}
public void Save()
{
CallCount = CallCounter++;
}
}
public class SaveDecorator1 : ISaveHandler
{
readonly ISaveHandler _handler;
public SaveDecorator1(ISaveHandler handler)
{
_handler = handler;
NumInstances++;
}
public static int NumInstances
{
get; set;
}
public static int CallCount
{
get; set;
}
public void Save()
{
CallCount = CallCounter++;
_handler.Save();
}
}
public class SaveDecorator2 : ISaveHandler
{
readonly ISaveHandler _handler;
public SaveDecorator2(ISaveHandler handler)
{
_handler = handler;
}
public static int CallCount
{
get; set;
}
public void Save()
{
CallCount = CallCounter++;
_handler.Save();
}
}
public class Foo
{
}
[Test]
public void TestSimpleCase()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
CallCounter = 1;
SaveHandler.CallCount = 0;
SaveDecorator1.CallCount = 0;
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveDecorator1.CallCount, 1);
Assert.IsEqual(SaveHandler.CallCount, 2);
}
[Test]
public void TestMultiple()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
Container.Decorate<ISaveHandler>().With<SaveDecorator2>();
CallCounter = 1;
SaveHandler.CallCount = 0;
SaveDecorator1.CallCount = 0;
SaveDecorator2.CallCount = 0;
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveDecorator2.CallCount, 1);
Assert.IsEqual(SaveDecorator1.CallCount, 2);
Assert.IsEqual(SaveHandler.CallCount, 3);
}
[Test]
public void TestCaching()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsTransient();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
SaveHandler.NumInstances = 0;
SaveDecorator1.NumInstances = 0;
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveHandler.NumInstances, 1);
Assert.IsEqual(SaveDecorator1.NumInstances, 1);
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveHandler.NumInstances, 2);
Assert.IsEqual(SaveDecorator1.NumInstances, 2);
}
[Test]
public void TestCaching2()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsCached();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
SaveHandler.NumInstances = 0;
SaveDecorator1.NumInstances = 0;
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveHandler.NumInstances, 1);
Assert.IsEqual(SaveDecorator1.NumInstances, 1);
Container.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveHandler.NumInstances, 1);
Assert.IsEqual(SaveDecorator1.NumInstances, 1);
}
[Test]
public void TestDecoratorMethod()
{
SaveHandler.NumInstances = 0;
SaveDecorator1.CallCount = 0;
bool wasCalled = false;
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>()
.With<SaveDecorator1>().FromMethod((x, h) =>
{
wasCalled = true;
return new SaveDecorator1(h);
});
CallCounter = 1;
Assert.That(!wasCalled);
Assert.IsEqual(SaveHandler.NumInstances, 0);
Assert.IsEqual(SaveDecorator1.CallCount, 0);
Container.Resolve<ISaveHandler>().Save();
Assert.That(wasCalled);
Assert.IsEqual(SaveHandler.NumInstances, 1);
Assert.IsEqual(SaveDecorator1.CallCount, 1);
}
[Test]
public void TestContainerInheritance()
{
Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
var subContainer = Container.CreateSubContainer();
CallCounter = 1;
SaveHandler.CallCount = 0;
SaveDecorator1.CallCount = 0;
subContainer.Resolve<ISaveHandler>().Save();
Assert.IsEqual(SaveDecorator1.CallCount, 1);
Assert.IsEqual(SaveHandler.CallCount, 2);
}
// TODO - Fix this
//[Test]
//public void TestContainerInheritance2()
//{
//Container.Bind<ISaveHandler>().To<SaveHandler>().AsSingle();
//Container.Decorate<ISaveHandler>().With<SaveDecorator1>();
//var subContainer = Container.CreateSubContainer();
//subContainer.Decorate<ISaveHandler>().With<SaveDecorator2>();
//CallCounter = 1;
//SaveHandler.CallCount = 0;
//SaveDecorator1.CallCount = 0;
//SaveDecorator2.CallCount = 0;
//subContainer.Resolve<ISaveHandler>().Save();
//Assert.IsEqual(SaveDecorator2.CallCount, 1);
//Assert.IsEqual(SaveDecorator1.CallCount, 2);
//Assert.IsEqual(SaveHandler.CallCount, 3);
//}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 919290ef5f80b8c48832d7a91f8ab674
timeCreated: 1528372067
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,93 @@
using System;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestFacadeSubContainer
{
static int NumInstalls;
[Test]
public void Test1()
{
NumInstalls = 0;
InitTest.WasRun = false;
TickTest.WasRun = false;
DisposeTest.WasRun = false;
var container = new DiContainer();
container.Bind(typeof(TickableManager), typeof(InitializableManager), typeof(DisposableManager))
.ToSelf().AsSingle().CopyIntoAllSubContainers();
// This is how you add ITickables / etc. within sub containers
container.BindInterfacesAndSelfTo<FooKernel>()
.FromSubContainerResolve().ByMethod(InstallFoo).AsSingle();
var tickManager = container.Resolve<TickableManager>();
var initManager = container.Resolve<InitializableManager>();
var disposeManager = container.Resolve<DisposableManager>();
Assert.That(!InitTest.WasRun);
Assert.That(!TickTest.WasRun);
Assert.That(!DisposeTest.WasRun);
initManager.Initialize();
tickManager.Update();
disposeManager.Dispose();
Assert.That(InitTest.WasRun);
Assert.That(TickTest.WasRun);
Assert.That(DisposeTest.WasRun);
}
public void InstallFoo(DiContainer subContainer)
{
NumInstalls++;
subContainer.Bind<FooKernel>().AsSingle();
subContainer.Bind<IInitializable>().To<InitTest>().AsSingle();
subContainer.Bind<ITickable>().To<TickTest>().AsSingle();
subContainer.Bind<IDisposable>().To<DisposeTest>().AsSingle();
}
public class FooKernel : Kernel
{
}
public class InitTest : IInitializable
{
public static bool WasRun;
public void Initialize()
{
WasRun = true;
}
}
public class TickTest : ITickable
{
public static bool WasRun;
public void Tick()
{
WasRun = true;
}
}
public class DisposeTest : IDisposable
{
public static bool WasRun;
public void Dispose()
{
WasRun = true;
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 84329a2f5907361459ac88fb29b3c102
timeCreated: 1461708051
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,48 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestFactoryMemoryPoolCustomInterface : ZenjectUnitTestFixture
{
[Test]
public void Test1()
{
var foo = new Foo();
Container.BindFactoryCustomInterface<Foo, Foo.Factory, Foo.IFooFactory>().FromInstance(foo);
Assert.IsEqual(Container.Resolve<Foo.IFooFactory>().Create(), foo);
}
[Test]
public void Test2()
{
var foo = new Foo();
Container.BindMemoryPoolCustomInterface<Foo, Foo.Pool, Foo.IFooPool>().FromInstance(foo);
Assert.IsEqual(Container.Resolve<Foo.IFooPool>().Spawn(), foo);
}
public class Foo
{
public interface IFooFactory : IFactory<Foo>
{
}
public interface IFooPool : IMemoryPool<Foo>
{
}
public class Factory : PlaceholderFactory<Foo>, IFooFactory
{
}
public class Pool : MemoryPool<Foo>, IFooPool
{
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 3cca63c83328cfd4693f06f559c762ec
timeCreated: 1520703276
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,105 @@
using System.Collections.Generic;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestGenericContract : ZenjectUnitTestFixture
{
class Test1<T>
{
public T Data;
}
class Test2
{
}
[Test]
public void TestToSingle()
{
Container.Bind(typeof(Test1<>)).AsSingle().NonLazy();
var test1 = Container.Resolve<Test1<int>>();
Assert.That(test1.Data == 0);
test1.Data = 5;
var test2 = Container.Resolve<Test1<int>>();
Assert.That(test2 == test1);
Assert.That(test1.Data == 5);
}
[Test]
public void TestToTransient()
{
Container.Bind(typeof(Test1<>)).AsTransient().NonLazy();
var test1 = Container.Resolve<Test1<int>>();
Assert.That(test1.Data == 0);
var test2 = Container.Resolve<Test1<int>>();
Assert.That(test2.Data == 0);
Assert.That(test2 != test1);
Container.Resolve<Test1<string>>();
Container.Resolve<Test1<List<int>>>();
Container.Resolve<Test1<Test2>>();
}
interface IFoo<T>
{
}
interface IBar<T>
{
}
class Test2<T> : IFoo<T>, IBar<T>
{
}
[Test]
public void TestToSingleMultipleContracts()
{
Container.Bind(typeof(IFoo<>), typeof(IBar<>)).To(typeof(Test2<>)).AsSingle();
var foo = Container.Resolve<IFoo<int>>();
Assert.That(foo is Test2<int>);
var bar = Container.Resolve<IBar<int>>();
Assert.That(bar is Test2<int>);
Assert.IsEqual(foo, bar);
Assert.IsEqual(foo, Container.Resolve<IFoo<int>>());
Assert.IsEqual(bar, Container.Resolve<IBar<int>>());
}
public interface IQux {
}
public class Qux : IQux {
}
[Test]
public void TestToSingleMultipleContractsMismatch()
{
Container.Bind(typeof(IQux), typeof(IFoo<>), typeof(IBar<>)).To(typeof(Test2<>), typeof(Qux)).AsSingle();
var foo = Container.Resolve<IFoo<int>>();
Assert.That(foo is Test2<int>);
var bar = Container.Resolve<IBar<int>>();
Assert.That(bar is Test2<int>);
Assert.IsEqual(foo, bar);
Assert.IsEqual(foo, Container.Resolve<IFoo<int>>());
Assert.IsEqual(bar, Container.Resolve<IBar<int>>());
var qux = Container.Resolve<IQux>();
Assert.IsEqual(qux, Container.Resolve<IQux>());
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: af3b7f56a09aaa04c8eedd4f6c11945c
timeCreated: 1461708052
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,86 @@
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestKeyedFactoryExample : ZenjectUnitTestFixture
{
[Test]
public void Test1()
{
Container.BindFactory<Foo, Foo.Factory>().WithId("foo1")
.FromSubContainerResolve().ByMethod(InstallFoo1);
Container.BindFactory<Foo, Foo.Factory>().WithId("foo2")
.FromSubContainerResolve().ByMethod(InstallFoo2);
Container.Bind<Dictionary<string, IFactory<Foo>>>()
.FromMethod(GetFooFactories).WhenInjectedInto<FooFactory>();
Container.Bind<FooFactory>().AsSingle();
var keyedFactory = Container.Resolve<FooFactory>();
Assert.IsEqual(keyedFactory.Create("foo1").Number, 5);
Assert.IsEqual(keyedFactory.Create("foo2").Number, 6);
Assert.Throws(() => keyedFactory.Create("foo3"));
}
Dictionary<string, IFactory<Foo>> GetFooFactories(InjectContext ctx)
{
return ctx.Container.AllContracts.Where(
x => x.Type == typeof(Foo.Factory))
.ToDictionary(x => (string)x.Identifier, x => (IFactory<Foo>)ctx.Container.ResolveId<Foo.Factory>(x.Identifier));
}
void InstallFoo2(DiContainer subContainer)
{
subContainer.BindInstance(6);
subContainer.Bind<Foo>().AsCached();
}
void InstallFoo1(DiContainer subContainer)
{
subContainer.BindInstance(5);
subContainer.Bind<Foo>().AsCached();
}
public class FooFactory
{
readonly Dictionary<string, IFactory<Foo>> _subFactories;
public FooFactory(
Dictionary<string, IFactory<Foo>> subFactories)
{
_subFactories = subFactories;
}
public Foo Create(string key)
{
return _subFactories[key].Create();
}
}
public class Foo
{
public Foo(int number)
{
Number = number;
}
public int Number
{
get; private set;
}
public class Factory : PlaceholderFactory<Foo>
{
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: c2095fa27bc74d346a34ccfe39d21464
timeCreated: 1520700395
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,94 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Bindings.Singletons
{
[TestFixture]
public class TestLazy : ZenjectUnitTestFixture
{
[Test]
public void Test1()
{
Bar.InstanceCount = 0;
Container.Bind<Bar>().AsSingle();
Container.Bind<Foo>().AsSingle();
var foo = Container.Resolve<Foo>();
Assert.IsEqual(Bar.InstanceCount, 0);
foo.DoIt();
Assert.IsEqual(Bar.InstanceCount, 1);
}
[Test]
public void TestOptional1()
{
Container.Bind<Bar>().AsSingle();
Container.Bind<Qux>().AsSingle();
Assert.IsNotNull(Container.Resolve<Qux>().Bar.Value);
}
[Test]
public void TestOptional2()
{
Container.Bind<Qux>().AsSingle();
Assert.IsNull(Container.Resolve<Qux>().Bar.Value);
}
[Test]
public void TestOptional3()
{
Container.Bind<Gorp>().AsSingle();
var gorp = Container.Resolve<Gorp>();
object temp;
Assert.Throws(() => temp = gorp.Bar.Value);
}
public class Bar
{
public static int InstanceCount;
public Bar()
{
InstanceCount++;
}
public void DoIt()
{
}
}
public class Foo
{
readonly LazyInject<Bar> _bar;
public Foo(LazyInject<Bar> bar)
{
_bar = bar;
}
public void DoIt()
{
_bar.Value.DoIt();
}
}
public class Qux
{
[Inject(Optional = true)]
public LazyInject<Bar> Bar;
}
public class Gorp
{
public LazyInject<Bar> Bar;
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: cd58ae826c869c849a16a72b99a4c984
timeCreated: 1527488175
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,54 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestNestedContainer : ZenjectUnitTestFixture
{
public interface IFoo
{
int GetBar();
}
public class Foo : IFoo
{
public int GetBar()
{
return 0;
}
}
public class Foo2 : IFoo
{
public int GetBar()
{
return 1;
}
}
[Test]
public void TestCase1()
{
var container1 = new DiContainer();
Assert.Throws(() => container1.Resolve<IFoo>());
Assert.Throws(() => Container.Resolve<IFoo>());
Container.Bind<IFoo>().To<Foo>().AsSingle();
Assert.Throws(() => container1.Resolve<IFoo>());
Assert.IsEqual(Container.Resolve<IFoo>().GetBar(), 0);
var container2 = Container.CreateSubContainer();
Assert.IsEqual(container2.Resolve<IFoo>().GetBar(), 0);
Assert.IsEqual(Container.Resolve<IFoo>().GetBar(), 0);
container2.Bind<IFoo>().To<Foo2>().AsSingle();
Assert.IsEqual(container2.Resolve<IFoo>().GetBar(), 1);
Assert.IsEqual(Container.Resolve<IFoo>().GetBar(), 0);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9feaeae29aa84c64fb1741b2195e54ba
timeCreated: 1461708052
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,106 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestPoolableManager : ZenjectUnitTestFixture
{
static int CallCount;
public class Foo : IPoolable
{
public static int SpawnCallCount;
public static int DespawnCallCount;
public void OnSpawned()
{
SpawnCallCount = CallCount++;
}
public void OnDespawned()
{
DespawnCallCount = CallCount++;
}
}
public class Bar : IPoolable
{
public static int SpawnCallCount;
public static int DespawnCallCount;
public void OnSpawned()
{
SpawnCallCount = CallCount++;
}
public void OnDespawned()
{
DespawnCallCount = CallCount++;
}
}
[Test]
public void TestDefaultOrder()
{
Container.Bind<PoolableManager>().AsSingle();
Container.Bind<IPoolable>().To<Foo>().AsSingle();
Container.Bind<IPoolable>().To<Bar>().AsSingle();
var poolManager = Container.Resolve<PoolableManager>();
CallCount = 1;
Foo.SpawnCallCount = 0;
Foo.DespawnCallCount = 0;
Bar.SpawnCallCount = 0;
Bar.DespawnCallCount = 0;
poolManager.TriggerOnSpawned();
Assert.IsEqual(Foo.SpawnCallCount, 1);
Assert.IsEqual(Bar.SpawnCallCount, 2);
Assert.IsEqual(Foo.DespawnCallCount, 0);
Assert.IsEqual(Bar.DespawnCallCount, 0);
poolManager.TriggerOnDespawned();
Assert.IsEqual(Foo.SpawnCallCount, 1);
Assert.IsEqual(Bar.SpawnCallCount, 2);
Assert.IsEqual(Foo.DespawnCallCount, 4);
Assert.IsEqual(Bar.DespawnCallCount, 3);
}
[Test]
public void TestExplicitOrder()
{
Container.Bind<PoolableManager>().AsSingle();
Container.Bind<IPoolable>().To<Foo>().AsSingle();
Container.Bind<IPoolable>().To<Bar>().AsSingle();
Container.BindExecutionOrder<Foo>(2);
Container.BindExecutionOrder<Bar>(1);
var poolManager = Container.Resolve<PoolableManager>();
CallCount = 1;
Foo.SpawnCallCount = 0;
Foo.DespawnCallCount = 0;
Bar.SpawnCallCount = 0;
Bar.DespawnCallCount = 0;
poolManager.TriggerOnSpawned();
Assert.IsEqual(Foo.SpawnCallCount, 2);
Assert.IsEqual(Bar.SpawnCallCount, 1);
Assert.IsEqual(Foo.DespawnCallCount, 0);
Assert.IsEqual(Bar.DespawnCallCount, 0);
poolManager.TriggerOnDespawned();
Assert.IsEqual(Foo.SpawnCallCount, 2);
Assert.IsEqual(Bar.SpawnCallCount, 1);
Assert.IsEqual(Foo.DespawnCallCount, 3);
Assert.IsEqual(Bar.DespawnCallCount, 4);
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 25dab1c5f3ebf424da72c0f042790b11
timeCreated: 1528651046
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,71 @@
using System.Collections.Generic;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestResolve : ZenjectUnitTestFixture
{
class Foo
{
}
[Test]
public void TestResolveAmbiguousBindings1()
{
var f1 = new Foo();
var f2 = new Foo();
// Should always choose the binding with condition when forced to choose
Container.BindInstance(f1);
Container.BindInstance(f2).When(_ => true);
Assert.IsEqual(Container.Resolve<Foo>(), f2);
}
[Test]
public void TestResolveAmbiguousBindings2()
{
var f1 = new Foo();
var f2 = new Foo();
// Order shouldn't matter
Container.BindInstance(f2).When(_ => true);
Container.BindInstance(f1);
Assert.IsEqual(Container.Resolve<Foo>(), f2);
}
[Test]
public void TestDirectListBindings1()
{
var f1 = new Foo();
Container.BindInstance(f1);
Assert.IsEqual(Container.Instantiate<Bar>().Foos[0], f1);
var l1 = new List<Foo>();
Container.BindInstance(l1);
// Direct list bindings should override the automatic list bindings
Assert.IsEqual(Container.Instantiate<Bar>().Foos, l1);
}
class Bar
{
public List<Foo> Foos;
public Bar(List<Foo> foos)
{
Foos = foos;
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: ab22cbcf8e3080d41bd2031eb04612ee
timeCreated: 1522221939
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,42 @@
using System.Collections.Generic;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestResolveMany : ZenjectUnitTestFixture
{
class Test0
{
}
class Test1 : Test0
{
}
class Test2 : Test0
{
}
[Test]
public void TestCase1()
{
Container.Bind<Test0>().To<Test1>().AsSingle();
Container.Bind<Test0>().To<Test2>().AsSingle();
List<Test0> many = Container.ResolveAll<Test0>();
Assert.That(many.Count == 2);
}
[Test]
public void TestOptional()
{
List<Test0> many = Container.ResolveAll<Test0>();
Assert.That(many.Count == 0);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 8adbb260feb50b248943eb2fb41681cc
timeCreated: 1461708051
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,93 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestSubContainers : ZenjectUnitTestFixture
{
class Test0
{
}
[Test]
public void TestIsRemoved()
{
var subContainer = Container.CreateSubContainer();
var test1 = new Test0();
subContainer.Bind<Test0>().FromInstance(test1);
Assert.That(ReferenceEquals(test1, subContainer.Resolve<Test0>()));
Assert.Throws(
delegate { Container.Resolve<Test0>(); });
}
class Test1
{
[Inject]
public Test0 Test = null;
}
[Test]
public void TestCase2()
{
Test0 test0;
Test1 test1;
var subContainer = Container.CreateSubContainer();
var test0Local = new Test0();
subContainer.Bind<Test0>().FromInstance(test0Local);
subContainer.Bind<Test1>().AsSingle();
test0 = subContainer.Resolve<Test0>();
Assert.IsEqual(test0Local, test0);
test1 = subContainer.Resolve<Test1>();
Assert.Throws(
delegate { Container.Resolve<Test0>(); });
Assert.Throws(
delegate { Container.Resolve<Test1>(); });
Container.Bind<Test0>().AsSingle();
Container.Bind<Test1>().AsSingle();
Assert.That(Container.Resolve<Test0>() != test0);
Assert.That(Container.Resolve<Test1>() != test1);
}
interface IFoo
{
}
interface IFoo2
{
}
class Foo : IFoo, IFoo2
{
}
[Test]
public void TestMultipleSingletonDifferentScope()
{
IFoo foo1;
var subContainer1 = Container.CreateSubContainer();
subContainer1.Bind<IFoo>().To<Foo>().AsSingle();
foo1 = subContainer1.Resolve<IFoo>();
var subContainer2 = Container.CreateSubContainer();
subContainer2.Bind<IFoo>().To<Foo>().AsSingle();
var foo2 = subContainer2.Resolve<IFoo>();
Assert.That(foo2 != foo1);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: bc27ceb0add4e284880a47f778bd5433
timeCreated: 1461708053
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,82 @@
using System.Collections.Generic;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestSubContainers4 : ZenjectUnitTestFixture
{
readonly Dictionary<object, DiContainer> _subContainers = new Dictionary<object, DiContainer>();
[Test]
public void RunTest()
{
SetupContainer();
var view1 = Container.Resolve<RotorView>();
Assert.IsEqual(view1.Controller.Model, view1.Model);
var view2 = Container.Resolve<RotorView>();
Assert.IsEqual(view2.Controller.Model, view2.Model);
Assert.IsNotEqual(view2.Model, view1.Model);
Assert.IsNotEqual(view2, view1);
}
void SetupContainer()
{
Container.Bind<RotorController>().FromMethod(SubContainerResolve<RotorController>).AsTransient()
.WhenInjectedInto<RotorView>();
Container.Bind<RotorModel>().FromMethod(SubContainerResolve<RotorModel>).AsTransient()
.WhenInjectedInto<RotorView>();
Container.Bind<RotorView>().AsTransient();
}
T SubContainerResolve<T>(InjectContext context)
{
Assert.IsNotNull(context.ObjectInstance);
DiContainer subContainer;
if (!_subContainers.TryGetValue(context.ObjectInstance, out subContainer))
{
subContainer = context.Container.CreateSubContainer();
_subContainers.Add(context.ObjectInstance, subContainer);
InstallViewBindings(subContainer);
}
return (T)subContainer.Resolve(context);
}
void InstallViewBindings(DiContainer subContainer)
{
subContainer.Bind<RotorController>().AsSingle();
subContainer.Bind<RotorModel>().AsSingle();
}
public class RotorController
{
[Inject]
public RotorModel Model;
}
public class RotorView
{
[Inject]
public RotorController Controller;
[Inject]
public RotorModel Model;
}
public class RotorModel
{
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9b761fe9578b04148a779d2c323548fb
timeCreated: 1461708052
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,100 @@
using System;
using ModestTree.Util;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestTaskUpdater
{
DiContainer _container;
[SetUp]
public void Setup()
{
_container = new DiContainer();
_container.Bind<TaskUpdater<ITickable>>().FromInstance(new TickablesTaskUpdater());
}
public void BindTickable<TTickable>(int priority) where TTickable : ITickable
{
_container.BindInterfacesAndSelfTo<TTickable>().AsSingle();
_container.Bind<ValuePair<Type, int>>().FromInstance(ValuePair.New(typeof(TTickable), priority));
}
[Test]
public void TestTickablesAreOptional()
{
Assert.IsNotNull(_container.Resolve<TaskUpdater<ITickable>>());
}
[Test]
// Test that tickables get called in the correct order
public void TestOrder()
{
BindTickable<Tickable3>(2);
BindTickable<Tickable1>(0);
BindTickable<Tickable2>(1);
var taskUpdater = _container.Resolve<TaskUpdater<ITickable>>();
var tick1 = _container.Resolve<Tickable1>();
var tick2 = _container.Resolve<Tickable2>();
var tick3 = _container.Resolve<Tickable3>();
int tickCount = 0;
tick1.TickCalled += delegate
{
Assert.IsEqual(tickCount, 0);
tickCount++;
};
tick2.TickCalled += delegate
{
Assert.IsEqual(tickCount, 1);
tickCount++;
};
tick3.TickCalled += delegate
{
Assert.IsEqual(tickCount, 2);
tickCount++;
};
taskUpdater.UpdateAll();
}
class Tickable1 : ITickable
{
public event Action TickCalled = delegate {};
public void Tick()
{
TickCalled();
}
}
class Tickable2 : ITickable
{
public event Action TickCalled = delegate {};
public void Tick()
{
TickCalled();
}
}
class Tickable3 : ITickable
{
public event Action TickCalled = delegate {};
public void Tick()
{
TickCalled();
}
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 191c7c5b01ead0f4283fa0656f3310c8
timeCreated: 1461708048
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,45 @@
using System.Collections.Generic;
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests
{
[TestFixture]
public class TestTestUtil
{
[Test]
public void TestTrue()
{
Assert.That(TestListComparer.ContainSameElements(
new List<int> {1},
new List<int> {1}));
Assert.That(TestListComparer.ContainSameElements(
new List<int> {1, 2},
new List<int> {2, 1}));
Assert.That(TestListComparer.ContainSameElements(
new List<int> {1, 2, 3},
new List<int> {3, 2, 1}));
Assert.That(TestListComparer.ContainSameElements(
new List<int>(),
new List<int>()));
}
[Test]
public void TestFalse()
{
Assert.That(!TestListComparer.ContainSameElements(
new List<int> {1, 2, 3},
new List<int> {3, 2, 3}));
Assert.That(!TestListComparer.ContainSameElements(
new List<int> {1, 2},
new List<int> {1, 2, 3}));
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 248d9a33864afc54ab7c4d1a664fe81e
timeCreated: 1461708049
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,25 @@
using NUnit.Framework;
namespace Zenject.Tests.Other
{
[TestFixture]
public class TestTransientMockProvider : ZenjectUnitTestFixture
{
public interface IFoo
{
int GetBar();
}
[Test]
public void TestCase1()
{
// Commented out because this requires that zenject be installed with mocking support which isn't always the case
//Container.FallbackProvider = new TransientMockProvider(Container);
//var foo = Container.Resolve<IFoo>();
//Assert.IsEqual(foo.GetBar(), 0);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 669690277b55b8c419f4d34dbef0cbde
timeCreated: 1461708051
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,226 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests
{
[TestFixture]
public class TestValidation
{
DiContainer Container
{
get; set;
}
[SetUp]
public void Setup()
{
Container = new DiContainer(true);
Container.Settings = new ZenjectSettings(
ValidationErrorResponses.Throw, RootResolveMethods.All);
}
[Test]
public void TestFailure()
{
Container.Bind<Bar>().AsSingle();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestSuccess()
{
Container.Bind<Foo>().AsSingle();
Container.Bind<Bar>().AsSingle();
Container.ResolveRoots();
}
[Test]
public void TestNumCalls()
{
Gorp.CallCount = 0;
Container.BindInterfacesAndSelfTo<Gorp>().AsSingle();
Container.ResolveRoots();
Assert.IsEqual(Gorp.CallCount, 1);
}
[Test]
public void TestFactoryFail()
{
Container.BindFactory<Bar, Bar.Factory>();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestFactorySuccess()
{
Container.Bind<Foo>().AsSingle();
Container.BindFactory<Bar, Bar.Factory>();
Container.ResolveRoots();
}
[Test]
public void TestSubContainerMethodSuccess()
{
Container.Bind<Qux>().FromSubContainerResolve().ByMethod(
container =>
{
container.Bind<Qux>().AsSingle();
container.Bind<Foo>().AsSingle();
container.Bind<Bar>().AsSingle();
})
.AsSingle();
Container.ResolveRoots();
}
[Test]
public void TestSubContainerMethodFailure()
{
Container.Bind<Qux>().FromSubContainerResolve().ByMethod(
container =>
{
container.Bind<Qux>().AsSingle();
container.Bind<Bar>().AsSingle();
})
.AsSingle();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestSubContainerInstallerFailure()
{
Container.Bind<Qux>().FromSubContainerResolve().ByInstaller<QuxInstaller>().AsSingle();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestLazyFail()
{
Container.Bind<Jaze>().AsSingle();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestLazySuccess()
{
Container.Bind<Qux>().AsSingle();
Container.Bind<Jaze>().AsSingle();
Container.ResolveRoots();
}
[Test]
public void TestMemoryPoolFailure()
{
Container.BindMemoryPool<Bar, Bar.Pool>();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestMemoryPoolSuccess()
{
Container.Bind<Foo>().AsSingle();
Container.BindMemoryPool<Bar, Bar.Pool>();
Container.ResolveRoots();
}
[Test]
public void TestCustomValidatable()
{
Container.BindInterfacesAndSelfTo<Loy>().AsSingle().NonLazy();
Container.ResolveRoots();
Assert.IsEqual(Container.Resolve<Loy>().CallCount, 1);
}
public class Loy : IValidatable, IInitializable, ITickable
{
public int CallCount
{
get; set;
}
public void Initialize()
{
}
public void Tick()
{
}
public void Validate()
{
CallCount++;
}
}
public class Jaze
{
[Inject]
public LazyInject<Qux> Qux;
}
public class QuxInstaller : Installer<QuxInstaller>
{
public override void InstallBindings()
{
Container.Bind<Qux>().AsSingle();
Container.Bind<Bar>().AsSingle();
}
}
public class Qux
{
}
public class Bar
{
public Bar(Foo foo)
{
}
public class Factory : PlaceholderFactory<Bar>
{
}
public class Pool : MemoryPool<Bar>
{
}
}
public class Foo
{
}
public interface IGorp
{
}
public class Gorp : IGorp, IValidatable
{
public static int CallCount
{
get; set;
}
public void Validate()
{
CallCount++;
}
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 6faaf38387f08ff4ab2b1aeaa3aa6447
timeCreated: 1527489133
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,74 @@
using NUnit.Framework;
using Assert = ModestTree.Assert;
namespace Zenject.Tests
{
[TestFixture]
public class TestValidationSettings
{
DiContainer Container
{
get; set;
}
[SetUp]
public void Setup()
{
Container = new DiContainer(true);
}
// Doesn't work because the logged error is flagged as a test error
//[Test]
//public void TestValidationErrorLogOnly()
//{
//Container.Settings = new ZenjectSettings(ValidationErrorResponses.Log);
//Container.Bind<Bar>().AsSingle().NonLazy();
//Container.ResolveRoots();
//}
[Test]
public void TestValidationErrorThrows()
{
Container.Settings = new ZenjectSettings(ValidationErrorResponses.Throw);
Container.Bind<Bar>().AsSingle().NonLazy();
Assert.Throws(() => Container.ResolveRoots());
}
[Test]
public void TestOutsideObjectGraph1()
{
Container.Settings = new ZenjectSettings(ValidationErrorResponses.Throw);
Container.Bind<Bar>().AsSingle();
Container.ResolveRoots();
}
[Test]
public void TestOutsideObjectGraph2()
{
Container.Settings = new ZenjectSettings(
ValidationErrorResponses.Throw, RootResolveMethods.All);
Container.Bind<Bar>().AsSingle();
Assert.Throws(() => Container.ResolveRoots());
}
public class Bar
{
public Bar(Foo foo)
{
}
}
public class Foo
{
}
}
}
@@ -0,0 +1,13 @@
fileFormatVersion: 2
guid: 9b700b061b5fac341a8668b2329c3c5e
timeCreated: 1527606589
licenseType: Free
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,90 @@
using NUnit.Framework;
namespace Zenject.Tests.Other
{
[TestFixture]
public class ZenjectProfileTest
{
class Test0
{
public void DoStuff()
{
}
public void DoStuff1()
{
}
}
class Test1
{
[Inject]
public Test0 TestB
{
set;
get;
}
[Inject]
public Test0 _testC = null;
public Test1(Test0 test1, Test0 test2, Test0 test3, Test0 test4)
{
}
public void DoStuff()
{
}
public void DoStuff1()
{
}
}
class Test2
{
[Inject]
public Test1 TestB
{
set;
get;
}
[Inject]
public Test1 _testC = null;
public Test2(Test1 test1, Test1 test2, Test1 test3, Test1 test4)
{
}
}
[Test]
// Test speed of resolve function
public void Test()
{
//var container = new DiContainer();
//container.Bind<Test0>().AsTransient();
//container.Bind<Test1>().AsTransient();
//container.Bind<Test2>().AsTransient();
//var stopwatch = new Stopwatch();
//stopwatch.Start();
//for (int i = 0; i < 1000; i++)
//{
//var test0 = container.Resolve<Test2>();
//var test1 = container.Resolve<Test2>();
//var test2 = container.Resolve<Test2>();
//}
//stopwatch.Stop();
//Log.InfoFormat("time = {0}", stopwatch.Elapsed.TotalSeconds);
//Assert.That(false);
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 33e5ecde955b52a42bf3454813d30435
timeCreated: 1461708049
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: