Ejemplo n.º 1
0
        public void AddRoute_ShouldRegisterRoutes()
        {
            // Arrange
            var townA  = new Town("A");
            var townB  = new Town("B");
            var townC  = new Town("C");
            var route1 = new Route(townA, townC, 1);
            var route2 = new Route(townA, townB, 2);

            // Act
            townA.AddRoute(route1);
            townA.AddRoute(route2);

            // Assert
            Assert.True(townA.Routes.SequenceEqual(new[] { route1, route2 }));
        }
Ejemplo n.º 2
0
        public void AddRoute_WhenOriginIsNotSameTown_ShouldThrowInconsistentRouteException()
        {
            // Arrange
            var townA = new Town("A");
            var townB = new Town("B");
            var townC = new Town("C");
            var route = new Route(townB, townC, 1);

            // Act and assert
            Assert.Throws <InconsistentRouteException>(() => townA.AddRoute(route));
        }
Ejemplo n.º 3
0
        public void GetRoute_WhenThereIsNoRouteToTheExpectedTown_ShouldReturnInvalidRouteException()
        {
            // Arrange
            var townA         = new Town("A");
            var townB         = new Town("B");
            var townC         = new Town("C");
            var expectedRoute = new Route(townA, townC, 1);

            townA.AddRoute(expectedRoute);

            // Act and assert
            Assert.Throws <InvalidRouteException>(() => townA.GetRoute(townB));
        }
Ejemplo n.º 4
0
        public void Adding_Route_Between_Two_Towns_Should_Add_Route()
        {
            var townA = new Town {
                Name = "A"
            };
            var townB = new Town {
                Name = "B"
            };

            townA.AddRoute(townB, 10);

            Assert.AreEqual(1, townA.Routes.Count);
        }
        public TripServiceUnitTests()
        {
            var townA = new Town("A");

            Railway.AddTown(townA);
            var townB = new Town("B");

            Railway.AddTown(townB);
            var townC = new Town("C");

            Railway.AddTown(townC);
            var townD = new Town("D");

            Railway.AddTown(townD);
            var townE = new Town("E");

            Railway.AddTown(townE);

            // AB5
            townA.AddRoute(new Route(townA, townB, 5));
            // BC4
            townB.AddRoute(new Route(townB, townC, 4));
            // CD8
            townC.AddRoute(new Route(townC, townD, 8));
            // DC8
            townD.AddRoute(new Route(townD, townC, 8));
            // DE6
            townD.AddRoute(new Route(townD, townE, 6));
            // AD5
            townA.AddRoute(new Route(townA, townD, 5));
            // CE2
            townC.AddRoute(new Route(townC, townE, 2));
            // EB3
            townE.AddRoute(new Route(townE, townB, 3));
            // AE7
            townA.AddRoute(new Route(townA, townE, 7));
        }
Ejemplo n.º 6
0
        public void GetRoute_ShouldReturnExpectedRoute()
        {
            // Arrange
            var townA         = new Town("A");
            var townC         = new Town("C");
            var expectedRoute = new Route(townA, townC, 1);

            townA.AddRoute(expectedRoute);

            // Act
            var route = townA.GetRoute(townC);

            // Assert
            Assert.True(route.Equals(expectedRoute));
        }
Ejemplo n.º 7
0
        public void Retrievieng_Route_That_Does_Not_Exists_Should_Return_Null()
        {
            var townA = new Town {
                Name = "A"
            };
            var townB = new Town {
                Name = "B"
            };

            townA.AddRoute(townB, 10);

            var route = townA.GetRouteTo("C");

            Assert.IsNull(route);
        }
Ejemplo n.º 8
0
        public void Retrievieng_Route_That_Already_Exists_Should_Return_The_Route()
        {
            var townA = new Town {
                Name = "A"
            };
            var townB = new Town {
                Name = "B"
            };

            townA.AddRoute(townB, 10);

            var route = townA.GetRouteTo("B");

            Assert.AreEqual("B", route.Destination.Name);
            Assert.AreEqual(10, route.Distance);
        }
Ejemplo n.º 9
0
        public void CloneIfEquals_WhenTownsAreSame_ShouldClone_PreservingName()
        {
            // Arrange
            var townA      = new Town("A");
            var otherTownA = new Town("A");
            var townB      = new Town("B");
            var route      = new Route(townA, townB, 1);

            townA.AddRoute(route);

            // Act
            var result = townA.CloneIfEquals(otherTownA, out var cloned);

            // Assert
            Assert.True(result);
            Assert.False(ReferenceEquals(townA, cloned));
            Assert.Equal(cloned.Name, townA.Name);
            Assert.True(cloned.Routes.SequenceEqual(new [] { route }));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Clones a given <see cref="Town"/> instance if they are equals.
        /// </summary>
        /// <param name="town">The instance of the <see cref="Town"/>.</param>
        /// <param name="other">The other instance of the <see cref="Town"/> to compare.</param>
        /// <param name="clone">An output var with the cloned <see cref="Town"/>.</param>
        /// <param name="newName">(optional) An <see cref="string"/> representing a new name for the cloned <see cref="Town"/>.</param>
        /// <returns><c>True</c> if the <paramref name="town"/> and <paramref name="other"/> are equals; Otherwise, <c>false</c>.</returns>
        public static bool CloneIfEquals(this ITown town, ITown other, out ITown clone, string newName = "")
        {
            clone = town;

            if (!town.Equals(other))
            {
                return(false);
            }

            var routes = town.Routes;

            newName = string.IsNullOrWhiteSpace(newName) ? town.Name : newName;
            var cloneTown = new Town(newName);

            foreach (var route in routes)
            {
                var cloneRoute = new Route(cloneTown, route.To, route.Distance);
                cloneTown.AddRoute(cloneRoute);
            }

            clone = cloneTown;
            return(true);
        }