Seed: price calculator with discount tests
Mini project for agentd live testing: three of the five tests fail on main. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
bin/
|
||||||
|
obj/
|
||||||
|
*.user
|
||||||
|
.vs/
|
||||||
4
livetest.slnx
Normal file
4
livetest.slnx
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<Solution>
|
||||||
|
<Project Path="src/Prices/Prices.csproj" />
|
||||||
|
<Project Path="tests/Prices.Tests/Prices.Tests.csproj" />
|
||||||
|
</Solution>
|
||||||
21
src/Prices/PriceCalculator.cs
Normal file
21
src/Prices/PriceCalculator.cs
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
namespace Prices;
|
||||||
|
|
||||||
|
/// <summary>Turns a list price into what the customer actually pays.</summary>
|
||||||
|
public static class PriceCalculator
|
||||||
|
{
|
||||||
|
/// <summary>Applies a percentage discount to a price, e.g. 25 for a quarter off.</summary>
|
||||||
|
public static decimal ApplyDiscount(decimal price, decimal discountPercent)
|
||||||
|
{
|
||||||
|
if (price < 0)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(price), "A price cannot be negative.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (discountPercent is < 0 or > 100)
|
||||||
|
{
|
||||||
|
throw new ArgumentOutOfRangeException(nameof(discountPercent), "A discount is between 0 and 100 percent.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return price - discountPercent;
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/Prices/Prices.csproj
Normal file
9
src/Prices/Prices.csproj
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
36
tests/Prices.Tests/PriceCalculatorTests.cs
Normal file
36
tests/Prices.Tests/PriceCalculatorTests.cs
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Prices.Tests;
|
||||||
|
|
||||||
|
public sealed class PriceCalculatorTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void No_discount_keeps_the_price()
|
||||||
|
{
|
||||||
|
Assert.Equal(100m, PriceCalculator.ApplyDiscount(100m, 0m));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_full_discount_makes_it_free()
|
||||||
|
{
|
||||||
|
Assert.Equal(0m, PriceCalculator.ApplyDiscount(200m, 100m));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_quarter_off_200_is_150()
|
||||||
|
{
|
||||||
|
Assert.Equal(150m, PriceCalculator.ApplyDiscount(200m, 25m));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Half_off_80_is_40()
|
||||||
|
{
|
||||||
|
Assert.Equal(40m, PriceCalculator.ApplyDiscount(80m, 50m));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void A_discount_above_100_percent_is_refused()
|
||||||
|
{
|
||||||
|
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.ApplyDiscount(100m, 101m));
|
||||||
|
}
|
||||||
|
}
|
||||||
20
tests/Prices.Tests/Prices.Tests.csproj
Normal file
20
tests/Prices.Tests/Prices.Tests.csproj
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
|
||||||
|
<PackageReference Include="xunit" Version="2.9.3" />
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\..\src\Prices\Prices.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
Reference in New Issue
Block a user