コード例 #1
0
        public void TestMethodPropertyLinkedObjects3()
        {
            BeethovenFactory factory = new BeethovenFactory();
            int             setCount = 0;
            ITestProperties test     = factory.Generate <ITestProperties>(
                new LinkedObjects(
                    new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                    .SkipIfEqual(),
                    new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                    .DelegatedSetter(value => setCount++)));

            test.Property1 = 5;
            test.Property1 = 6;
            Assert.AreEqual(2, setCount);
        }
コード例 #2
0
        public void TestMethodDefaultPropertyNotifyChanged1()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestProperties  test    = factory.Generate <ITestProperties>(
                new DefaultProperty()
                .NotifyChanged());
            List <string> changes = new List <string>();

            test.PropertyChanged += (sender, args) => changes.Add(args.PropertyName);
            test.Property1        = 5;
            test.Property2        = "5";
            CollectionAssert.AreEquivalent(
                new[] { "Property1", "Property2" },
                changes);
        }
コード例 #3
0
        public void LinkedMethodsReturnValueTest6()
        {
            CustomImplentation implementation   = new CustomImplentation();
            BeethovenFactory   beethovenFactory = new BeethovenFactory();
            bool         called   = false;
            ITestMethods instance = beethovenFactory.Generate <ITestMethods>(
                new LinkedMethodsReturnValue(nameof(ITestMethods.OutAndRef))
                .MappedMethod(implementation, nameof(CustomImplentation.OutAndRef))
                .FlowControl(() => false)
                .Action(() => called = true));
            string text2 = "wetwt";

            instance.OutAndRef(out string _, ref text2, 5);
            Assert.IsFalse(called);
        }
コード例 #4
0
        public void TestMethodDefaultPropertyFallback1()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestProperties  test    = factory.Generate <ITestProperties>(
                new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                .MappedGetter(() => 6),
                new DefaultProperty()
                .SetterGetter());

            Assert.AreEqual(6, test.Property1);
            test.Property2 = "Some value";
            Assert.AreEqual("Some value", test.Property2);
            test.Property1 = 55;
            Assert.AreEqual(6, test.Property1);
        }
コード例 #5
0
        public void EventWithReturnValue1()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestEvents      test    = factory.Generate <ITestEvents>();
            IEventTrigger    trigger = factory.CreateEventTrigger(test, nameof(ITestEvents.WithReturnValue));
            bool             withReturnValueEventCalled = false;
            bool             otherEventCalled           = false;

            test.Simple          += delegate { otherEventCalled = true; };
            test.WithParameters  += delegate { otherEventCalled = true; };
            test.WithReturnValue += delegate { return(withReturnValueEventCalled = true); };
            trigger.Notify("");
            Assert.IsTrue(withReturnValueEventCalled);
            Assert.IsFalse(otherEventCalled);
        }
コード例 #6
0
        public void LinkedMethodsTest9()
        {
            List <string>        log              = new List <string>();
            SimpleImplementation implementation   = new SimpleImplementation();
            BeethovenFactory     beethovenFactory = new BeethovenFactory();
            const string         name             = nameof(ITestMethods.Simple);
            ITestMethods         instance         = beethovenFactory.Generate <ITestMethods>(
                new LinkedObjects(
                    ActionMethod.Create(name, () => log.Add("Before")),
                    new MappedMethod(name, implementation),
                    ActionMethod.Create(name, () => log.Add("After"))));

            instance.Simple();
            CollectionAssert.AreEquivalent(new[] { "Before", "After" }, log);
        }
コード例 #7
0
        public IApproverChain CreateChain(params IApprover[] approvers)
        {
            LinkedMethodsReturnValue linkedMethods = approvers
                                                     .Aggregate(new LinkedMethodsReturnValue(nameof(IApproverChain.Approve)),
                                                                (value, approver) => value
                                                                .AutoMappedMethod(approver)
                                                                .InvertResult());

            /*
             * In the linked methods, the method return true to continue execution, and also to interrupt.
             * It is illogical that 'Approve' should return false for non-approved.
             * InvertResult simply inverts the result of a bool-method.
             */
            return(beethovenFactory.Generate <IApproverChain>(linkedMethods));
        }
