// A new composed class wrapping each approver private IApprover WrapApprover(IApprover approver) { ApprovalAccount approvalAccount = new ApprovalAccount(approver.GetType().Name); addAccountAction(approvalAccount); IApprover postApprover = beethovenFactory.Generate <IApprover>( LinkedMethodsReturnValue.Create <IApprover>(nameof(IApprover.Approve)) .PartialMatchMethod(approvalAccount) .PartialMatchMethod(companyAccount) ); Action <double, string, bool> currentResultAction = approveAction; currentResultAction += (amount, approvedBy, accepted) => { if (accepted) { postApprover.NotifyApprove(amount); } }; return(beethovenFactory.Generate <IApprover>( LinkedMethodsReturnValue.Create <IApprover>(nameof(IApprover.Approve)) .AutoMappedMethod(approver) .InvertResult() .SkipIfResultCondition <bool>(value => value) .PartialMatchMethod(new MailApprover(approver, mailService, currentResultAction)) )); }
public IApproverChain CreateChain(IEnumerable <IApprover> approvers) { return(beethovenFactory.Generate <IApproverChain>(approvers .Select(WrapApprover) .Aggregate(LinkedMethodsReturnValue.Create <IApproverChain>(nameof(IApproverChain.Approve)), (value, approver) => value.AutoMappedMethod(approver)))); }
public void LinkedMethodsReturnValueTest2() { PartialMethods partialMethods = new PartialMethods(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters), 1) .PartialMatchMethod(partialMethods, nameof(partialMethods.WithParametersReturnValue))); Assert.AreEqual(5, instance.WithParameters("w", "sd", 3)); }
public void LinkedMethodsTest12() { BeethovenFactory beethovenFactory = new BeethovenFactory(); IGenericMethods instance = beethovenFactory.Generate <IGenericMethods>( LinkedMethodsReturnValue.Create <IGenericMethods>(nameof(IGenericMethods.Simple)) .Func(() => true) .FlowControl(() => false) .Func(() => false)); Assert.AreEqual(true, instance.Simple <bool>()); }
public void LinkedMethodsReturnValueTest3() { PartialMethods partialMethods = new PartialMethods(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.GetMain)) .PartialMatchMethod <ITestMethods>(partialMethods, "testMethods")); object actual = instance.GetMain("w", "sd"); Assert.AreEqual(instance, actual); }
public void LinkedMethodsReturnValueTest4() { CustomImplementation implementation = new CustomImplementation(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.OutAndRef)) .MappedMethod(implementation, nameof(CustomImplementation.OutAndRef)) .Action(Assert.Fail)); string text2 = "wetwt"; instance.OutAndRef(out string _, ref text2, 5); }
public void LinkedMethodsTest11() { BeethovenFactory beethovenFactory = new BeethovenFactory(); WithParametersImplementation implentation = new WithParametersImplementation(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters), 1) .AutoMappedMethod(implentation) .Func(() => 5)); int result = instance.WithParameters("fdgdf", "afasf", 3); Assert.AreEqual(5, result); }
public void LinkedMethodsTest4() { BeethovenFactory beethovenFactory = new BeethovenFactory(); bool skip = true; ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.ReturnValue)) .SkipIf(() => skip) .Func(() => 477)); Assert.AreEqual(0, instance.ReturnValue()); skip = false; Assert.AreEqual(477, instance.ReturnValue()); }
public void LinkedMethodsReturnValueTest3() { Logger logger = new Logger(); CustomImplementation implementation = new CustomImplementation(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods unused = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters)) .MappedMethod(logger, nameof(logger.LogBefore)) .AutoMappedMethod(implementation) .MappedMethod(logger, nameof(logger.LogAfter))); Assert.Fail(); }
public void LinkedMethodsReturnValueTest5() { CustomImplementation implementation = new CustomImplementation(); BeethovenFactory beethovenFactory = new BeethovenFactory(); bool called = false; ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.OutAndRef)) .MappedMethod(implementation, nameof(CustomImplementation.OutAndRef1)) .Action(() => called = true)); string text2 = "wetwt"; instance.OutAndRef(out string _, ref text2, 5); Assert.IsTrue(called); }
public void LinkedMethodsTest3() { BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters)) .SkipIf <string, string>((text1, text2) => string.IsNullOrEmpty(text1)) .SkipIf <string, string>((text1, text2) => string.IsNullOrEmpty(text2)) .Func((string text1, string text2) => text1.Length + text2.Length)); //Assert.AreEqual(0, instance.WithParameters(null, null)); //Assert.AreEqual(0, instance.WithParameters("", "dsfgdsfhsd")); //Assert.AreEqual(0, instance.WithParameters("gjgkffg", "")); Assert.AreEqual(15, instance.WithParameters("fdsfd", "dsfgdsfhsd")); }
public void LinkedMethodsReturnValueTest1() { Logger logger = new Logger(); CustomImplementation implementation = new CustomImplementation(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters), 1) .MappedMethod(logger, nameof(logger.LogBefore)) .MappedMethod(implementation, nameof(CustomImplementation.GetLength)) .MappedMethod(logger, nameof(logger.LogAfter))); Assert.AreEqual(10, instance.WithParameters("w", "sd", 7)); Assert.AreEqual(2, logger.Log.Count); }
public void LinkedMethodsReturnValueTest2() { CustomImplementation implementation = new CustomImplementation(); BeethovenFactory beethovenFactory = new BeethovenFactory(); ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.OutAndRef)) .MappedMethod(implementation, nameof(implementation.OutAndRef1)) .AutoMappedMethod(implementation)); string text1 = "abc"; Assert.AreEqual(20, instance.OutAndRef(out string text2, ref text1, 5)); Assert.AreEqual("cba", text1); Assert.AreEqual("abc abc abc abc abc", text2); }
public void LinkedMethodsReturnValueTest4() { BeethovenFactory beethovenFactory = new BeethovenFactory(); string gotValue1 = ""; string gotValue2 = ""; ITestMethods instance = beethovenFactory.Generate <ITestMethods>( LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.WithParameters), 1) .Action((string text1) => gotValue1 = text1) .Action((string text2) => gotValue2 = text2) ); instance.WithParameters("w", "sd", 3); Assert.AreEqual(gotValue1, "w"); Assert.AreEqual(gotValue2, "sd"); }
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)); }
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); }
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); }
//[TestMethod] public void LinkedMethodsReturnValueTest7() { CustomImplementation implementation = new CustomImplementation(); TypeDefinition <ITestMethods> typeDefinition = TypeDefinition <ITestMethods> .Create(FieldDefinition .CreateFromConstructorParameter <BoolContainer>() .ImportInMain(), LinkedMethodsReturnValue.Create <ITestMethods>(nameof(ITestMethods.OutAndRef)) .MappedMethod(implementation, nameof(CustomImplementation.OutAndRef))); BoolContainer boolContainer = new BoolContainer(); ITestMethods instance = typeDefinition.CreateNew(boolContainer); BoolContainer boolContainer2 = new BoolContainer(); typeDefinition.CreateNew(boolContainer2); string text2 = "wetwt"; instance.OutAndRef(out string _, ref text2, 5); Assert.IsTrue(boolContainer.Value); Assert.IsFalse(boolContainer2.Value); }