Exemplo n.º 1
0
        void TestParticipantsNotStartingTimer(params DateTime[] birthDateTimes)
        {
            var mockTimer = new Mock <IDispatcherTimer>(MockBehavior.Strict);

            mockTimer.SetupProperty(m => m.Interval);
            mockTimer.Setup(m => m.Start());
            mockTimer.Setup(m => m.Stop());
            var moqArgs      = new  MoqTimerEventArgs(true);
            var participants = GetParticipants(moqArgs, birthDateTimes);
            var ageService   = new AgeUpdatingService(participants, mockTimer.Object);

            mockTimer.Verify(m => m.Start(), Times.Never);
        }
Exemplo n.º 2
0
        List <ParticipantListItemViewModel> GetParticipants(MoqTimerEventArgs moqArgs, DateTime[] birthDateTimes)
        {
            var returnVar = new List <ParticipantListItemViewModel>(birthDateTimes.Length);

            for (int i = 0; i < birthDateTimes.Length; i++)
            {
                var p = new ParticipantListItemViewModel(
                    new ParticipantBaseModel
                {
                    Id            = i + 1,
                    DateTimeBirth = birthDateTimes[i]
                });
                p.PropertyChanged += (s, e) =>
                {
                    var returnedP = (ParticipantListItemViewModel)s;
                    Console.WriteLine("Id:{0}, DOB:{1:dd/MM hh:mm}, daysOld:{2}, called:{3:dd/MM hh:mm}", p.Id, p.DateTimeBirth, p.AgeDays, moqArgs.StartAt);
                };
                returnVar.Add(p);
            }
            return(returnVar);
        }
Exemplo n.º 3
0
        public void TestAgeUpdatingStartOnAdd()
        {
            var moqArgs   = new MoqTimerEventArgs(true);
            var mockTimer = new Mock <IDispatcherTimer>(MockBehavior.Strict);

            mockTimer.SetupProperty(m => m.Interval);
            mockTimer.Setup(m => m.Start()).Verifiable();
            mockTimer.Setup(m => m.Stop()).Verifiable();
            IDispatcherTimer timer = mockTimer.Object;

            var ageService = new AgeUpdatingService(new ParticipantListItemViewModel[0], timer);

            mockTimer.Verify(m => m.Start(), Times.Never);

            TimeSpan toDob = TimeSpan.FromMinutes(-10);
            var      p     = new ParticipantListItemViewModel(
                new ParticipantBaseModel
            {
                Id            = 1,
                DateTimeBirth = moqArgs.StartAt + toDob
            });

            TimeSpan expectedInterval = TimeSpan.FromDays(1) + toDob;
            TimeSpan tolerance        = TimeSpan.FromSeconds(defaultSecsTolerance);

            mockTimer.SetupSet(m => m.Interval = It.IsAny <TimeSpan>()).Callback <TimeSpan>(i =>
            {
                string usrMsg = string.Format(intervalLogTemplate, i, expectedInterval, tolerance);
                if (i < expectedInterval - tolerance || i > expectedInterval + tolerance)
                {
                    throw new ArgumentOutOfRangeException("Interval", usrMsg);
                }
                else
                {
                    Console.WriteLine(usrMsg);
                }
            });
            ageService.AddParticipant(p);
            mockTimer.Verify(m => m.Start(), Times.Once);

            TimeSpan laterDayversary = TimeSpan.FromMinutes(-5);

            p = new ParticipantListItemViewModel(
                new ParticipantBaseModel
            {
                Id            = 2,
                DateTimeBirth = moqArgs.StartAt + laterDayversary
            });
            ageService.AddParticipant(p);

            toDob = TimeSpan.FromMinutes(-30);
            p     = new ParticipantListItemViewModel(
                new ParticipantBaseModel
            {
                Id            = 2,
                DateTimeBirth = moqArgs.StartAt + toDob
            });
            expectedInterval = TimeSpan.FromDays(1) + toDob;

            ageService.AddParticipant(p);

            toDob = TimeSpan.FromDays(-1).Add(TimeSpan.FromMinutes(5));
            p     = new ParticipantListItemViewModel(
                new ParticipantBaseModel
            {
                Id            = 2,
                DateTimeBirth = moqArgs.StartAt + toDob
            });
            expectedInterval = TimeSpan.FromDays(1) + toDob;

            ageService.AddParticipant(p);
        }
