Permalink
Browse files

Enhancing the logic for the CostDisplay and PumpOperations and adding…

… tests for PumpOperations.
  • Loading branch information...
1 parent f180a9a commit fb947cb6fa87b75fae53aa623d3baef4acee914a @neraath committed Feb 8, 2012
Showing with 108 additions and 12 deletions.
  1. +87 −0 PumpLogix.Tests/PumpOperationsManualTests.cs
  2. +0 −1 PumpLogix/CostDisplay.cs
  3. +21 −11 PumpLogix/PumpOperations.cs
View
87 PumpLogix.Tests/PumpOperationsManualTests.cs
@@ -0,0 +1,87 @@
+using System;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using PumpLogix.Moles;
+
+namespace PumpLogix.Tests
+{
+ [TestClass]
+ public class PumpOperationsManualTests
+ {
+ private PumpOperations pumpOps;
+
+ [TestInitialize]
+ public void InitializeTests()
+ {
+ this.pumpOps = new PumpOperations();
+ }
+
+ [TestMethod, HostType("Moles")]
+ public void WhenPumpingSucceedsPumpingStops()
+ {
+ // Arrange.
+ bool isPumping = false;
+ MValveController.BehaveAsNotImplemented();
+ MValveController.AllInstances.OpenValveFuelType = (fType, instance) => { };
+ MValveController.AllInstances.CloseValveFuelType = (fType, instance) => { };
+ MValveController.AllInstances.BeginPumping = (instance) => { isPumping = true; };
+ MValveController.AllInstances.StopPumping = (instance) => { isPumping = false; };
+ MValveController.AllInstances.ReadAmountDispensedFuelType = (fType, instance) => 5.0;
+
+ // Act.
+ this.pumpOps.BeginPumping(FuelType.Regular);
+
+ // Assert.
+ Assert.IsFalse(isPumping);
+ }
+
+ [TestMethod, HostType("Moles")]
+ public void WhenPumpingFailsRequestToStopPumpingStillOccurs()
+ {
+ // Arrange.
+ bool requestToStopPumping = false;
+ MValveController.BehaveAsNotImplemented();
+ MValveController.AllInstances.OpenValveFuelType = (fType, instance) => { };
+ MValveController.AllInstances.CloseValveFuelType = (fType, instance) => { };
+ MValveController.AllInstances.BeginPumping = (instance) =>
+ {
+ throw new InvalidOperationException();
+ };
+ MValveController.AllInstances.StopPumping = (instance) => { requestToStopPumping = true; };
+ MValveController.AllInstances.ReadAmountDispensedFuelType = (fType, instance) => 5.0;
+
+ // Act.
+ this.pumpOps.BeginPumping(FuelType.Regular);
+
+ // Assert.
+ Assert.IsTrue(requestToStopPumping);
+ }
+
+ [TestMethod, HostType("Moles")]
+ public void WhenPumpingSucceedsValveIsNeverLeftOpen()
+ {
+ bool isOpen = false;
+ MValveController.BehaveAsNotImplemented();
+ MValveController.AllInstances.OpenValveFuelType = (fType, instance) => { isOpen = true; };
+ MValveController.AllInstances.CloseValveFuelType = (fType, instance) => { isOpen = false; };
+ MValveController.AllInstances.BeginPumping = (instance) => { };
+ MValveController.AllInstances.StopPumping = (instance) => { };
+ MValveController.AllInstances.ReadAmountDispensedFuelType = (fType, instance) => 5.0;
+ this.pumpOps.BeginPumping(FuelType.Regular);
+ Assert.IsFalse(isOpen);
+ }
+
+ [TestMethod, HostType("Moles")]
+ public void WhenPumpingFailsValveIsNeverLeftOpen()
+ {
+ bool isOpen = false;
+ MValveController.BehaveAsNotImplemented();
+ MValveController.AllInstances.OpenValveFuelType = (fType, instance) => { isOpen = true; };
+ MValveController.AllInstances.CloseValveFuelType = (fType, instance) => { isOpen = false; };
+ MValveController.AllInstances.BeginPumping = (instance) => { throw new InvalidOperationException(); };
+ MValveController.AllInstances.StopPumping = (instance) => { };
+ this.pumpOps.BeginPumping(FuelType.Regular);
+ Assert.IsFalse(isOpen);
+ }
+
+ }
+}
View
1 PumpLogix/CostDisplay.cs
@@ -7,7 +7,6 @@ public class CostDisplay : IShowCost
public void AddAmountDispensed(double amount)
{
AmountDispensed += amount;
- throw new Exception("Cannot connect to device output.");
}
public double AmountDispensed { get; private set; }
View
32 PumpLogix/PumpOperations.cs
@@ -1,4 +1,6 @@
-namespace PumpLogix
+using System;
+
+namespace PumpLogix
{
public class PumpOperations
{
@@ -13,17 +15,25 @@ public PumpOperations()
public void BeginPumping(FuelType fuelType)
{
- // Pump.
- this.controller.OpenValve(fuelType);
- this.controller.BeginPumping();
-
- // Monitor.
- double amountDispensed = this.controller.ReadAmountDispensed(fuelType);
- this.costDisplay.AddAmountDispensed(amountDispensed);
+ try
+ {
+ // Pump.
+ this.controller.OpenValve(fuelType);
+ this.controller.BeginPumping();
- // Stop.);
- this.controller.StopPumping();
- this.controller.CloseValve(fuelType);
+ // Monitor.
+ double amountDispensed = this.controller.ReadAmountDispensed(fuelType);
+ this.costDisplay.AddAmountDispensed(amountDispensed);
+ }
+ catch (Exception)
+ {
+ }
+ finally
+ {
+ // Stop.
+ this.controller.StopPumping();
+ this.controller.CloseValve(fuelType);
+ }
}
}
}

0 comments on commit fb947cb

Please sign in to comment.