Exemplo n.º 1
0
 public ResistorColorCode(ResistorColor resistorColor, int?significantFigures, double?multiplier, double?tolerance)
 {
     ResistorColor      = resistorColor;
     SignificantFigures = significantFigures;
     Multiplier         = multiplier;
     Tolerance          = tolerance;
 }
Exemplo n.º 2
0
    public void ColorCode_ColorIsNull_ThrowsArgumentNullException()
    {
        // Arrange
        string testColor = null;

        // Act + Assert
        Assert.Throws <ArgumentNullException>(() => ResistorColor.ColorCode(testColor));
    }
Exemplo n.º 3
0
    public void ColorCode_ForEachColor_ReturnsAppropriateNumber(string testColor, int expectedNumber)
    {
        // Act
        int actualNumber = ResistorColor.ColorCode(testColor);

        // Assert
        Assert.Equal(expectedNumber, actualNumber);
    }
Exemplo n.º 4
0
        /// <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)));
        }
Exemplo n.º 5
0
    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);
    }
Exemplo n.º 6
0
    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);
    }
Exemplo n.º 7
0
    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");
    }
Exemplo n.º 8
0
 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);
 }
Exemplo n.º 9
0
 public void BlackIndex()
 {
     Assert.Equal("black", ResistorColor.ColorCode(0));
 }
Exemplo n.º 10
0
 public void OrangeUpperCase()
 {
     Assert.Equal(3, ResistorColor.ColorCode("ORANGE"));
 }
Exemplo n.º 11
0
 public void Whitespaces()
 {
     Assert.Equal(-1, ResistorColor.ColorCode("  "));
 }
Exemplo n.º 12
0
 public void Null()
 {
     Assert.Equal(-1, ResistorColor.ColorCode(null));
 }
Exemplo n.º 13
0
 public void Empty()
 {
     Assert.Equal(-1, ResistorColor.ColorCode(""));
 }
Exemplo n.º 14
0
 public void Undefined_color()
 {
     Assert.Equal((int)ResistorColorCode.Undefined, ResistorColor.ColorCode("cyan with hint of marine"));
 }
Exemplo n.º 15
0
 public ResistorColorCode FindBy(ResistorColor resistorColor)
 {
     return(_resistorColorCodes.Single(x => x.ResistorColor == resistorColor));
 }
Exemplo n.º 16
0
 public void CodeForColor_GivenColor_ReturnsCodeForColor(BandColors color, int code)
 {
     Assert.AreEqual(code, ResistorColor.CodeForColor(color));
 }
Exemplo n.º 17
0
 public void White()
 {
     Assert.Equal(9, ResistorColor.ColorCode("white"));
 }
Exemplo n.º 18
0
 public void Colors()
 {
     Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors());
 }
Exemplo n.º 19
0
 public void Blue()
 {
     Assert.Equal(6, ResistorColor.ColorCode("blue"));
 }
Exemplo n.º 20
0
 public void Green()
 {
     Assert.Equal(5, ResistorColor.ColorCode("green"));
 }
Exemplo n.º 21
0
 public void Yellow()
 {
     Assert.Equal(4, ResistorColor.ColorCode("yellow"));
 }
Exemplo n.º 22
0
 public void Red()
 {
     Assert.Equal(2, ResistorColor.ColorCode("red"));
 }
Exemplo n.º 23
0
 public void Brown()
 {
     Assert.Equal(1, ResistorColor.ColorCode("brown"));
 }
Exemplo n.º 24
0
 public void Violet()
 {
     Assert.Equal(7, ResistorColor.ColorCode("violet"));
 }
Exemplo n.º 25
0
 public static int ColorCode(string color) => ResistorColor
 .Colors()
 .Select((_color, index) => new { _color, index })
 .First(item => item._color.Equals(color)).index;
Exemplo n.º 26
0
 public void Orange()
 {
     Assert.Equal(3, ResistorColor.ColorCode("orange"));
 }
Exemplo n.º 27
0
 public void Gray()
 {
     Assert.Equal(8, ResistorColor.ColorCode("grey"));
 }
Exemplo n.º 28
0
 public void Black()
 {
     Assert.Equal(0, ResistorColor.ColorCode("black"));
 }
Exemplo n.º 29
0
 public void Colors()
 {
     Console.WriteLine(string.Join(", ", ResistorColor.Colors()));
     Assert.Equal(new[] { "black", "brown", "red", "orange", "yellow", "green", "blue", "violet", "grey", "white" }, ResistorColor.Colors());
 }