Advertisement
Guest User

CalculateQuoteUnitTest.cs

a guest
Aug 17th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.76 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using FastwayCourier;
  4.  
  5. namespace UnitTestAssignment1 {
  6.  
  7.     [TestClass]
  8.     public class CalculatingQuote_Should {
  9.  
  10.        
  11.         /*
  12.          *   Zone Price Checking, "ParcelQuoteFromNelson.CalculateQuote"
  13.          *
  14.         */
  15.  
  16.         [DataTestMethod]
  17.  
  18.         //Boundry Test - PINK
  19.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  20.         [DataRow(0.1, 0)]
  21.         [DataRow(25.0, 0)]
  22.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  23.  
  24.         //Partition Test - PINK
  25.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  26.         [DataRow(8.3, 0)]
  27.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  28.  
  29.         public void ReturnNumberOfTicketsForPinkZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  30.             MatchExpectedNumberOfTicketsForZoneDelivery_WhenInWeightRange(inWeight, "pink", expected);
  31.         }
  32.  
  33.  
  34.  
  35.  
  36.         [DataTestMethod]
  37.  
  38.         //Boundry Test - BLUE
  39.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  40.         [DataRow(0.1, 0)]
  41.         [DataRow(25.0, 0)]
  42.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  43.  
  44.         //Partition Test - BLUE
  45.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  46.         [DataRow(8.3, 0)]
  47.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  48.  
  49.         public void ReturnNumberOfTicketsForBlueZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  50.             MatchExpectedNumberOfTicketsForZoneDelivery_WhenInWeightRange(inWeight, "blue", expected);
  51.         }
  52.  
  53.  
  54.  
  55.  
  56.         [DataTestMethod]
  57.  
  58.         //Boundry Test - LIME (Using up to 15 as inclusive)
  59.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  60.         [DataRow(0.1, 0)]
  61.         [DataRow(15.0, 0)]
  62.  
  63.         [DataRow(15.1, 1)]
  64.         [DataRow(25.0, 1)]
  65.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  66.  
  67.         //Partition Test - LIME
  68.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  69.         [DataRow(16.3, 1)]
  70.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  71.  
  72.         public void ReturnNumberOfTicketsForLimeZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  73.             MatchExpectedNumberOfTicketsForZoneDelivery_WhenInWeightRange(inWeight, "lime", expected);
  74.         }
  75.  
  76.  
  77.  
  78.  
  79.         [DataTestMethod]
  80.  
  81.         //Boundry Test - ORANGE (Using up to 15 as inclusive)
  82.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  83.         [DataRow(0.1, 0)]
  84.         [DataRow(15.0, 0)]
  85.  
  86.         [DataRow(15.1, 1)]
  87.         [DataRow(20.0, 1)]
  88.  
  89.         [DataRow(20.1, 2)]
  90.         [DataRow(25.0, 2)]
  91.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  92.  
  93.         //Partition Test - ORANGE
  94.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  95.         [DataRow(16.5, 1)]
  96.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  97.  
  98.         public void ReturnNumberOfTicketsForOrangeZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  99.             MatchExpectedNumberOfTicketsForZoneDelivery_WhenInWeightRange(inWeight, "orange", expected);
  100.         }
  101.        
  102.         public void MatchExpectedNumberOfTicketsForZoneDelivery_WhenInWeightRange(double inWeight, string region, object expected) {
  103.             //Arrange
  104.             Type expectedException = expected is Type ? (Type) expected : null;
  105.             int expectedTickets = expected is int ? Convert.ToInt32(expected) : -1;
  106.  
  107.             ParcelQuoteFromNelson quote = new ParcelQuoteFromNelson();
  108.             decimal weight = Convert.ToDecimal(inWeight);
  109.  
  110.             Exception exceptionThrown = null;
  111.             ParcelQuoteResult result = null;
  112.  
  113.             //Act
  114.             try {
  115.                 result = quote.CalculateQuote(weight, region);
  116.             } catch (Exception e) {
  117.                 exceptionThrown = e;
  118.             }
  119.  
  120.  
  121.             //Assert
  122.             // Negative Test
  123.             if (exceptionThrown != null) {
  124.                 if (expectedException != null && expectedException != exceptionThrown.GetType())
  125.                     throw exceptionThrown;
  126.  
  127.                 return;
  128.             }
  129.  
  130.             // Positive Test
  131.             if (expectedException != null) {
  132.                 Assert.Fail("Test Expected Exception: " + expectedException);
  133.                 return;
  134.             }
  135.  
  136.             Assert.AreEqual(expectedTickets, result.ExcessTickets);
  137.         }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.         /*
  145.          *   Zone Price Checking, "ParcelQuoteFromNelson.CalculateQuote"
  146.          *
  147.         */
  148.  
  149.  
  150.         [DataTestMethod]
  151.  
  152.         //Boundry Test - PINK (Using up to 25 as inclusive)
  153.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  154.         [DataRow(0.1, 4.15d)]
  155.         [DataRow(25.0, 4.15d)]
  156.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  157.  
  158.         //Partition Test - PINK
  159.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  160.         [DataRow(8.3, 4.15d)]
  161.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  162.        
  163.         public void ReturnPriceForPinkZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  164.             MatchExpectedPriceForZoneDelivery_WhenInWeightRange(inWeight, "pink", expected);
  165.         }
  166.  
  167.  
  168.  
  169.  
  170.         [DataTestMethod]
  171.  
  172.         //Boundry Test - BLUE (Using up to 25 as inclusive)
  173.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  174.         [DataRow(0.1, 6.95d)]
  175.         [DataRow(25.0, 6.95d)]
  176.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  177.  
  178.         //Partition Test - BLUE
  179.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  180.         [DataRow(8.3, 6.95d)]
  181.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  182.        
  183.         public void ReturnPriceForBlueZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  184.             MatchExpectedPriceForZoneDelivery_WhenInWeightRange(inWeight, "blue", expected);
  185.         }
  186.  
  187.  
  188.  
  189.  
  190.         [DataTestMethod]
  191.  
  192.         //Boundry Test - LIME (Using up to 15 as inclusive)
  193.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  194.         [DataRow(0.1, 8.7d)]
  195.         [DataRow(15.0, 8.7d)]
  196.  
  197.         [DataRow(15.1, 14.9d)]
  198.         [DataRow(25.0, 14.9d)]
  199.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  200.  
  201.         //Partition Test - LIME
  202.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  203.         [DataRow(16.3, 14.9d)]
  204.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  205.  
  206.         public void ReturnPriceForLimeZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  207.             MatchExpectedPriceForZoneDelivery_WhenInWeightRange(inWeight, "lime", expected);
  208.         }
  209.  
  210.  
  211.  
  212.  
  213.         [DataTestMethod]
  214.  
  215.         //Boundry Test - ORANGE (Using up to 15 as inclusive)
  216.         [DataRow(0.0, typeof(ArgumentOutOfRangeException))]
  217.         [DataRow(0.1, 12.95d)]
  218.         [DataRow(15.0, 12.95d)]
  219.  
  220.         [DataRow(15.1, 19.15d)]
  221.         [DataRow(20.0, 19.15d)]
  222.  
  223.         [DataRow(20.1, 25.35d)]
  224.         [DataRow(25.0, 25.35d)]
  225.         [DataRow(25.1, typeof(ArgumentOutOfRangeException))]
  226.  
  227.         //Partition Test - ORANGE
  228.         [DataRow(-4.9, typeof(ArgumentOutOfRangeException))]
  229.         [DataRow(16.5, 19.15d)]
  230.         [DataRow(27.9, typeof(ArgumentOutOfRangeException))]
  231.  
  232.         public void ReturnPriceForOrangeZoneDelivery_WhenInWeightRange(double inWeight, object expected) {
  233.             MatchExpectedPriceForZoneDelivery_WhenInWeightRange(inWeight, "orange", expected);
  234.         }
  235.        
  236.         public void MatchExpectedPriceForZoneDelivery_WhenInWeightRange(double inWeight, string region, object expected) {
  237.             //Arrange
  238.             Type expectedException = expected is Type ? (Type) expected : null;
  239.             decimal expectedPrice = expected is double ? Convert.ToDecimal(expected) : -1.0m;
  240.            
  241.             ParcelQuoteFromNelson quote = new ParcelQuoteFromNelson();
  242.             decimal weight = Convert.ToDecimal(inWeight);
  243.  
  244.             Exception exceptionThrown = null;
  245.             ParcelQuoteResult result = null;
  246.            
  247.             //Act
  248.             try {
  249.                 result = quote.CalculateQuote(weight, region);
  250.             } catch (Exception e) {
  251.                 exceptionThrown = e;
  252.             }
  253.  
  254.  
  255.             //Assert
  256.             // Negative Test
  257.             if (exceptionThrown != null) {
  258.                 if (expectedException != null && expectedException != exceptionThrown.GetType())
  259.                     throw exceptionThrown;
  260.  
  261.                 return;
  262.             }
  263.  
  264.             // Positive Test
  265.             if (expectedException != null) {
  266.                 Assert.Fail("Test Expected Exception: " + expectedException);
  267.                 return;
  268.             }
  269.  
  270.             Assert.AreEqual(expectedPrice, result.Price);
  271.         }
  272.        
  273.  
  274.     }
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement