Exemple #1
0
        public void TestParseValidSide()
        {
            var coloredSide  = ColoredSide.Parse("Red-7");
            var expectedSide = new ColoredSide(Color.Red, 7);

            Assert.AreEqual(coloredSide, expectedSide);
        }
Exemple #2
0
        public void TestEqualsAndHashCode()
        {
            var side1 = new ColoredSide(Color.Red, 7);
            var side2 = new ColoredSide(Color.Red, 7);

            Assert.IsTrue(side1.Equals(side2));
            Assert.AreEqual(side1.GetHashCode(), side2.GetHashCode());
            side2 = new ColoredSide(Color.Black, 7);
            Assert.IsFalse(side1.Equals(side2));
        }
Exemple #3
0
        /// <summary>
        /// This method is constructing a triangle from three sides
        /// </summary>
        /// <param name="a">First side</param>
        /// <param name="b">Second side</param>
        /// <param name="c">Third side</param>
        /// <returns>New instance of ColoredTriangle</returns>
        /// <exception cref="DomainException">Is thrown when invalid length of sides are specified</exception>
        public static ColoredTriangle Create(ColoredSide a, ColoredSide b, ColoredSide c)
        {
            if (a.Length + b.Length <= c.Length || a.Length + c.Length <= b.Length || b.Length + c.Length <= a.Length)
            {
                throw new DomainException($"Invalid side length for triangle ({a.Length}, {b.Length}, {c.Length})");
            }

            return(new ColoredTriangle
            {
                FirstSide = a,
                SecondSide = b,
                ThirdSide = c
            });
        }
Exemple #4
0
        /// <summary>
        /// This method is creating a triangle from a string specified in a specific format
        /// </summary>
        /// <param name="triangle">Actual string</param>
        /// <returns>New instance of ColoredTriangle</returns>
        /// <exception cref="DomainException">
        /// Is thrown when triangle string has an invalid format or the length of sides are invalid
        /// </exception>
        public static ColoredTriangle Parse(string triangle)
        {
            var fields = triangle.Split(FieldDelimiter);

            if (fields.Length < 3)
            {
                throw new DomainException($"Not enough sides for triangle ({fields.Length})");
            }
            return(Create(
                       ColoredSide.Parse(fields[0]),
                       ColoredSide.Parse(fields[1]),
                       ColoredSide.Parse(fields[2])
                       ));
        }
        public void TestEqualsAndHashCode()
        {
            var side1    = new ColoredSide(Color.Red, 7);
            var side2    = new ColoredSide(Color.Blue, 6);
            var side3    = new ColoredSide(Color.Yellow, 5);
            var triangle = new ColoredTriangle
            {
                FirstSide  = side1,
                SecondSide = side2,
                ThirdSide  = side3
            };

            var triangle2 = new ColoredTriangle
            {
                FirstSide  = side1,
                SecondSide = side2,
                ThirdSide  = side3
            };


            Assert.IsTrue(triangle.Equals(triangle2));
            Assert.AreEqual(triangle.GetHashCode(), triangle2.GetHashCode());
        }
Exemple #6
0
 public void TestParseInvalidSide()
 {
     Assert.Throws <DomainException>(() => ColoredSide.Parse("Rdd-43"));
 }