コード例 #8
0
        public void LinkedMethodsReturnValueTest4()
        {
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            string           gotValue1        = "";
            string           gotValue2        = "";
            ITestMethods     instance         = beethovenFactory.Generate <ITestMethods>(
                new LinkedMethodsReturnValue(nameof(ITestMethods.WithParameters))
                .Action((string text1) => gotValue1 = text1)
                .Action((string text2) => gotValue2 = text2)
                );

            instance.WithParameters("w", "sd", 3);
            Assert.AreEqual(gotValue1, "w");
            Assert.AreEqual(gotValue2, "sd");
        }
コード例 #9
0
        public void TestMethodLazyCreatorProperty2()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestProperties  test    = factory.Generate <ITestProperties>(
                new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                .LazyCreator(() =>
            {
                Assert.Fail();
                return(0);
            })
                .SetterGetter());

            test.Property1 = 7;
            Assert.AreEqual(7, test.Property1);
        }
コード例 #10
0
        public void EventWithParameters1()
        {
            BeethovenFactory        factory = new BeethovenFactory();
            ITestEvents             test    = factory.Generate <ITestEvents>();
            Action <double, string> trigger =
                new EventTrigger(test, nameof(ITestEvents.WithParameters)).ToAction <double, string>();
            bool withParametersEventCalled = false;
            bool otherEventCalled          = false;

            test.Simple          += delegate { otherEventCalled = true; };
            test.WithParameters  += delegate { withParametersEventCalled = true; };
            test.WithReturnValue += delegate { return(otherEventCalled = true); };
            trigger(4.4, "");
            Assert.IsTrue(withParametersEventCalled);
            Assert.IsFalse(otherEventCalled);
        }
コード例 #11
0
        public void LinkedMethodsTest13()
        {
            List <string> log            = new List <string>();
            List <int>    implementation = new List <int> {
                5, 2, 17
            };
            BeethovenFactory  beethovenFactory = new BeethovenFactory();
            IEnumerable <int> instance         = beethovenFactory.Generate <IEnumerable <int> >(
                new LinkedObjects(
                    ActionMethod.Create("GetEnumerator", () => log.Add("Before")),
                    implementation,
                    ActionMethod.Create("GetEnumerator", () => log.Add("After"))));

            CollectionAssert.AreEqual(new[] { 5, 2, 17 }, instance.ToArray());
            CollectionAssert.AreEquivalent(new[] { "Before", "After" }, log);
        }
コード例 #12
0
        public void LinkedMethodsTest6()
        {
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            int          calledCount          = 0;
            ValueCheck   valueCheck           = new ValueCheck();
            ITestMethods instance             = beethovenFactory.Generate <ITestMethods>(
                new LinkedMethods(nameof(ITestMethods.NoReturnValue))
                .SkipIf(valueCheck, nameof(valueCheck.HasNoValue1))
                .Action(delegate { calledCount++; }));

            instance.NoReturnValue("", "afasf");
            instance.NoReturnValue(null, "afasf");
            Assert.AreEqual(0, calledCount);
            instance.NoReturnValue("fdgdf", "afasf");
            Assert.AreEqual(1, calledCount);
        }
コード例 #13
0
        public void EventSimpleError()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestEvents      test    = factory.Generate <ITestEvents>(new DefaultEvent());
            Action <int>     trigger =
                new EventTrigger(test, nameof(ITestEvents.Simple)).ToAction <int>();
            bool simpleEventCalled = false;
            bool otherEventCalled  = false;

            test.Simple          += delegate { simpleEventCalled = true; };
            test.WithParameters  += delegate { otherEventCalled = true; };
            test.WithReturnValue += delegate { return(otherEventCalled = true); };
            trigger(123);
            Assert.IsTrue(simpleEventCalled);
            Assert.IsFalse(otherEventCalled);
        }
