Exemple #1
0
        public void TestInheritedInterfaces()
        {
            var factory = new AdaptiveFactory <Base>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type ibase    = factory.Implement(typeof(IBase));
            Type iderived = factory.Implement(typeof(IDerived));

            Assert.AreNotSame(ibase, iderived);
            Assert.IsTrue(typeof(IDisposable).IsAssignableFrom(ibase));
            Assert.IsTrue(typeof(IBase).IsAssignableFrom(ibase));
            Assert.IsTrue(typeof(IBase).IsAssignableFrom(iderived));
            Assert.IsTrue(typeof(IDerived).IsAssignableFrom(iderived));

            using (IBase b = (IBase)Activator.CreateInstance(ibase))
            {
                Assert.IsNotNull(b.DoSomething());
                Assert.AreEqual("DoSomething", b.DoSomething().Name);
            }
            using (IDerived d = (IDerived)Activator.CreateInstance(iderived))
            {
                Assert.AreEqual("DoSomething", d.DoSomething().Name);
                Assert.AreEqual("DoMore", d.DoMore().Name);
            }
        }
        public void ObjectAsBaseTest()
        {
            //TODO: Same for props and events
            //Better naming
            // Comments
            // out and ref params
            // rewrite test, one big with all features and lots of handlers

            // Create factory using plain object as base
            var factory = new AdaptiveFactory <object>();

            factory.ImplementMethods(m => m.Name == "Max").UsingSharedExecuter(GetMaxHandler);
            factory.ImplementMethods().UsingSharedExecuter(InitObjectAsBase, ExecObjectAsBase);
            factory.ImplementProperties(p => p.PropertyType == typeof(int)).UsingGetterAndSetter(InitProp, GetIntProp, SetIntProp);
            factory.ImplementEvents().UsingAdderAndRemover(InitEvent, AddEvent, RemoveEvent);
            //factory.AddMethodHandler<string>().ForAllMethods().WithInitializer(InitObjectAsBase).WithExecutor(ExecObjectAsBase);

            // Create implementation
            Type type = factory.Implement(typeof(ILayer));

            Assert.AreEqual(typeof(object), type.BaseType);
            Assert.AreNotEqual(typeof(object), type);

            // Verify same implementation next time
            Assert.AreSame(type, factory.Implement(typeof(ILayer)));

            // Create instance and call it
            ILayer layer = (ILayer)Activator.CreateInstance(type);

            Assert.AreEqual(3, layer.Add(1, 2));
            Assert.AreEqual(30, layer.Add(10, 20));

            var d = layer.GetOnly;

            layer.GetAndSet = 22;
            layer.Fire     += layer_Fire;
            layer.Fire     -= layer_Fire;
            // TODO: Impl setter, check box/unbox scenarios, generic setter/getter, generic TInfo

            int max = layer.Max(1, 3, 2);

            Assert.AreEqual(3, max);
        }
Exemple #3
0
        public void TestGenericBase()
        {
            var factory = new AdaptiveFactory <GenericBase <int> >();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            IGeneric <int> g = Activator.CreateInstance(factory.Implement <IGeneric <int> >()) as IGeneric <int>;

            Assert.AreEqual("DoGeneric", g.DoGeneric(42).Name);
            Assert.IsTrue(g is GenericBase <int>);
        }
Exemple #4
0
        public void TestGenericInterface()
        {
            var factory = new AdaptiveFactory <Base>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type           t = factory.Implement <IGeneric <int> >();
            IGeneric <int> g = (IGeneric <int>)Activator.CreateInstance(t);

            Assert.AreEqual("DoGeneric", g.DoGeneric(42).Name);
        }
        static TestAccessLayer()
        {
            s_Factory = new AdaptiveFactory <TestAccessLayer>();

            // Method handlers
            s_Factory.ImplementMethods(m => m.Name == "A").UsingSharedExecuter(InitA, ExecA);
            s_Factory.ImplementMethods(m => m.Name == "B").UsingSharedExecuter(InitB, ExecB);
            s_Factory.ImplementMethods(m => m.Name == "C").UsingSharedExecuter("InitC", "ExecC");

            // Property handlers
            s_Factory.ImplementProperties(p => p.Name.StartsWith("P")).UsingGetterAndSetter(InitP, GetP, SetP);
            s_Factory.ImplementProperties(p => p.Name.StartsWith("Q")).UsingGetterAndSetter(InitQ, GetP, SetP);
            s_Factory.ImplementProperties(p => p.Name.StartsWith("R"))
            .UsingBackingField()
            .WithGetInspector(GetInspector)
            .WithGetInspector("GenericGetInspector")
            .WithSetInspector("SetInspector", "SetInspector");

            // Event handlers
            s_Factory.ImplementEvents().UsingAdderAndRemover("InitEvent", "AddEvent", "RemoveEvent");

            // TODO: State without initializer, generic getter/setter, setter does not work, split getter/setter
        }
Exemple #6
0
        public void TestMultipleFactories()
        {
            var factory1 = new AdaptiveFactory <GenericBase <int> >();

            factory1.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            var factory2 = new AdaptiveFactory <GenericBase <int> >();

            factory2.ImplementMethods().UsingSharedExecuter("Init", "Exec");

            Type t1 = factory1.Implement <IGeneric <int> >();
            Type t2 = factory2.Implement <IGeneric <int> >();

            Assert.AreNotSame(t1, t2);
        }
Exemple #7
0
        public void InvalidMethodSignaturesTest()
        {
            var factory = new AdaptiveFactory <LayerBase>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec2");

            try
            {
                factory.Implement <IRefParamInterface>();
                Assert.Fail();
            }
            catch (ArgumentException)
            {
            }
        }
Exemple #8
0
        public void ValidMethodSignaturesTest()
        {
            var factory = new AdaptiveFactory <LayerBase>();

            factory.ImplementMethods().UsingSharedExecuter("Init", "Exec");
            factory.ImplementProperties(p => true).UsingGetterAndSetter("Init", "Get", "Set");

            Type            type  = factory.Implement(typeof(IValidInterface));
            IValidInterface layer = (IValidInterface)Activator.CreateInstance(type);

            layer.VoidMethod();
            layer.SingleArgument(0);
            layer.DefaultArgument();
            Assert.AreEqual(42, layer.ValueTypeReturn());
            Assert.AreEqual("ABC", string.Join("", layer.RefTypeReturn()));

            layer.Number = 42;
            Assert.AreEqual(42, layer.Number);
        }
 /// <summary>
 /// Configures factory
 /// </summary>
 static NLogAccessLayer()
 {
     s_Factory.ImplementMethods().UsingSharedExecuter("LogWriteInit", "LogWriteExec").WithSyntaxChecker(LogWriteCheck);
 }
 static FileAccessLayer()
 {
     // Implement all methods by calling mapped static method on File class.
     s_Factory.ImplementMethods().UsingTarget(null, GetFileMethod);
 }