Slice with lambda: add C# exercise projects and export

- Add SliceWithLambda.slnx + csproj scaffolding for ideas/slice-with-lambda/
  exercise/csharp (examples project + xUnit solution/test project)
- Add exercises/slice-with-lambda (C# version of the prefactored-longrange
  exercise with Buyers and SliceWithLambda projects)
- Add export/slice-with-lambda (nested git repo with exercise export)
- Ignore bin/ and obj/ build output at repo root
This commit is contained in:
Your Name
2026-09-30 00:01:12 +00:00
parent 77ae05c28c
commit ac45255a80
35 changed files with 999 additions and 0 deletions
@@ -0,0 +1,82 @@
// 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());
}
}