// Tests for the three Slice-with-Lambda solutions — step 4: the lambda as a stub. // // Condensed mechanics: https://sammancoaching.org/refactorings/slice.html // (where that page says mock, we use a lambda instead). using System; using Xunit; public class SliceWithLambdaTests { // --- Example 1: metaphor-free (Calculator) --- [Fact] public void Guards_against_negative_input() { var calc = new Calculator(new SomeDependency()); // the real, flaky one... Assert.Equal(0, calc.Calculate(-1, a => 42)); // ...never asked: stub not invoked } [Fact] public void Throws_when_precondition_not_satisfied() { var calc = new Calculator(new SomeDependency()); Assert.Throws( () => calc.Calculate(7, a => throw new InvalidOperationException())); } [Fact] public void Combines_input_and_the_stubbed_value() { var calc = new Calculator(new SomeDependency()); Assert.Equal(84, calc.Calculate(0, a => 42)); // stub through the seam } // --- Example 2: Weather Station (SprinklerController) --- [Fact] public void Skips_watering_when_already_watered() { var controller = new SprinklerController(() => 200); // pure lambda: no sensor controller.ScheduleWatering(); // water once... Assert.Equal(WateringDuration.None, controller.ScheduleWatering()); } [Fact] public void Waters_longer_in_full_sun() { var controller = new SprinklerController(() => 200); // sunny: > 128 Assert.Equal(WateringDuration.Medium, controller.ScheduleWatering()); } [Fact] public void Waters_less_in_the_shade() { var controller = new SprinklerController(() => 10); // shady: ≤ 128 Assert.Equal(WateringDuration.Short, controller.ScheduleWatering()); } // --- Example 3: Ice Cream Shop (IceCreamShop) --- [Fact] public void Skips_thawing_when_already_thawed() { var shop = new IceCreamShop(() => 30); // pure lambda: no freezer shop.ThawStock(); // thaw once... Assert.Equal(ThawAmount.None, shop.ThawStock()); } [Fact] public void Thaws_more_when_warm() { var shop = new IceCreamShop(() => 30); // warm: > 25 Assert.Equal(ThawAmount.Medium, shop.ThawStock()); } [Fact] public void Thaws_less_when_cold() { var shop = new IceCreamShop(() => 5); // cold: ≤ 25 Assert.Equal(ThawAmount.Low, shop.ThawStock()); } }