Пример #1
0
 public static Result<decimal, string> CostToEnter(Person p)
 {
     return from a in Club.CheckAge(p)
            from b in Club.CheckClothes(a)
            from c in Club.CheckSobriety(b)
            select c.Gender == Gender.Female ? 0m : 5m;
 }
Пример #2
0
 public static Result<Person, string> CheckClothes(Person p)
 {
     if (p.Gender == Gender.Male && !p.Clothes.Contains("Tie"))
         return Result<Person, string>.FailWith("Smarten up!");
     if (p.Gender == Gender.Female && p.Clothes.Contains("Trainers"))
         return Result<Person, string>.FailWith("Wear high heels!");
     return Result<Person, string>.Succeed(p);
 }
Пример #3
0
 public static Result<Person, string> CheckAge(Person p)
 {
     if (p.Age < 18)
         return Result<Person, string>.FailWith("Too young!");
     if (p.Age > 40)
         return Result<Person, string>.FailWith("Too old!");
     return Result<Person, string>.Succeed(p);
 }
Пример #4
0
        public void Part1()
        {
            var Dave = new Person(Gender.Male, 41, new List<string> { "Tie", "Jeans" }, Sobriety.Sober);
            var costDave = ClubbedToDeath.CostToEnter(Dave);
            Assert.AreEqual("Too old!", costDave.FailedWith().First());

            var Ken = new Person(Gender.Male, 28, new List<string> { "Tie", "Shirt" }, Sobriety.Tipsy);
            var costKen = ClubbedToDeath.CostToEnter(Ken);
            Assert.AreEqual(5m, costKen.SucceededWith());

            var Ruby = new Person(Gender.Female, 25, new List<string> { "High heels" }, Sobriety.Tipsy);
            var costRuby = ClubbedToDeath.CostToEnter(Ruby);
            costRuby.Match(
                (x, msgs) =>
                {
                    Assert.AreEqual(0m, x);
                },
                msgs =>
                {
                    Assert.Fail();

                });

            var Ruby17 = new Person(Ruby.Gender, 17, Ruby.Clothes, Ruby.Sobriety);
            var costRuby17 = ClubbedToDeath.CostToEnter(Ruby17);
            Assert.AreEqual("Too young!", costRuby17.FailedWith().First());

            var KenUnconscious = new Person(Ken.Gender, Ken.Age, Ken.Clothes, Sobriety.Unconscious);
            var costKenUnconscious = ClubbedToDeath.CostToEnter(KenUnconscious);
            costKenUnconscious.Match(
                (x, msgs) =>
                {
                    Assert.Fail();
                },
                msgs =>
                {
                    Assert.AreEqual("Sober up!", msgs.First());
                });
        }
Пример #5
0
 public static Result<Person, string> CheckSobriety(Person p)
 {
     if (new[] { Sobriety.Drunk, Sobriety.Paralytic, Sobriety.Unconscious }.Contains(p.Sobriety))
         return Result<Person, string>.FailWith("Sober up!");
     return Result<Person, string>.Succeed(p);
 }
Пример #6
0
 public void Part3()
 {
     var person = new Person(
         gender: Gender.Male,
         age: 59,
         clothes: new List<string> { "Jeans" },
         sobriety: Sobriety.Paralytic);
     var cost = GayBar.CostToEnter(person);
     cost.Match(
         ifSuccess: (x, msgs) => Assert.Fail(),
         ifFailure: errs => Assert.That(errs, Is.EquivalentTo(new[] { "Too old!", "Smarten up!", "Sober up!" })));
 }
Пример #7
0
 public static Result<decimal, string> CostToEnter(Person p)
 {
     return new List<Func<Person, Result<Person, string>>> { CheckGender, Club.CheckAge, Club.CheckClothes, Club.CheckSobriety }
         .Select(check => check(p))
         .Collect()
         .Select(x => x[0].Age + 1.5m);
 }
Пример #8
0
 public static Result<Person, string> CheckGender (Person p)
 {
     if (p.Gender == Gender.Male)
         return Result<Person, string>.Succeed(p);
     return Result<Person, string>.FailWith("Men only");
 }
Пример #9
0
        public void Part2()
        {
            var daveParalytic = new Person(
                age: 41,
                clothes: new List<string> { "Tie", "Shirt" }, 
                gender: Gender.Male,
                sobriety: Sobriety.Paralytic);
            
            var costDaveParalytic = ClubTropicana.CostToEnter(daveParalytic);

            costDaveParalytic.Match(
                ifSuccess: (x, msgs) => Assert.Fail(),
                ifFailure: errs => Assert.That(errs.ToList(), Is.EquivalentTo(new[] { "Too old!", "Sober up!" })));
        }
Пример #10
0
 public static decimal CostByGender(Person p, Person x, Person y)
 {
     return p.Gender == Gender.Female ? 0m : 7.5m;
 }
Пример #11
0
 public static Result<decimal, string> CostToEnter(Person p)
 {
     return from c in Club.CheckAge(p)
            join x in Club.CheckClothes(p) on 1 equals 1
            join y in Club.CheckSobriety(p) on 1 equals 1
            select c.Gender == Gender.Female ? 0m : 7.5m;
 }