Files
learning-hour-maker/ideas/slice-with-lambda/exercise/csharp/solution/SliceWithLambdaTests.cs
T
Your Name 77ae05c28c Rename split-loop-with-lambda to slice-with-lambda
The refactoring is now published as Slice on sammancoaching.org:
https://sammancoaching.org/refactorings/slice.html

- git mv ideas/split-loop-with-lambda -> ideas/slice-with-lambda
- rename learning-hour file and update frontmatter name
- update paths in tools/rehearse scripts and report title
- update prose: 'Split Loop with Lambda' -> 'Slice with Lambda'
  in learning hour, exercise READMEs, storyboard, solution comments
- link the published Slice refactoring from the pattern draft
2026-09-29 15:47:30 +00:00

82 lines
2.6 KiB
C#

// 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<InvalidOperationException>(
() => 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());
}
}