127 lines
3.3 KiB
C#
127 lines
3.3 KiB
C#
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));
|
|
}
|
|
|
|
[Fact]
|
|
public void Quantity_9_gets_no_quantity_discount()
|
|
{
|
|
Assert.Equal(0, PriceCalculator.GetQuantityDiscountPercent(9));
|
|
}
|
|
|
|
[Fact]
|
|
public void Quantity_10_gets_five_percent_quantity_discount()
|
|
{
|
|
Assert.Equal(5, PriceCalculator.GetQuantityDiscountPercent(10));
|
|
}
|
|
|
|
[Fact]
|
|
public void Quantity_49_gets_five_percent_quantity_discount()
|
|
{
|
|
Assert.Equal(5, PriceCalculator.GetQuantityDiscountPercent(49));
|
|
}
|
|
|
|
[Fact]
|
|
public void Quantity_50_gets_ten_percent_quantity_discount()
|
|
{
|
|
Assert.Equal(10, PriceCalculator.GetQuantityDiscountPercent(50));
|
|
}
|
|
|
|
[Fact]
|
|
public void Quantity_below_one_is_refused()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.GetQuantityDiscountPercent(0));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_applies_five_percent_quantity_discount()
|
|
{
|
|
Assert.Equal(950m, PriceCalculator.CalculateTotal(100m, 10, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_applies_percentage_discount_before_quantity_discount()
|
|
{
|
|
Assert.Equal(712.5m, PriceCalculator.CalculateTotal(100m, 10, 25m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_applies_ten_percent_quantity_discount_for_fifty_pieces()
|
|
{
|
|
Assert.Equal(4500m, PriceCalculator.CalculateTotal(100m, 50, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Rounding_rounds_up_to_the_nearest_five_rappen()
|
|
{
|
|
Assert.Equal(1.25m, PriceCalculator.RoundToFiveRappen(1.23m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Rounding_rounds_down_to_the_nearest_five_rappen()
|
|
{
|
|
Assert.Equal(1.20m, PriceCalculator.RoundToFiveRappen(1.21m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Rounding_rounds_midpoints_away_from_zero()
|
|
{
|
|
Assert.Equal(1.15m, PriceCalculator.RoundToFiveRappen(1.125m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_rounds_after_the_quantity_discount()
|
|
{
|
|
Assert.Equal(15.85m, PriceCalculator.CalculateTotal(1.67m, 10, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_refuses_a_negative_unit_price()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(-1m, 1, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_refuses_a_quantity_below_one()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(10m, 0, 0m));
|
|
}
|
|
|
|
[Fact]
|
|
public void Total_refuses_a_discount_above_100_percent()
|
|
{
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => PriceCalculator.CalculateTotal(10m, 1, 101m));
|
|
}
|
|
}
|