public ResistorColorCode(ResistorColor resistorColor, int?significantFigures, double?multiplier, double?tolerance) { ResistorColor = resistorColor; SignificantFigures = significantFigures; Multiplier = multiplier; Tolerance = tolerance; }
public void ColorCode_ColorIsNull_ThrowsArgumentNullException() { // Arrange string testColor = null; // Act + Assert Assert.Throws <ArgumentNullException>(() => ResistorColor.ColorCode(testColor)); }
public void ColorCode_ForEachColor_ReturnsAppropriateNumber(string testColor, int expectedNumber) { // Act int actualNumber = ResistorColor.ColorCode(testColor); // Assert Assert.Equal(expectedNumber, actualNumber); }
/// <inheritdoc /> public Int64 CalculateOhmValue(string bandAColor, string bandBColor, string bandCColor, string bandDColor) { //Validate input parameters Assertion.Requires(bandAColor.HasValue()); Assertion.Requires(bandBColor.HasValue()); Assertion.Requires(bandCColor.HasValue()); Assertion.Requires(bandDColor.HasValue()); List <ResistorColor> lstRingColor = null; ResistorColor objRingColor = null; Int16 multiplier = 0; Int64 ohmValue = 0; lstRingColor = GetRingColorModel(); // bandAColor objRingColor = lstRingColor.FirstOrDefault(r => r.ColorCode == bandAColor); if (objRingColor != null) { ohmValue = objRingColor.SignificantFigure.HasValue ? Convert.ToInt16(objRingColor.SignificantFigure.Value) * 10 : 1; // color codes None, Pink, Silver, Gold should not impact the calculation as its optional } else { throw new Exception("Invalid Band A color"); // all thess validations can be done at the begining of logics to avoid resource utilization } // bandBColor objRingColor = lstRingColor.FirstOrDefault(r => r.ColorCode == bandBColor); if (objRingColor != null) { ohmValue = objRingColor.SignificantFigure.HasValue ? Convert.ToInt16(objRingColor.SignificantFigure.Value) + ohmValue : 1; // color codes None, Pink, Silver, Gold should not impact the calculation as its optional } else { throw new Exception("Invalid Band B color"); } // bandCColor objRingColor = lstRingColor.FirstOrDefault(r => r.ColorCode == bandCColor); if (objRingColor != null) { multiplier = Convert.ToInt16(objRingColor.SignificantFigure ?? 0); } else { throw new Exception("Invalid Band C color"); } // bandDColor objRingColor = lstRingColor.FirstOrDefault(r => r.ColorCode == bandDColor); if (objRingColor == null) { throw new Exception("Invalid Band D color"); } return(Convert.ToInt64(ohmValue * Math.Pow(10, multiplier))); }
public void ColorCode_UnknownColor_ThrowsArgumentException() { // Arrange string testColor = "unknown"; // Act + Assert ArgumentException ex = Assert.Throws <ArgumentException>(() => ResistorColor.ColorCode(testColor)); Assert.Equal($"Unknown color: {testColor}", ex.Message); }
public void Colors_ReturnsAllColors() { // Arrange var expectedColors = new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }; // Act var actualColors = ResistorColor.Colors(); // Assert Assert.Equal(expectedColors, actualColors); }
public void CantModifyColors() { var colors = ResistorColor.Colors(); colors[0] = "whatever"; Assert.Equal( new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors() ); // Assert.ThrowsAny<Exception>(() => colors[0] = "whatever"); }
public IEnumerable <ResistorColor> GetColors() { if (_colors == null) { _colors = Select <ResistorColor>("SELECT * FROM ResistorColor", new KeyValuePair <string, object>[] { } , (rdr) => { var t = new ResistorColor { Name = rdr.GetString(0), Code = GetValue <string>(rdr.GetValue(1)), SignificantFigures = GetValue <int?>(rdr.GetValue(2)), Multiplier = GetValue <double?>(rdr.GetValue(3)), Tolerance = GetValue <decimal?>(rdr.GetValue(4)) }; return(t); }).ToArray(); } return(_colors); }
public void BlackIndex() { Assert.Equal("black", ResistorColor.ColorCode(0)); }
public void OrangeUpperCase() { Assert.Equal(3, ResistorColor.ColorCode("ORANGE")); }
public void Whitespaces() { Assert.Equal(-1, ResistorColor.ColorCode(" ")); }
public void Null() { Assert.Equal(-1, ResistorColor.ColorCode(null)); }
public void Empty() { Assert.Equal(-1, ResistorColor.ColorCode("")); }
public void Undefined_color() { Assert.Equal((int)ResistorColorCode.Undefined, ResistorColor.ColorCode("cyan with hint of marine")); }
public ResistorColorCode FindBy(ResistorColor resistorColor) { return(_resistorColorCodes.Single(x => x.ResistorColor == resistorColor)); }
public void CodeForColor_GivenColor_ReturnsCodeForColor(BandColors color, int code) { Assert.AreEqual(code, ResistorColor.CodeForColor(color)); }
public void White() { Assert.Equal(9, ResistorColor.ColorCode("white")); }
public void Colors() { Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors()); }
public void Blue() { Assert.Equal(6, ResistorColor.ColorCode("blue")); }
public void Green() { Assert.Equal(5, ResistorColor.ColorCode("green")); }
public void Yellow() { Assert.Equal(4, ResistorColor.ColorCode("yellow")); }
public void Red() { Assert.Equal(2, ResistorColor.ColorCode("red")); }
public void Brown() { Assert.Equal(1, ResistorColor.ColorCode("brown")); }
public void Violet() { Assert.Equal(7, ResistorColor.ColorCode("violet")); }
public static int ColorCode(string color) => ResistorColor .Colors() .Select((_color, index) => new { _color, index }) .First(item => item._color.Equals(color)).index;
public void Orange() { Assert.Equal(3, ResistorColor.ColorCode("orange")); }
public void Gray() { Assert.Equal(8, ResistorColor.ColorCode("grey")); }
public void Black() { Assert.Equal(0, ResistorColor.ColorCode("black")); }
public void Colors() { Console.WriteLine(string.Join(", ", ResistorColor.Colors())); Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors()); }