У меня есть точка от и до на одной линии поезда / метро, и мне нужно найти кратчайший путь между этими точками. У каждой точки есть значения (x, y), и у меня есть модульный тест, который определяет правила поиска пути. У меня есть три теста, из которых только T2 (тест два) проверяется моей функцией, но Тест 1 (T1 path_to_where_i_am) и Тест 3 (T3 get_path_to_station_on_same_line) не проходят, так как у меня есть проблема, чтобы идентифицировать шаги и затем преобразовать их в код.
Функция GetShortestPath
public IReadOnlyList<IStation> GetShortestPath(IStation from, IStation to)
{
//T2 _cant_get_path_from_and_or_to_null()
if (from == null || to == null || from == to)
throw new ArgumentException();
//T1 _path_to_where_i_am()
//s is added and then only one should be same as s
//T3 get_path_to_station_on_same_line()
// from to have to be on same line
throw new NotImplementedException();
//return pathBetweenFromTo;
}
Модульный тест
[Test]
public void T1_path_to_where_i_am()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
c.GetShortestPath(s, s).Single().Should().BeSameAs(s);
}
[Test]
public void T2_cant_get_path_from_and_or_to_null()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s = c.AddStation("Opera", 0, 0);
Action action = (() => c.GetShortestPath(s, null));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, s));
action.ShouldThrow<ArgumentException>();
action = (() => c.GetShortestPath(null, null));
action.ShouldThrow<ArgumentException>();
}
[Test]
public void T3_get_path_to_station_on_same_line()
{
ICity c = CityFactory.CreateCity("Paris");
IStation s1 = c.AddStation("Opera", 0, 0);
IStation s2 = c.AddStation("Chatelet", 10, 10);
IStation s3 = c.AddStation("Gare de Lyon", 20, 20);
ILine l = c.AddLine("RER A");
l.AddBefore(s1);
l.AddBefore(s2);
l.AddBefore(s3);
var path = c.GetShortestPath(s1, s2);
path.Count.Should().Be(2);
(path[0] == s1 && path[1] == s2).Should().BeTrue();
path = c.GetShortestPath(s2, s1);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s1).Should().BeTrue();
path = c.GetShortestPath(s1, s3);
(path.Count == 3).Should().BeTrue();
(path[0] == s1 && path[1] == s2 && path[2] == s3).Should().BeTrue();
path = c.GetShortestPath(s3, s1);
(path.Count == 3).Should().BeTrue();
(path[0] == s3 && path[1] == s2 && path[2] == s1).Should().BeTrue();
path = c.GetShortestPath(s2, s3);
(path.Count == 2).Should().BeTrue();
(path[0] == s2 && path[1] == s3).Should().BeTrue();
}