From 5775449ae9f512e66a667a27de55c3a92e2ef548 Mon Sep 17 00:00:00 2001 From: Ryan Jackson Date: Sun, 23 Jul 2017 20:46:29 -0500 Subject: [PATCH 1/2] Implemented good case test, which fixed a few bugs. --- ExercisesForProgrammers/Bill.cs | 32 +++----- ExercisesForProgrammers/Tip.cs | 7 +- TipCalculator.Tests/EndToEndTest.cs | 13 ---- TipCalculator.Tests/EndToEndTests.cs | 73 +++++++++++++++++++ .../TipCalculator.Tests.csproj | 8 +- 5 files changed, 95 insertions(+), 38 deletions(-) delete mode 100644 TipCalculator.Tests/EndToEndTest.cs create mode 100644 TipCalculator.Tests/EndToEndTests.cs diff --git a/ExercisesForProgrammers/Bill.cs b/ExercisesForProgrammers/Bill.cs index 7d44908..b7c1d4a 100644 --- a/ExercisesForProgrammers/Bill.cs +++ b/ExercisesForProgrammers/Bill.cs @@ -12,7 +12,7 @@ public double Subtotal { if (this._tip == null) { this._tip = new Tip(); } - this.Tip.Calculate(value, this.Tip.PercentageOfSubtotal); + this._tip.Calculate(value, this._tip.PercentageOfSubtotal); _subtotal = value; } } @@ -25,33 +25,19 @@ public Tip Tip return _tip; } set - { //TODO: Write a test for this. - this.Subtotal = value.Subtotal; - this.Total = _subtotal + value.Value; + { _tip = value; - } - } - - public double Total { get; private set; } - - - public Bill() - { - this.Tip = new Tip(); + this.Subtotal = value.Subtotal; + } } - public Bill(double subtotal, Tip tip) - { - this._subtotal = subtotal; - - if (subtotal.CompareTo(tip.Subtotal) == 0 || tip.Value.CompareTo(0D) == 0) + public double Total + { + get { - this._tip = tip; - } - else - { - throw new ArgumentException("Tip given not calculated against given subtotal"); + return _subtotal + this._tip.Value; } } + } } diff --git a/ExercisesForProgrammers/Tip.cs b/ExercisesForProgrammers/Tip.cs index 82a5668..e3c1c00 100644 --- a/ExercisesForProgrammers/Tip.cs +++ b/ExercisesForProgrammers/Tip.cs @@ -42,7 +42,12 @@ public Tip(double subtotal, int percentageOfSubtotal) { } public double Calculate(double subtotal, int percentageOfSubtotal) { - var result = subtotal * (percentageOfSubtotal / 100); + this._subtotal = subtotal; + this._percentageOfSubtotal = percentageOfSubtotal; + + var result = ((double)percentageOfSubtotal / 100d); + result = subtotal * result; + this.Value = result; return result; } diff --git a/TipCalculator.Tests/EndToEndTest.cs b/TipCalculator.Tests/EndToEndTest.cs deleted file mode 100644 index 774d867..0000000 --- a/TipCalculator.Tests/EndToEndTest.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using NUnit.Framework; - -namespace TipCalculator.Tests -{ - [TestFixture] - public class EndToEndTest - { - public EndToEndTest() - { - } - } -} diff --git a/TipCalculator.Tests/EndToEndTests.cs b/TipCalculator.Tests/EndToEndTests.cs new file mode 100644 index 0000000..7ad656d --- /dev/null +++ b/TipCalculator.Tests/EndToEndTests.cs @@ -0,0 +1,73 @@ +using System; +using NUnit.Framework; +using ExercisesForProgrammers; //TipCalculator project + +namespace TipCalculator.Tests +{ + [TestFixture] + public class EndToEndTests + { + [Test] + public void BillAndTipSetupWithRealisticValues_ShouldCalculateTipAmountAndTotal() + { + //arrange + + //input + double billAmount = 10.00; //dollars + int tipRate = 15; //percent (of subtotal) + + double expectedTipAmount = 1.50; //dollars + double expectedTotalAmount = 11.50; //dollars + + //act + var tip = new Tip(); + double actualTipAmount = tip.Calculate(billAmount, tipRate); + + var bill = new Bill() { Tip = tip }; + var actualTotalAmount = bill.Total; + + //assert + Assert.AreEqual(expectedTipAmount, actualTipAmount, "Actual tip amount does not equal expected tip amount."); + Assert.AreEqual(expectedTotalAmount, actualTotalAmount, "Actual total amount does not equal expected total amount."); + } + + [Test] + public void GoodTest_OutputFormatting() + { + throw new NotImplementedException(); + } + + [Test] + public void Test_ZeroTipPercentageInput() + { + throw new NotImplementedException(); + } + + /// + /// Defines the current requirement + /// -- might be nice to have a minimum tip in the future. + /// + [Test] + public void Test_ZeroSubtotalInput() + { + throw new NotImplementedException(); + } + + [Test] + public void BadCase_NonNumericInput_ShouldOutputErrorMessage() { + throw new NotImplementedException(); + } + + [Test] + public void BaseCase_NegativeTipPercentage_ShouldOutputErrorMessage() { + throw new NotImplementedException(); + } + + [Test] + public void BaseCase_NegativeSubtotal_ShouldOutputErrorMessage() + { + throw new NotImplementedException(); + } + + } +} diff --git a/TipCalculator.Tests/TipCalculator.Tests.csproj b/TipCalculator.Tests/TipCalculator.Tests.csproj index 379d183..8673959 100644 --- a/TipCalculator.Tests/TipCalculator.Tests.csproj +++ b/TipCalculator.Tests/TipCalculator.Tests.csproj @@ -33,10 +33,16 @@ - + + + + {C1ACE25A-6829-4563-9636-E08DC4DAD01C} + TipCalculator + + \ No newline at end of file From 4bcc4f5e63a510e885e37de004fb71b4a7f1eb4b Mon Sep 17 00:00:00 2001 From: Ryan Jackson Date: Sun, 23 Jul 2017 21:19:26 -0500 Subject: [PATCH 2/2] Added currency formatting (test merely shows that the .NET framework can be used). --- ExercisesForProgrammers/Program.cs | 4 ++-- .../{EndToEndTests.cs => FunctionalTests.cs} | 12 ++++++++++-- TipCalculator.Tests/TipCalculator.Tests.csproj | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) rename TipCalculator.Tests/{EndToEndTests.cs => FunctionalTests.cs} (86%) diff --git a/ExercisesForProgrammers/Program.cs b/ExercisesForProgrammers/Program.cs index 2900c8f..c1607a9 100644 --- a/ExercisesForProgrammers/Program.cs +++ b/ExercisesForProgrammers/Program.cs @@ -16,9 +16,9 @@ public static void Main(string[] args) var newTip = new Tip(newBill.Subtotal, percentageOfSubtotal); //Assignment of subtotal is a bit odd here. newBill.Tip = newTip; - Console.WriteLine($"The tip is {newTip.Value}. "); + Console.WriteLine($"The tip is {newTip.Value.ToString("C")}. "); - Console.WriteLine($"The total is {newBill.Total}. "); + Console.WriteLine($"The total is {newBill.Total.ToString("C")}. "); Console.ReadLine(); // Wait for user to end program. } diff --git a/TipCalculator.Tests/EndToEndTests.cs b/TipCalculator.Tests/FunctionalTests.cs similarity index 86% rename from TipCalculator.Tests/EndToEndTests.cs rename to TipCalculator.Tests/FunctionalTests.cs index 7ad656d..02e6c26 100644 --- a/TipCalculator.Tests/EndToEndTests.cs +++ b/TipCalculator.Tests/FunctionalTests.cs @@ -5,7 +5,7 @@ namespace TipCalculator.Tests { [TestFixture] - public class EndToEndTests + public class FunctionalTests { [Test] public void BillAndTipSetupWithRealisticValues_ShouldCalculateTipAmountAndTotal() @@ -34,7 +34,15 @@ public void BillAndTipSetupWithRealisticValues_ShouldCalculateTipAmountAndTotal( [Test] public void GoodTest_OutputFormatting() { - throw new NotImplementedException(); + //arrange + string expectedTipFormat = "$1.50"; + double tipAmount = 1.50d; + + //act + var actualTipFormatted = tipAmount.ToString("C"); + + //assert + Assert.AreEqual(expectedTipFormat, actualTipFormatted); } [Test] diff --git a/TipCalculator.Tests/TipCalculator.Tests.csproj b/TipCalculator.Tests/TipCalculator.Tests.csproj index 8673959..10519f6 100644 --- a/TipCalculator.Tests/TipCalculator.Tests.csproj +++ b/TipCalculator.Tests/TipCalculator.Tests.csproj @@ -33,7 +33,7 @@ - +