コード例 #14
0
        public void LinkedMethodsReturnValueTest1()
        {
            PartialMethods   partialMethods   = new PartialMethods();
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            ITestMethods     instance         = beethovenFactory.Generate <ITestMethods>(
                new LinkedMethodsReturnValue(nameof(ITestMethods.WithParameters))
                .PartialMatchMethod(partialMethods, nameof(partialMethods.WithParameters1))
                .PartialMatchMethod(partialMethods, nameof(partialMethods.WithParameters2))
                .PartialMatchMethod(partialMethods, nameof(partialMethods.WithParametersCount))
                .PartialMatchMethod(partialMethods, nameof(partialMethods.WithParameters)));

            Assert.AreEqual(9, instance.WithParameters("w", "sd", 3));
            Assert.AreEqual(0, instance.WithParameters(null, "sd", 3));
            Assert.AreEqual(0, instance.WithParameters("w", "sd", -68));
            Assert.AreEqual(0, instance.WithParameters("w", "", 7));
        }
コード例 #15
0
        public void TestMethodDefaultPropertyDelegated4()
        {
            DefaultImplementation2 implementation = new DefaultImplementation2();
            BeethovenFactory       factory        = new BeethovenFactory();
            ITestProperties        test           = factory.Generate <ITestProperties>(
                new DefaultProperty()
                .DelegatedSetter(implementation, nameof(implementation.DelegatedSetter))
                .DelegatedGetter(implementation, nameof(implementation.DelegatedGetter)));

            Assert.AreEqual(0, test.Property1);
            test.Property2 = "Nothing";
            test.Property2 = "Some value";
            Assert.AreEqual("Some value", test.Property2);
            test.Property1 = 55;
            Assert.AreEqual(55, test.Property1);
        }
コード例 #16
0
        public void LinkedMethodsTest7()
        {
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            int          calledCount          = 0;
            ValueCheck   valueCheck           = new ValueCheck();
            ITestMethods instance             = beethovenFactory.Generate <ITestMethods>(
                LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters))
                .SkipIf(valueCheck, nameof(valueCheck.HasNoValue2))
                .Func((string text1, string text2) => calledCount++));

            instance.WithParameters("", "");
            instance.WithParameters("fegf", null);
            Assert.AreEqual(0, calledCount);
            instance.WithParameters("fdgdf", "afasf");
            Assert.AreEqual(1, calledCount);
        }
コード例 #17
0
        public void EventWithReturnValue2()
        {
            BeethovenFactory factory  = new BeethovenFactory();
            ITestEvents      test     = factory.Generate <ITestEvents>();
            IEventTrigger    trigger  = factory.CreateEventTrigger(test, nameof(ITestEvents.WithReturnValue));
            string           gotValue = null;

            test.WithReturnValue += value =>
            {
                gotValue = value;
                return(true);
            };
            bool returnValue = (bool)trigger.Notify("123");

            Assert.AreEqual(true, returnValue);
            Assert.AreEqual("123", gotValue);
        }
コード例 #18
0
        public void TestMethodDefaultMethod1()
        {
            List <string> methodsCalled = new List <string>();

            object LogCall(MethodInfo methodInfo, object[] _)
            {
                methodsCalled.Add(methodInfo.Name);
                return(null);
            }

            BeethovenFactory factory = new BeethovenFactory();
            ITestMethods     test    = factory.Generate <ITestMethods>(
                new DefaultMethod(LogCall));

            test.NoReturnValue("asd", "gggggdsss");
            CollectionAssert.AreEquivalent(new[] { "NoReturnValue" }, methodsCalled);
        }
