Exemplo n.º 1
0
        public void SplittingByType()
        {
            var logger  = new ExceptionLogger();
            var counter = new ExecutionCounter();

            var res1 = new[] { "1", "12", "123", "1234", null, "12345" }
            .Try(value => counter.Action(() => 100 / (value.Length - 2)))
            .WhenIs(logger.LogInts)
            .WhenIs(logger.HandleException)
            .ToArray();

            Assert.AreEqual(6, res1.Count());           // we should have 6 return values
            Assert.AreEqual(2, logger.List.Count());    // out list of logs contains two exceptions
            Assert.AreEqual(4, logger.IntList.Count()); // out list of ints contains two integers
            Assert.AreEqual(6, counter.Count);          // we have only 6 executions of the calculation
            Assert.AreEqual(-100, (int)res1[0]);        // check for a successfull result
        }
Exemplo n.º 2
0
        public void PerformingActionsWhileCalculating()
        {
            var logger  = new ExceptionLogger();
            var counter = new ExecutionCounter();

            var res1 = new[] { "1", "12", "123", "1234", null, "12345" }
            .WithAction <string, int>(counter.Action2)      // this is not optimal, since we need to specify types here
            .WithAction <string, int>(counter.Action3)      // this is not optimal, since we need to specify types here
            .Skip(1)
            .Try(Calculate)                                 // here the value is calculated while calling the two actions defined above
            .WhenIs(logger.HandleException)
            .ToArray();

            Assert.AreEqual(5, res1.Count());          // we should have 6 return values
            Assert.AreEqual(5, counter.Count);         // we have only 6 executions of the calculation
            Assert.AreEqual(2, logger.List.Count());   // out list of logs contains two exceptions
            Assert.AreEqual(100, (int)res1[1]);        // check for a successfull result
        }
Exemplo n.º 3
0
        public void PerformingActionsFromAnArrayWhileCalculating()
        {
            var logger  = new ExceptionLogger();
            var counter = new ExecutionCounter();

            var actions = new Func <Func <string, int>, string, int>[] { counter.Action2, counter.Action3 };

            var res1 = new[] { "1", "12", "123", "1234", null, "12345" }
            .WithActions(actions)
            .Skip(1)
            .Try(Calculate)
            .WhenIs(logger.HandleException)
            .ToArray();

            Assert.AreEqual(5, res1.Count());          // we should have 6 return values
            Assert.AreEqual(5, counter.Count);         // we have only 6 executions of the calculation
            Assert.AreEqual(2, logger.List.Count());   // out list of logs contains two exceptions
            Assert.AreEqual(100, (int)res1[1]);        // check for a successfull result
        }