public void CreateChildFromPrefabWithInActive() { var parent = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleEntryPoint>().AsSelf(); }); var childPrefab = new GameObject("Child").AddComponent <SampleChildLifetimeScope>(); childPrefab.gameObject.SetActive(false); var child = parent.CreateChildFromPrefab(childPrefab); Assert.That(childPrefab.gameObject.activeSelf, Is.False); Assert.That(child.gameObject.activeSelf, Is.False); child.Build(); var parentResolved = parent.Container.Resolve <SampleEntryPoint>(); var childResolved = child.Container.Resolve <SampleEntryPoint>(); Assert.That(parentResolved, Is.InstanceOf <SampleEntryPoint>()); Assert.That(childResolved, Is.InstanceOf <SampleEntryPoint>()); Assert.That(childResolved, Is.EqualTo(parentResolved)); Assert.That(child.parentReference.Object, Is.EqualTo(parent)); }
public IEnumerator Create() { var lifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleEntryPoint>(Lifetime.Scoped).AsSelf(); builder.Register <DisposableServiceA>(Lifetime.Scoped); }); yield return(null); yield return(null); var entryPoint = lifetimeScope.Container.Resolve <SampleEntryPoint>(); Assert.That(entryPoint, Is.InstanceOf <SampleEntryPoint>()); Assert.That(entryPoint.InitializeCalled, Is.True); Assert.That(entryPoint.PostInitializeCalled, Is.True); Assert.That(entryPoint.StartCalled, Is.True); Assert.That(entryPoint.PostStartCalled, Is.True); Assert.That(entryPoint.TickCalls, Is.EqualTo(2)); var disposable = lifetimeScope.Container.Resolve <DisposableServiceA>(); lifetimeScope.Dispose(); yield return(null); Assert.That(disposable.Disposed, Is.True); Assert.That(entryPoint.TickCalls, Is.EqualTo(2)); }
public void RegisterComponentInHierarchyAutoInject() { var go1 = new GameObject("SampleMonoBehaviour"); var target = go1.AddComponent <SampleMonoBehaviour>(); var lifetimeScope = LifetimeScope.Create(builder => { builder.Register <ServiceA>(Lifetime.Transient); builder.RegisterComponentInHierarchy <SampleMonoBehaviour>() .UnderTransform(go1.transform); }); Assert.That(target.ServiceA, Is.InstanceOf <ServiceA>()); }
public IEnumerator Create() { var lifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleEntryPoint>().AsSelf(); builder.Register <DisposableServiceA>(Lifetime.Scoped); }); yield return(null); var entryPoint = lifetimeScope.Container.Resolve <SampleEntryPoint>(); Assert.That(entryPoint, Is.InstanceOf <SampleEntryPoint>()); Assert.That(entryPoint.InitializeCalled, Is.EqualTo(1)); Assert.That(entryPoint.PostInitializeCalled, Is.EqualTo(1)); Assert.That(entryPoint.StartCalled, Is.EqualTo(1)); Assert.That(entryPoint.PostStartCalled, Is.EqualTo(1)); yield return(new WaitForFixedUpdate()); yield return(new WaitForFixedUpdate()); Assert.That(entryPoint.FixedTickCalls, Is.GreaterThan(0)); Assert.That(entryPoint.PostFixedTickCalls, Is.GreaterThan(0)); var fixedTickCalls = entryPoint.FixedTickCalls; var postFixedTickCalls = entryPoint.PostFixedTickCalls; var tickCalls = entryPoint.TickCalls; var postTickCalls = entryPoint.PostTickCalls; var lateTickCalls = entryPoint.LateTickCalls; var postLateTickCalls = entryPoint.PostLateTickCalls; var disposable = lifetimeScope.Container.Resolve <DisposableServiceA>(); lifetimeScope.Dispose(); yield return(null); yield return(new WaitForFixedUpdate()); Assert.That(fixedTickCalls, Is.EqualTo(fixedTickCalls)); Assert.That(postFixedTickCalls, Is.EqualTo(postFixedTickCalls)); Assert.That(tickCalls, Is.EqualTo(tickCalls)); Assert.That(postTickCalls, Is.EqualTo(postTickCalls)); Assert.That(lateTickCalls, Is.EqualTo(lateTickCalls)); Assert.That(postLateTickCalls, Is.EqualTo(postLateTickCalls)); Assert.That(disposable.Disposed, Is.True); }
public IEnumerator AsyncStartup() => UniTask.ToCoroutine(async() => { var lifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleAsyncStartable>(Lifetime.Scoped) .AsSelf(); }); var entryPoint = lifetimeScope.Container.Resolve <SampleAsyncStartable>(); Assert.That(entryPoint.Started, Is.False); await UniTask.Yield(); await UniTask.Yield(); Assert.That(entryPoint.Started, Is.True); });
public void RegisterComponentInHierarchyWithBuiltinType() { var go1 = new GameObject("Parent"); go1.AddComponent <BoxCollider>(); var lifetimeScope = LifetimeScope.Create(builder => { builder.Register <ServiceA>(Lifetime.Transient); builder.RegisterComponentInHierarchy <BoxCollider>(); }); var resolved = lifetimeScope.Container.Resolve <BoxCollider>(); Assert.That(resolved, Is.InstanceOf <BoxCollider>()); }
public IEnumerator StartableExceptionHandler() { var handled = 0; LifetimeScope.Create(builder => { builder.RegisterEntryPoint <StartableThrowable>(); builder.RegisterEntryPointExceptionHandler(ex => { handled += 1; }); }); yield return(null); yield return(null); Assert.That(handled, Is.EqualTo(1)); }
public IEnumerator CreateChild() { var parentLifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleEntryPoint>(Lifetime.Scoped).AsSelf(); }); yield return(null); yield return(null); var parentEntryPoint = parentLifetimeScope.Container.Resolve <SampleEntryPoint>(); var parentLifetimeScopeFromContainer = parentLifetimeScope.Container.Resolve <LifetimeScope>(); Assert.That(parentLifetimeScopeFromContainer, Is.EqualTo(parentLifetimeScope)); Assert.That(parentLifetimeScopeFromContainer.transform.childCount, Is.Zero); Assert.That(parentEntryPoint, Is.InstanceOf <SampleEntryPoint>()); Assert.That(parentEntryPoint.InitializeCalled, Is.True); Assert.That(parentEntryPoint.TickCalls, Is.EqualTo(2)); var childLifetimeScope = parentLifetimeScope.CreateChild(builder => { builder.RegisterEntryPoint <SampleEntryPoint>(Lifetime.Scoped).AsSelf(); builder.Register <DisposableServiceA>(Lifetime.Scoped); }); yield return(null); yield return(null); var childEntryPoint = childLifetimeScope.Container.Resolve <SampleEntryPoint>(); var childDisposable = childLifetimeScope.Container.Resolve <DisposableServiceA>(); var childLifetimeScopeFromContainer = childLifetimeScope.Container.Resolve <LifetimeScope>(); Assert.That(childLifetimeScopeFromContainer, Is.EqualTo(childLifetimeScope)); Assert.That(childEntryPoint, Is.InstanceOf <SampleEntryPoint>()); Assert.That(childEntryPoint, Is.Not.EqualTo(parentEntryPoint)); Assert.That(childEntryPoint.InitializeCalled, Is.True); Assert.That(childEntryPoint.TickCalls, Is.EqualTo(2)); childLifetimeScope.Dispose(); yield return(null); Assert.That(childDisposable.Disposed, Is.True); Assert.That(childLifetimeScope == null, Is.True); Assert.That(parentLifetimeScope.transform.childCount, Is.Zero); }
public void CreateScopeWithSingleton() { var parentLifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleEntryPoint>(Lifetime.Singleton).AsSelf(); }); var parentEntryPoint = parentLifetimeScope.Container.Resolve <SampleEntryPoint>(); Assert.That(parentEntryPoint, Is.InstanceOf <SampleEntryPoint>()); var childLifetimeScope = parentLifetimeScope.CreateChild(); var childEntryPoint = childLifetimeScope.Container.Resolve <SampleEntryPoint>(); Assert.That(childEntryPoint, Is.InstanceOf <SampleEntryPoint>()); Assert.That(childEntryPoint, Is.EqualTo(parentEntryPoint)); }
public IEnumerator AsyncStartupExceptionHandler() => UniTask.ToCoroutine(async() => { Exception caught = null; var lifetimeScope = LifetimeScope.Create(builder => { builder.RegisterEntryPoint <SampleAsyncStartableThrowable>(Lifetime.Scoped) .AsSelf(); builder.RegisterEntryPointExceptionHandler(ex => caught = ex); }); var entryPoint = lifetimeScope.Container.Resolve <SampleAsyncStartableThrowable>(); Assert.That(entryPoint, Is.InstanceOf <SampleAsyncStartableThrowable>()); await UniTask.Yield(); await UniTask.Yield(); Assert.That(caught, Is.InstanceOf <InvalidOperationException>()); });
public void UseComponentsWithParentTransform() { var go1 = new GameObject("Parent"); var go2 = new GameObject("Child A"); var go3 = new GameObject("Child B"); go3.transform.SetParent(go2.transform); go2.transform.SetParent(go1.transform); go1.AddComponent <SampleMonoBehaviour>(); go3.AddComponent <SampleMonoBehaviour>(); var lifetimeScope = LifetimeScope.Create(builder => { builder.Register <ServiceA>(Lifetime.Scoped); builder.Register <ServiceB>(Lifetime.Scoped); builder.UseComponents(go2.transform, components => { components.AddInHierarchy <SampleMonoBehaviour>(); }); builder.UseComponents(go3.transform, components => { components.AddOnNewGameObject <SampleMonoBehaviour2>(Lifetime.Scoped); }); }); var found = lifetimeScope.Container.Resolve <SampleMonoBehaviour>(); Assert.That(found, Is.InstanceOf <SampleMonoBehaviour>()); Assert.That(found.transform.parent, Is.EqualTo(go2.transform)); var created = lifetimeScope.Container.Resolve <SampleMonoBehaviour2>(); Assert.That(created, Is.InstanceOf <SampleMonoBehaviour2>()); Assert.That(created.transform.parent, Is.EqualTo(go3.transform)); }
public void RegisterComponentInHierarchy() { var go1 = new GameObject("Parent"); var go2 = new GameObject("Child A"); var go3 = new GameObject("Child B"); go3.transform.SetParent(go2.transform); go2.transform.SetParent(go1.transform); go3.AddComponent <SampleMonoBehaviour>(); var lifetimeScope = LifetimeScope.Create(builder => { builder.Register <ServiceA>(Lifetime.Transient); builder.RegisterComponentInHierarchy <SampleMonoBehaviour>(); }); var resolved = lifetimeScope.Container.Resolve <SampleMonoBehaviour>(); Assert.That(resolved, Is.InstanceOf <SampleMonoBehaviour>()); Assert.That(resolved.ServiceA, Is.InstanceOf <ServiceA>()); Assert.That(resolved.transform, Is.EqualTo(go3.transform)); }