コード例 #19
0
        public void LinkedMethodsTest5()
        {
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            bool             skip             = true;
            int          calledCount          = 0;
            ITestMethods instance             = beethovenFactory.Generate <ITestMethods>(
                new LinkedMethods(nameof(ITestMethods.Simple))
                .SkipIf(() => skip)
                .Action(() => calledCount++));

            instance.Simple();
            instance.Simple();
            Assert.AreEqual(0, calledCount);
            skip = false;
            instance.Simple();
            Assert.AreEqual(1, calledCount);
        }
コード例 #20
0
        public void TestMethodPropertyDelegatedGetter2()
        {
            int        getCount = 0;
            Func <int> action   = () =>
            {
                getCount++;
                return(55);
            };
            BeethovenFactory factory = new BeethovenFactory();
            ITestProperties  test    = factory.Generate <ITestProperties>(
                new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                .DelegatedGetter(action));

            Assert.AreEqual(0, getCount);
            Assert.AreEqual(55, test.Property1);
            Assert.AreEqual(1, getCount);
        }
コード例 #21
0
        public void EventWithReturnValue2()
        {
            BeethovenFactory    factory = new BeethovenFactory();
            ITestEvents         test    = factory.Generate <ITestEvents>();
            Func <string, bool> trigger =
                new EventTrigger(test, nameof(ITestEvents.WithReturnValue)).ToFunc <string, bool>();
            string gotValue = null;

            test.WithReturnValue += value =>
            {
                gotValue = value;
                return(true);
            };
            bool returnValue = trigger("123");

            Assert.AreEqual(true, returnValue);
            Assert.AreEqual("123", gotValue);
        }
コード例 #22
0
        public void TestMethodDefaultPropertyFallback2()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestProperties  test    = factory.Generate <ITestProperties>(
                new PropertyDefinition <int>(nameof(ITestProperties.Property1))
                .SetterGetter(),
                new DefaultProperty()
                .NotifyChanged()
                .SetterGetter());
            List <string> changes = new List <string>();

            test.PropertyChanged += (sender, args) => changes.Add(args.PropertyName);
            test.Property1        = 5;
            test.Property2        = "5";
            CollectionAssert.AreEquivalent(
                new[] { "Property2" },
                changes);
        }
コード例 #23
0
        public void ImplementsTest1()
        {
            BeethovenFactory factory  = new BeethovenFactory();
            ITestImplements  instance = factory.Generate <ITestImplements>(
                new PropertyDefinition <string>("Property2")
                .ValidityCheck(name => !string.IsNullOrEmpty(name))
                .SkipIfEqual()
                .SetterGetter()
                .NotifyChanged(),
                new SimpleEventDefinition <PropertyChangedEventHandler>(nameof(INotifyPropertyChanged.PropertyChanged)));
            int count = 0;

            instance.PropertyChanged += (_, __) => count++;
            instance.Property2        = "123";
            instance.Property2        = "123";
            instance.Property2        = "1e23";
            Assert.AreEqual(2, count);
        }
コード例 #24
0
        public void TestMethodDefaultMethod5()
        {
            List <string> methodsCalled = new List <string>();

            object LogCall(MethodInfo methodInfo, object[] _)
            {
                methodsCalled.Add(methodInfo.Name);
                return(null);
            }

            BeethovenFactory factory = new BeethovenFactory();
            ITestMethods     test    = factory.Generate <ITestMethods>(
                ActionMethod.Create(nameof(ITestMethods.Simple), () => { }),
                new DefaultMethod(LogCall));

            test.Simple();
            CollectionAssert.AreEquivalent(new string[0], methodsCalled);
        }
コード例 #25
0
        public IValueHolder Create(string name, int value, byte[] data)
        {
            IValueHolder valueHolder = beethovenFactory.Generate <IValueHolder>(
                new DefaultProperty()
                .InitialValue(new byte[0])
                .SetterGetter(),
                LinkedMappedMethods.Create(ComparerCreator));

            if (name != null)
            {
                valueHolder.Name = name;
            }
            valueHolder.Value = value;
            if (data != null)
            {
                valueHolder.Data = data;
            }
            return(valueHolder);
        }
