示例#1
0
 public JsonResult Calculate(ResistorViewModel resistorModel)
 {
     resistorModel.ComputedResult = "";
     if (!ModelState.IsValid)
     {
         return(Json(new { Success = false, Result = "", Errors = ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage).ToList() }, JsonRequestBehavior.AllowGet));
     }
     else
     {
         try
         {
             float  tolerance = 0f;
             double ohmValue  = _resCalSvc.CalculateResistorValue(resistorModel.SelectedColorA, resistorModel.SelectedColorB, resistorModel.SelectedColorC, resistorModel.SelectedColorD);
             if (!string.IsNullOrEmpty(resistorModel.SelectedColorD))
             {
                 tolerance = _resCalSvc.GetTolerance(resistorModel.SelectedColorD);
             }
             string result = $"Resistor value: { ohmValue.ToSI() } Ω +/- { tolerance } %";
             return(Json(new { Success = true, Result = result, Errors = "" }, JsonRequestBehavior.AllowGet));
         }
         catch (ArgumentException ex)
         {
             resistorModel.ComputedResult = "";
             return(Json(new { Success = false, Result = "", Errors = new List <string>(1)
                               {
                                   ex.Message
                               } }, JsonRequestBehavior.AllowGet));
         }
         catch (Exception)
         {
             resistorModel.ComputedResult = "";
             //logger.log exception
             return(Json(new { Success = false, Result = "", Errors = new List <string>(1)
                               {
                                   "Unknown error occured."
                               } }, JsonRequestBehavior.AllowGet));
         }
     }
 }
        public void CalculateTolrence_CaseInsensitive_Valid_BandD_Colors(string bandDColor, double expectedValue)
        {
            double actualValue = _calcSvc.GetTolerance(bandDColor);

            Assert.AreEqual(expectedValue, actualValue);
        }