public void AllElementsCantDecide_AllCalled() { // Arrange int someVal = 10; ChainDecision expected = ChainDecision.CantSay; var element1 = MockRepository.GenerateStub <IChainElement <int> >(); var element2 = MockRepository.GenerateStub <IChainElement <int> >(); var element3 = MockRepository.GenerateStub <IChainElement <int> >(); element1.Stub(c => c.Process(someVal)).Return(ChainDecision.CantSay); element2.Stub(c => c.Process(someVal)).Return(ChainDecision.CantSay); element3.Stub(c => c.Process(someVal)).Return(ChainDecision.CantSay); var provider = MockRepository.GenerateStub <IChainElementsProvider <int> >(); provider.Stub(c => c.GetElements()).Return(new[] { element1, element2, element3 }); IChainOfResponsibility <int> chain = new ChainOfResponsibility <int>(provider); // Act ChainDecision result = chain.Process(someVal); // Assert element1.AssertWasCalled(s => s.Process(someVal)); element2.AssertWasCalled(s => s.Process(someVal)); element3.AssertWasCalled(s => s.Process(someVal)); Assert.AreEqual(expected, result); }
public void SecondElementCanDecide_FirstAndSecondCalled() { // Arrange int someVal = 10; ChainDecision expected = ChainDecision.Passed; IChainOfResponsibility <int> chain = new ChainOfResponsibility <int>(); var element1 = MockRepository.GenerateStub <IChainElement <int> >(); var element2 = MockRepository.GenerateStub <IChainElement <int> >(); var element3 = MockRepository.GenerateStub <IChainElement <int> >(); element1.Stub(c => c.Process(someVal)).Return(ChainDecision.CantSay); element2.Stub(c => c.Process(someVal)).Return(expected); chain.Add(element1); chain.Add(element2); chain.Add(element3); // Act ChainDecision result = chain.Process(someVal); // Assert element1.AssertWasCalled(s => s.Process(someVal)); element2.AssertWasCalled(s => s.Process(someVal)); element3.AssertWasNotCalled(s => s.Process(Arg <int> .Is.Anything)); Assert.AreEqual(expected, result); }
protected override Expression VisitMethodCall(MethodCallExpression m) { if (m.Method.DeclaringType == typeof(Queryable)) { ChainOfResponsibility <MethodCallExpression> .Start(m1 => m1.Method.Name == "Where", HandleWhere) .Then(m1 => m1.Method.Name == "Take", HandleTake) .Then(m1 => m1.Method.Name == "Skip", HandleSkip) .Then(m1 => m1.Method.Name == "OrderBy", HandleOrderBy) .Then(m1 => m1.Method.Name == "OrderByDescending", HandleOrderByDescending) .Else(m1 => { throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name)); }) .Run(m); } else { ChainOfResponsibility <MethodCallExpression> .Start(m1 => m1.Method.Name == "Substring", HandleSubstring) .Else(m1 => { throw new NotSupportedException(string.Format("The method '{0}' is not supported", m.Method.Name)); }) .Run(m); } return(m); }
protected Logger() { _disablers = new Stack <object>(); _handlers = ChainOfResponsibility.Empty <LogEntry>() .Chain(new LogEntryLink <TaskLog>(Insert, Update)) .Chain(new LogEntryLink <StepLog>(Insert, Update)) .Chain(new LogEntryLink <MessageLog>(Insert)); }
public void Handled_HandlerReturnsValue() { var chain = ChainOfResponsibility .Empty <int, string>() .Chain(new IntToStringLink(1)) .Chain(new IntToStringLink(2)) .Chain(new IntToStringLink(3)); Assert.That(chain.Handle(2), Is.EqualTo("2")); }
public void NotHandled_DefaultReturned() { var chain = ChainOfResponsibility .Empty <int, string>() .Chain(new IntToStringLink(1)) .Chain(new IntToStringLink(2)) .Chain(new IntToStringLink(3)); Assert.That(chain.Handle(5), Is.Null); }
public Installation GetInstallation() { var result = new ChainOfResponsibility <Installation>( new ConfigRTester(InstallationArguments.Instance.SourceFolder), new ConfigRTester(AppDomain.CurrentDomain.BaseDirectory) ).TryGetMatch(); if (result.Item1) { return(result.Item2); } throw new InvalidOperationException("Unable to run installation. No matching configuration was found see <tobefilledout> guide"); }
public void TryNotHandled_False_AndDefaultReturned() { var chain = ChainOfResponsibility .Empty <int, string>() .Chain(new IntToStringLink(1)) .Chain(new IntToStringLink(2)) .Chain(new IntToStringLink(3)); string result; Assert.That(chain.TryHandle(5, out result), Is.False); Assert.That(result, Is.Null); }
protected Logger() { this._disablers = new Stack <object>(); ChainOfResponsibilityLink <LogEntry> chainOfResponsibilityLink = ChainOfResponsibility.Empty <LogEntry>(); Logger logger = this; Logger logger1 = this; ChainOfResponsibilityLink <LogEntry> chainOfResponsibilityLink1 = chainOfResponsibilityLink.Chain(new Logger.LogEntryLink <TaskLog>(new Func <TaskLog, string>(logger.Insert), new Action <TaskLog>(logger1.Update))); Logger logger2 = this; Logger logger3 = this; Logger logger4 = this; this._handlers = chainOfResponsibilityLink1.Chain(new Logger.LogEntryLink <StepLog>(new Func <StepLog, string>(logger2.Insert), new Action <StepLog>(logger3.Update))).Chain(new Logger.LogEntryLink <MessageLog>(new Func <MessageLog, string>(logger4.Insert), null)); }
public void TryNotHandled_ContextNotModified() { var chain = ChainOfResponsibility .Empty <Context>() .Chain(new ToUpperIfStartsWith("1")) .Chain(new ToUpperIfStartsWith("2")) .Chain(new ToUpperIfStartsWith("3")); var context = new Context("5_a"); Assert.That(chain.TryHandle(context), Is.False); Assert.That(context.S, Is.EqualTo("5_a")); }
public void TryHandle_True_AndHandlerReturnsValue() { var chain = ChainOfResponsibility .Empty <int, string>() .Chain(new IntToStringLink(1)) .Chain(new IntToStringLink(2)) .Chain(new IntToStringLink(3)); string result; Assert.That(chain.TryHandle(2, out result), Is.True); Assert.That(result, Is.EqualTo("2")); }
public void Handled_ContextModifed() { var chain = ChainOfResponsibility .Empty <Context>() .Chain(new ToUpperIfStartsWith("1")) .Chain(new ToUpperIfStartsWith("2")) .Chain(new ToUpperIfStartsWith("3")); var context = new Context("2_a"); chain.Handle(context); Assert.That(context.S, Is.EqualTo("2_A")); }
public void IChainOfResponsibility_Works_JustFine() { var chain = ChainOfResponsibility.Empty <int, string>() .Chain(new ResponsibleLink <int, string>(new MultiLink(2))) .Chain(new MultiLink(1)); var anotherchain = ChainOfResponsibility .Empty <Exception>() .Chain(new ResponsibleLink <Exception>(new MultiLink())) .Chain(new MultiLink()); Assert.That(chain.Handle(1), Is.EqualTo("1")); var ex = new Exception(); anotherchain.Handle(ex); Assert.That(ex.Data, Is.Not.Empty); }
private ChainOfResponsibilityLink <int, string> initChainOfSubstitutes( out ChainOfResponsibilityLink <int, string> first, out ChainOfResponsibilityLink <int, string> second, out ChainOfResponsibilityLink <int, string> third ) { first = Substitute.For <ChainOfResponsibilityLink <int, string> >(); second = Substitute.For <ChainOfResponsibilityLink <int, string> >(); third = Substitute.For <ChainOfResponsibilityLink <int, string> >(); var chain = ChainOfResponsibility .Empty <int, string>() .Chain(first) .Chain(second) .Chain(third); return(chain); }
public void FluentChain_LinkingTwo_InnerChaining() { var l1 = new IToUpperIfStartsWith("1"); var l2 = new IToUpperIfStartsWith("2"); ChainOfResponsibilityLink <Context> chain = ChainOfResponsibility .Empty <Context>() .Chain(l1) .Chain(l2); //Assert.That(chain, Is.SameAs(l2)); //Assert.That(l1.Next, Is.SameAs(l2)); //Assert.That(l2.Next, Is.Null); var ctx = new Context("2a"); chain.Handle(ctx); Assert.That(ctx.S, Is.EqualTo("2A")); }
public MonitorWorkItem(MonitorConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException("configuration"); } DateTimeOffset utcNow = Time.UtcNow; if (configuration.LastRun > utcNow) { utcNow = configuration.LastRun; } this._checkRange = new Range <DateTimeOffset>(configuration.LastRun, utcNow); this._entries = new List <Tuple <Target, MonitorEntry> >(); this._ignore = new List <ISpecification <MonitorEntry> >(); this._redirects = ChainOfResponsibility.Empty <MonitorEntry, Target[]>(); this._messageGrouping = new List <Regex>(); this.Configuration = configuration; }
static void Main() { ILogger logger = new ConsoleLogger(); var funcBasedLoggingHandler = new FuncbasedHandler( request => false, request => { logger.WriteLine($"Logging handler-> {(string.IsNullOrEmpty(request?.Data) ? "EMPTY MSG" : request.Data)}"); }); var cor = new ChainOfResponsibility() .AddHandler(new OnlySmallLettersHandler(logger)) .AddHandler(new StartWithBigLetterRequestHandler(logger)) .AddHandler(funcBasedLoggingHandler) .AddHandler(new UnhandledRequestHandler(logger)); cor.Handle(new Request("all_small_letters_request")); cor.Handle(new Request("FirstIsBigRequest")); cor.Handle(new Request(null)); Console.ReadLine(); }
public MonitorWorkItem(MonitorConfiguration configuration) { if (configuration == null) { throw new ArgumentNullException(nameof(configuration)); } DateTimeOffset upperBound = Time.UtcNow; if (configuration.LastRun > upperBound) { upperBound = configuration.LastRun; } CheckRange = new Range <DateTimeOffset>(configuration.LastRun, upperBound); _entries = new List <Tuple <Target, MonitorEntry> >(); _ignore = new List <ISpecification <MonitorEntry> >(); _redirects = ChainOfResponsibility.Empty <MonitorEntry, Target[]>(); _messageGrouping = new List <Regex>(); Configuration = configuration; }
public void Setup() { _cor = new ChainOfResponsibility(); }
public Hand AddCard(ChainOfResponsibility.Cards.Card card) { this.Cards.Add(card); return this; }
public ChainOfResponsibility <T, R> SetNext(ChainOfResponsibility <T, R> next) { Next = next; return(Next); }
static void Main(string[] args) { ChainOfResponsibility.Run(); }
public void Initialization() { _actionWithLaserAim = new ActionWithLaserAim(_playerTransform, _mainControllers); _actionWithMuffler = new ActionWithMuffler(); _chainOfResponsibility = new ChainOfResponsibility(); }
public SpecificationVisitor(ChainOfResponsibility <CreateInfo <T> > creationCOR) => this.creationCOR = creationCOR;