After a long day of coding, I heard my girl singing “Santa Clause is coming to town” …

Had to get it out of my head…

var saintNick = new SantaClause(); // assert watched out Assert.IsTrue(Actor.Current.WatchedOut); // assert did not cry Assert.IsFalse(Actor.Current.DidCry); // assert did not pout with reason Assert.IsFalse(Actor.Current.DidPout, $"Santa Clause is coming to {Actor.Current.HomeTown}"); // make a list var kids = DataContext.Kids .Select(kid => kid.HomeTown == Actor.Current.HomeTown) .ToList(); // check it twice if(!kids.Any()) throw new InvalidOperationException("List cannot be empty if Santa is to visit."); else if(kids.Any(kid => kid.HomeTown != Actor.Current.HomeTown)) throw new ArgumentOutOfRangeException(); // find out who's naughty or nice var naughty = kids.Where(kid => kid.Behavior == Behavior.Naughty); var nice = kids.Where(kid => kid.Behavior == Behavior.Nice); // have santa come to town var santaVisitor = new SantaVisitor(saintNick); // collects stockings Actor.Current.Town.Accept(santaVisitor); // handle on sleep event kid.EnteredSleep += saintNick.SleepHandler; // handle on wake event kid.EnteredAwake += saintNick.AwakeHandler; // handle on behavior (bad or good) change kid.BehaviorChanged += (s, a) => { // passive assert good behavior if(a.Behavior == Behavior.Good && a.BehaviorReason != BehaviorReason.Goodness) { var kid = s as Kid; var stocking = santaVisitor.Stockings .First(s => s.Name == kid.Name && s.Address == kid.Address; s.Add(new Coal()); } }; 

by dasjestyr via /r/csharp

Leave a Reply