Exemplo n.º 4
0
        MyMoq TestAgeUpdatingSetInterval(params DateTime[] birthDateTimes)
        {
            if (birthDateTimes == null || birthDateTimes.Length < 2)
            {
                throw new ArgumentException("must include at least 2 birthDateTimes");
            }

            var moqArgs      = new MoqTimerEventArgs(true);
            var participants = GetParticipants(moqArgs, birthDateTimes);

            var expectedTimesFromNow = TimesFromNow(birthDateTimes, moqArgs.StartAt);

            var mockTimer = new Mock <IDispatcherTimer>(MockBehavior.Strict);

            mockTimer.SetupProperty(m => m.Interval);
            mockTimer.Setup(m => m.Start());
            mockTimer.Setup(m => m.Stop());
            TimeSpan expectedInterval = expectedTimesFromNow[0];
            TimeSpan tolerance        = TimeSpan.FromSeconds(defaultSecsTolerance);
            bool     requiresInterval = true;

            mockTimer.SetupSet(m => m.Interval = It.IsAny <TimeSpan>()).Callback <TimeSpan>(i =>
            {
                string usrMsg = string.Format(intervalLogTemplate, i, expectedInterval, tolerance);
                if (requiresInterval && (i < expectedInterval - tolerance || i > expectedInterval + tolerance))
                {
                    throw new ArgumentOutOfRangeException("Interval", usrMsg);
                }
                else
                {
                    Console.WriteLine(usrMsg);
                }
            });
            IDispatcherTimer timer = mockTimer.Object;

            Console.WriteLine("instantiating AgeUpdatingService");
            var ageService = new AgeUpdatingService(participants, timer);

            Console.WriteLine("finished instantiation");
            mockTimer.Verify(m => m.Start(), Times.AtLeastOnce);

            Action <TimeSpan, TimeSpan> validateIntervals = new Action <TimeSpan, TimeSpan>((last, curr) => {
                if (curr.Ticks > 0)
                {
                    moqArgs.StartAt             += last;
                    expectedInterval             = curr;
                    mockTimer.Raise(m => m.Tick += null, moqArgs);
                }
            });

            moqArgs.StartAt += expectedTimesFromNow.First(); //will have been set during instantiation
            expectedTimesFromNow.AggregatePairSelect((prev, cur) => cur - prev)
            .Where(t => t.Ticks != 0)
            .AggregatePairForEach(new TimeSpan(), validateIntervals);
            requiresInterval             = false;
            mockTimer.Raise(m => m.Tick += null, moqArgs);

            Console.WriteLine("Adding new participants");
            moqArgs.StartAt = DateTime.Now;
            foreach (var addedInterval in (new int[] { 10, -10 }).Select(i => TimeSpan.FromSeconds(i)))
            {
                participants.Add(new ParticipantListItemViewModel(
                                     new ParticipantBaseModel
                {
                    Id            = participants.Count + 1,
                    DateTimeBirth = moqArgs.StartAt - TimeSpan.FromDays(1) + addedInterval
                }));
                ageService.AddParticipant(participants[participants.Count - 1]);
            }
            requiresInterval = true;
            moqArgs.StartAt += TimeSpan.FromSeconds(10);

            expectedTimesFromNow = TimesFromNow(from p in participants where p.AgeDays <= 28 select p.DateTimeBirth, moqArgs.StartAt);

            expectedTimesFromNow.AggregatePairSelect(new TimeSpan(), (prev, cur) => cur - prev)
            .Where(t => t.Ticks != 0)
            .AggregatePairForEach(new TimeSpan(), validateIntervals);

            requiresInterval             = false;
            mockTimer.Raise(m => m.Tick += null, moqArgs);

            return(new MyMoq {
                AgeService = ageService, Moq = mockTimer
            });
        }