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:
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\solution\SliceWithLambda\SliceWithLambda.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user