コード例 #26
0
ファイル: Factory.cs プロジェクト: marwahaha/Beethoven
        public IValueHolder Create(string name, int value, byte[] data)
        {
            IValueHolder valueHolder = beethovenFactory.Generate <IValueHolder>(
                new DefaultProperty()
                .InitialValue(new byte[0])
                .SetterGetter(),
                new EqualsGetHash <IValueHolder>(ValuesGetterFunc));

            if (name != null)
            {
                valueHolder.Name = name;
            }
            valueHolder.Value = value;
            if (data != null)
            {
                valueHolder.Data = data;
            }
            return(valueHolder);
        }
コード例 #27
0
        public void LinkedMethodsTest8()
        {
            BeethovenFactory beethovenFactory = new BeethovenFactory();
            int calledCount = 0;
            WithParametersImplementation implentation = new WithParametersImplementation();
            ITestMethods instance = beethovenFactory.Generate <ITestMethods>(
                LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters), 1)
                .Func((int count) => count)
                .SkipIfResultCondition((int count) => count == 0)
                .AutoMappedMethod(implentation)
                .Action(() => calledCount++));
            int result1 = instance.WithParameters("fegf", "ggn", 0);

            Assert.AreEqual(0, calledCount);
            Assert.AreEqual(0, result1);
            int result2 = instance.WithParameters("fdgdf", "afasf", 3);

            Assert.AreEqual(1, calledCount);
            Assert.AreEqual(30, result2);
        }
コード例 #28
0
        public void AutoAssignTest2()
        {
            var defaultValues = new
            {
                Name    = "The evil company",
                Address = "2460 Sunshine road"
            };
            BeethovenFactory factory = null;
            IValueLookup     lookup  = new CompositeValueLookup(
                new AnonymousValueLookup(defaultValues),
                new InterfaceFactoryValueLookup((type, name) => factory.Generate(type)));

            factory = new BeethovenFactory(
                new DefaultProperty()
                .ValueLookup(lookup)
                .SetterGetter());
            ICompany company = factory.Generate <ICompany>();

            Assert.AreEqual("The evil company", company.Information.Name);
            Assert.AreEqual("2460 Sunshine road", company.Information.Address);
        }
コード例 #29
0
        public void AutoAssignTest4()
        {
            Dictionary <string, object> defaultValues = new Dictionary <string, object>
            {
                { "Name", "The evil company" },
                { "Address", 2460 }
            };
            BeethovenFactory factory = null;
            IValueLookup     lookup  = new CompositeValueLookup(
                new DictionaryValueLookup(defaultValues),
                new InterfaceFactoryValueLookup((type, name) => factory.Generate(type)));

            factory = new BeethovenFactory(
                new DefaultProperty()
                .ValueLookup(lookup)
                .SetterGetter());
            ICompany company = factory.Generate <ICompany>();

            Assert.AreEqual("The evil company", company.Information.Name);
            Assert.AreEqual(null, company.Information.Address);
        }
コード例 #30
0
        public void TestMethodDefaultMethod4()
        {
            BeethovenFactory factory = new BeethovenFactory();
            ITestMethods     test    = factory.Generate <ITestMethods>(
                new DefaultMethod((methodInfo, parameters) =>
            {
                Type intRefType = typeof(int).MakeByRefType();
                ParameterInfo[] parameterInfos = methodInfo.GetParameters();
                foreach (ParameterInfo parameterInfo in parameterInfos.Where(info => info.ParameterType == intRefType))
                {
                    parameters[parameterInfo.Position] = 5;
                }
            }));
            string b = "";

            test.OutAndRef(out string a, ref b, 5);
            int value = 0;

            test.Ref(ref value);
            Assert.AreEqual(5, value);
        }