public void RegisterForInterface_Implementation_SuccessfullyCreateWithNonDefaultConstructor() { var wrapType = OmniShim.RegisterForInterface <SuccessfulRegisterClass>("omnishim_test_core.ISimpleInterface"); ISimpleInterface transform = (ISimpleInterface)Activator.CreateInstance(wrapType, "test2"); Assert.AreEqual("test2", transform.DoThing("test1")); }
public MultipleConstructorsUntagged (ISimpleInterface simple, int intValue, string stringValue, float floatValue) { this.simple = simple; this.intValue = intValue; this.stringValue = stringValue; this.floatValue = floatValue; }
public void TestRuntimeSequenceCommandBinding() { string jsonInjectorString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\", \"Options\":\"ToSingleton\"}]"; injectionBinder.ConsumeBindings(jsonInjectorString); string jsonCommandString = "[{\"Bind\":\"TestEvent\",\"To\":[\"strange.unittests.CommandWithInjection\",\"strange.unittests.CommandWithExecute\",\"strange.unittests.CommandThatThrows\"],\"Options\":\"InSequence\"}]"; commandBinder.ConsumeBindings(jsonCommandString); ICommandBinding binding = commandBinder.GetBinding("TestEvent") as ICommandBinding; Assert.IsTrue(binding.isSequence); TestDelegate testDelegate = delegate { commandBinder.ReactTo("TestEvent"); }; //That the exception is thrown demonstrates that the last command ran NotImplementedException ex = Assert.Throws <NotImplementedException> (testDelegate); Assert.NotNull(ex); ISimpleInterface instance = injectionBinder.GetInstance <ISimpleInterface>() as ISimpleInterface; Assert.AreEqual(100, instance.intValue); }
public void Implementation_MultipleManagers_DifferentInterfaces() { // Arrange Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>(); observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>()); observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>()); observableMock.Setup(x => x.TryGetMember(It.IsAny <string>(), out It.Ref <object> .IsAny)).Returns(true); Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>(); // Act var manager = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object); var manager2 = new RuntimeProxyManager <IInterfaceWithNestedObservable>(observableMock.Object); manager.Subscribe(subscriberMock.Object); manager2.Subscribe(subscriberMock.Object); ISimpleInterface impl = manager.Implementation; IInterfaceWithNestedObservable impl2 = manager2.Implementation; Trace.WriteLine(impl.StringValue); Trace.WriteLine(impl2.NestedObservable); // Assert subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(ISimpleInterface.StringValue))), Times.Once); subscriberMock.Verify(x => x.OnPropertyAccessed(It.Is <PropertyAccessedArgs>(args => args.PropertyName == nameof(IInterfaceWithNestedObservable.NestedObservable))), Times.Once); }
public void BuildRuntimeType_ReferenceType_SetValue() { // Arrange string value = "test value"; object setValue = null; Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict); managerMock.SetupGet(x => x.Implementation).Returns(null); managerMock.Setup(x => x.GetValue(nameof(ISimpleInterface.StringValue))) .Returns(() => setValue); managerMock.Setup(x => x.SetValue(nameof(ISimpleInterface.StringValue), It.IsAny <object>())) .Callback <string, object>((name, value) => setValue = value); var manager = managerMock.Object; MethodInfo getMethod = manager.GetType().GetMethod("GetValue"); MethodInfo setMethod = manager.GetType().GetMethod("SetValue"); // Act Stopwatch sw = Stopwatch.StartNew(); Type runtimeType = RuntimeProxyBuilder.BuildRuntimeType(typeof(ISimpleInterface), getMethod, setMethod); ISimpleInterface impl = (ISimpleInterface)Activator.CreateInstance(runtimeType, manager); impl.StringValue = value; sw.Stop(); Trace.WriteLine(sw.ElapsedMilliseconds); // Assert Assert.AreEqual(value, impl.StringValue); }
public void TestNamedFactories() { binder.Bind <int> ().ToValue(20); binder.Bind <ISimpleInterface> ().To <SimpleInterfaceImplementer>().ToName(SomeEnum.ONE); binder.Bind <ISimpleInterface> ().To <SimpleInterfaceImplementerTwo>().ToName(SomeEnum.TWO); ISimpleInterface valueOneOne = binder.GetInstance <ISimpleInterface> (SomeEnum.ONE) as ISimpleInterface; ISimpleInterface valueOneTwo = binder.GetInstance <ISimpleInterface> (SomeEnum.ONE) as ISimpleInterface; ISimpleInterface valueTwoOne = binder.GetInstance <ISimpleInterface> (SomeEnum.TWO) as ISimpleInterface; ISimpleInterface valueTwoTwo = binder.GetInstance <ISimpleInterface> (SomeEnum.TWO) as ISimpleInterface; //Of course nothing should return null Assert.NotNull(valueOneOne); Assert.NotNull(valueOneTwo); Assert.NotNull(valueTwoOne); Assert.NotNull(valueTwoTwo); //All four instances should be unique. Assert.AreNotSame(valueOneOne, valueOneTwo); Assert.AreNotSame(valueOneTwo, valueTwoOne); Assert.AreNotSame(valueTwoOne, valueTwoTwo); Assert.AreNotSame(valueOneOne, valueTwoTwo); //First pair should be of type SimpleInterfaceImplementer. Assert.IsInstanceOf <SimpleInterfaceImplementer> (valueOneOne); Assert.IsInstanceOf <SimpleInterfaceImplementer> (valueOneTwo); //Second pair should be of type SimpleInterfaceImplementerTwo. Assert.IsInstanceOf <SimpleInterfaceImplementerTwo> (valueTwoOne); Assert.IsInstanceOf <SimpleInterfaceImplementerTwo> (valueTwoTwo); }
public void BuildRuntimeType_ReferenceType_GetValue() { // Arrange string value = "test value"; Mock <IMockableRuntimeTypePropertyManager> managerMock = new Mock <IMockableRuntimeTypePropertyManager>(MockBehavior.Strict); managerMock.Setup(x => x.GetValue(nameof(ISimpleInterface.StringValue))) .Returns(value); var manager = managerMock.Object; MethodInfo getMethod = manager.GetType().GetMethod("GetValue"); MethodInfo setMethod = manager.GetType().GetMethod("SetValue"); // Act Stopwatch sw = Stopwatch.StartNew(); Type runtimeType = RuntimeProxyBuilder.BuildRuntimeType(typeof(ISimpleInterface), getMethod, setMethod); ISimpleInterface impl = (ISimpleInterface)Activator.CreateInstance(runtimeType, manager); string returnedValue = impl.StringValue; sw.Stop(); Trace.WriteLine(sw.ElapsedMilliseconds); // Assert Assert.AreEqual(value, returnedValue); }
public MultipleConstructorsUntagged(ISimpleInterface simple, int intValue, string stringValue, float floatValue) { this.simple = simple; this.intValue = intValue; this.stringValue = stringValue; this.floatValue = floatValue; }
public ClientConcreteImplementation(ISimpleInterface interfaceRefrence, ConcreteImplement Implementator) { InterfaceRefrence = interfaceRefrence; var s = InterfaceRefrence.Property1; InterfaceRefrence.ThisMethodRequireImplementation(); }
public void TestSimpleProxy() { SimpleClass testClass = new SimpleClass(); ISimpleInterface testClassProxy = (ISimpleInterface) new EasyProxy(InvocationHandler).Create(testClass); TestCreatedSimpleProxy(testClassProxy); }
protected static void TestCreatedSimpleProxy(ISimpleInterface testClassProxy) { testClassProxy.Should().Not.Be.Null(); // No test for method 1, just make sure it doesn't bomb ;-) testClassProxy.Method1(); testClassProxy.Method2().Should().Be("Hello World!"); testClassProxy.Method3().Should().Be(10000); testClassProxy.Method4(123456).Should().Be(123456); int outValue = 1234; testClassProxy.Method5(3456, out outValue); outValue.Should().Be(3456); int refValue = 56748; testClassProxy.Method6(ref refValue); refValue.Should().Be(98765); // Test casting IImplemented implementedInterface = (IImplemented)testClassProxy; implementedInterface.Should().Not.Be.Null(); implementedInterface.ImplementedMethod(); // Test IDynamicProxy test IDynamicProxy dynProxy = (IDynamicProxy)testClassProxy; dynProxy.Should().Not.Be.Null(); }
public void TestPrereflectAll() { binder.Bind <HasNamedInjections> ().To <HasNamedInjections> (); binder.Bind <ISimpleInterface> ().To <SimpleInterfaceImplementer> ().ToName(SomeEnum.ONE); binder.Bind <ISimpleInterface> ().To <PolymorphicClass> (); binder.Bind <InjectableSuperClass> ().To <InjectableDerivedClass> (); binder.Bind <int>().ToValue(42); binder.Bind <string>().ToValue("zaphod"); //primitives won't get reflected... int count = binder.ReflectAll(); Assert.AreEqual(4, count); //...so list length will not include primitives ISimpleInterface s = binder.GetInstance <ISimpleInterface> () as ISimpleInterface; Assert.IsTrue(s is PolymorphicClass); IReflectedClass reflected1 = binder.injector.reflector.Get <HasNamedInjections> (); Assert.True(reflected1.preGenerated); IReflectedClass reflected2 = binder.injector.reflector.Get <SimpleInterfaceImplementer> (); Assert.True(reflected2.preGenerated); IReflectedClass reflected3 = binder.injector.reflector.Get <PolymorphicClass> (); Assert.True(reflected3.preGenerated); Assert.AreNotEqual(reflected2.constructor, reflected3.constructor); IReflectedClass reflected4 = binder.injector.reflector.Get <InjectableDerivedClass> (); Assert.True(reflected4.preGenerated); }
public void Simple() { IServiceProvider serviceProvider = Build(services => services.AddSingleton <ISimpleInterface, SimpleInterface>()); ISimpleInterface simpleInterface = serviceProvider.GetRequiredService <ISimpleInterface>(); Assert.NotNull(simpleInterface); }
public void TestUnnamedInstanceBeforeNamedInstance() { binder.Bind <ISimpleInterface> ().To <PolymorphicClass> (); binder.Bind <ISimpleInterface> ().To <SimpleInterfaceImplementer> ().ToName(SomeEnum.ONE); ISimpleInterface instance1 = binder.GetInstance <ISimpleInterface> (SomeEnum.ONE) as ISimpleInterface; ISimpleInterface instance2 = binder.GetInstance <ISimpleInterface> () as ISimpleInterface; Assert.That(instance1 is SimpleInterfaceImplementer); Assert.That(instance2 is PolymorphicClass); }
public void y() { var implementation = new ExplicitImplementation(); //You cannot call the method if both are explictedly implemented unless you first clear up what type it's representing //Assert.Equal("simple", implementation.GetName()); ISimpleInterface simple = implementation; ISimiliarInterface similiar = implementation; Assert.Equal("simple", simple.GetName()); Assert.Equal("similiar", similiar.GetName()); }
public void TestInvalidCastWithoutStrict() { SimpleClass testClass = new SimpleClass(); ISimpleInterface testClassProxy = (ISimpleInterface) new EasyProxy(InvocationHandler).Create(testClass); testClassProxy.Should().Not.Be.Null(); // Test invalid cast INotImplemented notImplementedInterface = (INotImplemented)testClassProxy; notImplementedInterface.NotImplementedMethod(); }
public void BasicCase() { ProxyGenerationOptions options = new ProxyGenerationOptions(); options.Selector = new AllInterceptorSelector(); ISimpleInterface target = generator.CreateInterfaceProxyWithTarget(typeof(ISimpleInterface), new SimpleClass(), options, new StandardInterceptor()) as ISimpleInterface; Assert.IsNotNull(target); target.Do(); }
public ExplicitInterfaceClient(ExplicitInterfaceImplementation implementationReference, ISimpleInterface interfaceReference) { // Uncommenting this will cause compilation errors. //var instancePropertyValue = implementationReference.ThisIntegerPropertyOnlyNeedsAGetter; //implementationReference.ThisMethodRequiresImplementation(); //implementationReference.ThisStringPropertyNeedsImplementingToo = "Hello"; //implementationReference.InterfacesCanContainEventsToo += EventHandler; var interfacePropertyValue = interfaceReference.ThisIntegerPropertyOnlyNeedsAGetter; interfaceReference.ThisMethodRequiresImplementation(); interfaceReference.ThisStringPropertyNeedsImplementingToo = "Hello"; interfaceReference.InterfacesCanContainEventsToo += EventHandler; }
public void TestExecuteWithInjection() { //CommandWithInjection requires an ISimpleInterface injectionBinder.Bind <ISimpleInterface>().To <SimpleInterfaceImplementer> ().ToSingleton(); //Bind the trigger to the command commandBinder.Bind(SomeEnum.ONE).To <CommandWithInjection>(); commandBinder.ReactTo(SomeEnum.ONE); //The command should set the value to 100 ISimpleInterface instance = injectionBinder.GetInstance <ISimpleInterface>() as ISimpleInterface; Assert.AreEqual(100, instance.intValue); }
public void CreateTypeForSimpleInterface() { Type iClass = DynamicTypeGenerator.GetTypeForInterface(typeof(ISimpleInterface)); ISimpleInterface instance = (ISimpleInterface)Activator.CreateInstance(iClass); instance.ShouldNotBeNull(); instance.Id = 42; instance.Name = "Lorem ipsum"; instance.Id.ShouldBe(42); instance.Name.ShouldBe("Lorem ipsum"); }
public void TestPolymorphicBinding() { binder.Bind <ISimpleInterface> ().Bind <IAnotherSimpleInterface> ().To <PolymorphicClass> (); ISimpleInterface callOnce = binder.GetInstance <ISimpleInterface> () as ISimpleInterface; Assert.NotNull(callOnce); Assert.IsInstanceOf <PolymorphicClass> (callOnce); IAnotherSimpleInterface callAgain = binder.GetInstance <IAnotherSimpleInterface> () as IAnotherSimpleInterface; Assert.NotNull(callAgain); Assert.IsInstanceOf <PolymorphicClass> (callAgain); }
public void TestSimpleRuntimeCommandBinding() { string jsonInjectorString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\", \"Options\":\"ToSingleton\"}]"; injectionBinder.ConsumeBindings(jsonInjectorString); string jsonCommandString = "[{\"Bind\":\"strange.unittests.SomeEnum.ONE\",\"To\":\"strange.unittests.CommandWithInjection\"}]"; commandBinder.ConsumeBindings(jsonCommandString); commandBinder.ReactTo(SomeEnum.ONE); ISimpleInterface instance = injectionBinder.GetInstance <ISimpleInterface>() as ISimpleInterface; Assert.AreEqual(100, instance.intValue); }
public void TestSimpleRuntimeInjection() { string jsonString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\"}]"; binder.ConsumeBindings(jsonString); IBinding binding = binder.GetBinding <ISimpleInterface> (); Assert.NotNull(binding); Assert.AreEqual((binding as IInjectionBinding).type, InjectionBindingType.DEFAULT); ISimpleInterface instance = binder.GetInstance(typeof(ISimpleInterface)) as ISimpleInterface; Assert.IsInstanceOf <SimpleInterfaceImplementer> (instance); }
public ExplicitInterfaceClient(ExplicitInterfaceImplementation implementationReference , ISimpleInterface interfaceReference) { //var instancePropertyValue = // implementationReference.ThisIntegerPropertyOnlyNeedsAGetter; //implementationReference.ThisMethodRequiresImplementation(); //implementationReference.ThisStringPropertyNeedsImplementingToo = "안녕하세요"; //implementationReference.InterfaceCanContainEventsToo += EventHandler; var interfacePropertyValue = interfaceReference.ThisIntegerPropertyOnlyNeedsAGetter; interfaceReference.ThisMethodRequiresImplementation(); interfaceReference.ThisStringPropertyNeedsImplementingToo = "안녕하세요"; interfaceReference.InterfaceCanContainEventsToo += EventHandler; }
public void Implementation_Manager_IsCorrectInstance() { // Arrange Mock <IObservableProperty> observableMock = new Mock <IObservableProperty>(); observableMock.Setup(x => x.GetObservedProperties()).Returns(new Dictionary <string, IObservableProperty>()); observableMock.Setup(x => x.GetObservedCollections()).Returns(new Dictionary <string, IObservableCollection>()); Mock <IPropertyAccessedSubscriber> subscriberMock = new Mock <IPropertyAccessedSubscriber>(); // Act var manager = new RuntimeProxyManager <ISimpleInterface>(observableMock.Object); ISimpleInterface impl = manager.Implementation; // Assert Assert.AreEqual(manager, ((IRuntimeProxy)impl).Manager); }
public void SelectorWorksForMethods() { ProxyGenerationOptions options = new ProxyGenerationOptions(); CallCountingInterceptor countingInterceptor = new CallCountingInterceptor(); options.Selector = new TypeInterceptorSelector <CallCountingInterceptor>(); ISimpleInterface target = generator.CreateInterfaceProxyWithTarget(typeof(ISimpleInterface), new SimpleClass(), options, new AddTwoInterceptor(), countingInterceptor) as ISimpleInterface; Assert.IsNotNull(target); int result = target.Do(); Assert.AreEqual(3, result); Assert.AreEqual(1, countingInterceptor.Count); }
public void TestRuntimeInjectionCrossContext() { string jsonString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\", \"Options\":[\"ToSingleton\",\"Weak\",\"CrossContext\"]}]"; binder.ConsumeBindings(jsonString); IBinding binding = binder.GetBinding <ISimpleInterface> (); Assert.NotNull(binding); Assert.IsTrue((binding as IInjectionBinding).isCrossContext); Assert.IsTrue(binding.isWeak); Assert.AreEqual((binding as IInjectionBinding).type, InjectionBindingType.SINGLETON); ISimpleInterface instance = binder.GetInstance(typeof(ISimpleInterface)) as ISimpleInterface; Assert.IsInstanceOf <SimpleInterfaceImplementer> (instance); }
public void TestRuntimeCommandWithPooling() { string jsonInjectorString = "[{\"Bind\":\"strange.unittests.ISimpleInterface\",\"To\":\"strange.unittests.SimpleInterfaceImplementer\", \"Options\":\"ToSingleton\"}]"; injectionBinder.ConsumeBindings(jsonInjectorString); string jsonCommandString = "[{\"Bind\":\"TestEvent\",\"To\":\"strange.unittests.CommandWithInjection\", \"Options\":\"Pooled\"}]"; commandBinder.ConsumeBindings(jsonCommandString); ICommandBinding binding = commandBinder.GetBinding("TestEvent") as ICommandBinding; Assert.IsTrue(binding.isPooled); commandBinder.ReactTo("TestEvent"); ISimpleInterface instance = injectionBinder.GetInstance <ISimpleInterface>() as ISimpleInterface; Assert.AreEqual(100, instance.intValue); }
public void TestRuntimeInjectionBindToSelf() { string jsonString = "[{\"Bind\":\"strange.unittests.SimpleInterfaceImplementer\"}]"; binder.ConsumeBindings(jsonString); IBinding binding = binder.GetBinding <SimpleInterfaceImplementer> (); Assert.NotNull(binding); Assert.AreEqual((binding as IInjectionBinding).type, InjectionBindingType.DEFAULT); SimpleInterfaceImplementer instance = binder.GetInstance(typeof(SimpleInterfaceImplementer)) as SimpleInterfaceImplementer; Assert.IsInstanceOf <SimpleInterfaceImplementer> (instance); ISimpleInterface instance2 = binder.GetInstance(typeof(SimpleInterfaceImplementer)) as ISimpleInterface; Assert.AreNotSame(instance, instance2); }
public void TestKeyToWithMultipleChainedValues() { ClassWithConstructorParameters test1 = new ClassWithConstructorParameters(1, "abc"); ClassWithConstructorParameters test2 = new ClassWithConstructorParameters(2, "def"); ClassWithConstructorParameters test3 = new ClassWithConstructorParameters(3, "ghi"); binding.Key <ISimpleInterface> ().To(test1).To(test2).To(test3); Assert.That(binding.key == typeof(ISimpleInterface)); object[] values = binding.value as object[]; Assert.IsNotNull(values); Assert.That(values.Length == 3); for (int a = 0; a < values.Length; a++) { ISimpleInterface value = values [a] as ISimpleInterface; Assert.IsNotNull(value); Assert.That(value.intValue == a + 1); } }
public void TestPolymorphicSingleton() { binder.Bind <ISimpleInterface> ().Bind <IAnotherSimpleInterface> ().To <PolymorphicClass> ().ToSingleton(); ISimpleInterface callOnce = binder.GetInstance <ISimpleInterface> (); Assert.NotNull(callOnce); Assert.IsInstanceOf <PolymorphicClass> (callOnce); IAnotherSimpleInterface callAgain = binder.GetInstance <IAnotherSimpleInterface> (); Assert.NotNull(callAgain); Assert.IsInstanceOf <PolymorphicClass> (callAgain); callOnce.intValue = 42; Assert.AreSame(callOnce, callAgain); Assert.AreEqual(42, (callAgain as ISimpleInterface).intValue); }
public void TestInterruptedSequence() { //CommandWithInjection requires an ISimpleInterface injectionBinder.Bind <ISimpleInterface>().To <SimpleInterfaceImplementer> ().ToSingleton(); //Bind the trigger to the command commandBinder.Bind(SomeEnum.ONE).To <CommandWithInjection>().To <FailCommand>().To <CommandWithoutExecute>().InSequence(); TestDelegate testDelegate = delegate { commandBinder.ReactTo(SomeEnum.ONE); }; //That the exception is not thrown demonstrates that the last command was interrupted Assert.DoesNotThrow(testDelegate); //That the value is 100 demonstrates that the first command ran ISimpleInterface instance = injectionBinder.GetInstance <ISimpleInterface>() as ISimpleInterface; Assert.AreEqual(100, instance.intValue); }
public MultipleConstructorsOneThreeFour (ISimpleInterface simple, int intValue, string stringValue) { this.simple = simple; this.intValue = intValue; this.stringValue = stringValue; }
public ComplexObjectWithInterfaceDependencies(ISimpleInterface simpleInterface, IComplexInterface complexInterface) { Id = Guid.NewGuid(); SimpleInterface = simpleInterface; ComplexInterface = complexInterface; }
public void PostConstruct() { Instance1 = pool.GetInstance () as ISimpleInterface; Instance2 = pool.GetInstance () as ISimpleInterface; }