There are three classes with interfaces Train: ITrain, Line: Iline and City: ICity. Generally, I advanced in the code and resolved TEST 3 & 4 but caused new issues.
I have following issues: 1. Test 1 on line 19 : "Sequence contains no elements" 2. Test 2 on line 38 : "Sequence contains no elements" 3. Test 5 on line 90 : "Expected value to be 2, but found 0"
My guess I am missing an increasing increment of Assignment and correct assigning in between Assignment & Train & Line. However, I do not have a clue how to solve the issue.
internal class Line : ILine
{
private List<Station> _stations;
internal IEnumerable<Train> _trains;
internal Line(string name, City city)
{
this.Name = name;
this.City = city;
_stations = new List<Station>();
_trains = new List<Train>();
}
public string Name
{
get;
}
public ICity City
{
get;
}
public IEnumerable<IStation> Stations
{
get
{
return _stations;
}
}
public IEnumerable<ITrain> Trains
{
get
{
return _trains;
}
}
}
internal class Train : ITrain
{
private List<Line> _lines;
internal Train(string name, Company company)
{
this.Name = name;
this.Company = company;
_lines = new List<Line>();
}
public string Name
{
get;
}
public ICompany Company
{
get;
}
public ILine Assignment
{
get;
private set;
}
public void AssignTo(ILine l)
{
if (!(Assignment == null || l == null || Assignment.City == l.City))
{
throw new ArgumentException();
}
Assignment = l;
foreach (var line in _lines)
{
//verify that LINE is not null
if (Assignment != null && Assignment.Trains.Contains(this))
{
//need to remove this from l1.trains
((List<Train>)Assignment.Trains).Remove(this);
((List<Train>)l.Trains).Add(this);
}
if (Assignment == null && Assignment.Trains.Contains(this))
{
//need to remove this from l1.trains
((List<Train>)l.Trains).Remove(this);
}
}
}
}
//TESTs are not passing on line 19, 38, 46
// Only test T3 and T4 function
[Test]
public void T1_trains_can_be_assigned_to_a_line()
{
ICity s = CityFactory.CreateCity("Paris");
ICompany c = s.AddCompany("SNCF");
ITrain t1 = c.AddTrain("RER1");
ITrain t2 = c.AddTrain("RER2");
t1.Assignment.Should().BeNull();
t2.Assignment.Should().BeNull();
ILine l = s.AddLine("RER A");
t1.AssignTo(l);
t1.Assignment.Should().BeSameAs(l);
t2.Assignment.Should().BeNull();
//Error:Sequence contains no elements
l.Trains.Single().Should().BeSameAs(t1);
}
[Test]
public void T2_when_a_train_is_assigned_to_a_line_he_losts_its_previous_line()
{
ICity s = CityFactory.CreateCity("Paris");
ICompany c = s.AddCompany("SNCF");
ILine l1 = s.AddLine("RER A");
ILine l2 = s.AddLine("RER B");
ITrain t1 = c.AddTrain("RER1");
t1.AssignTo(l1);
t1.Assignment.Should().BeSameAs(l1);
t1.AssignTo(l2);
t1.Assignment.Should().BeSameAs(l2);
l1.Trains.Count().Should().Be(0);
//Error:Sequence contains no elements
l2.Trains.Single().Should().BeSameAs(t1);
}
[Test]
public void T5_line_can_have_mutiple_trains()
{
ICity s1 = CityFactory.CreateCity("Paris");
ICompany c1 = s1.AddCompany("SNCF");
ILine l1 = s1.AddLine("RER A");
ITrain t1 = c1.AddTrain("RER1");
ITrain t2 = c1.AddTrain("RER2");
t1.AssignTo(l1);
t2.AssignTo(l1);
//Error: Expected value to be 2, but found 0
l1.Trains.Count().Should().Be(2);
l1.Trains.Should().Contain(t1);
l1.Trains.Should().Contain(t2);
t1.AssignTo(null);
l1.Trains.Single().Should().BeSameAs(t2);
}
void AssignTo(ILine l);
The third Test is checking that trains and lines belongs to the same city.
I assume the following is meant: whenever a Train is assiged to Line1 and then the assignment changes to Line2, Line2 must be in the same City as Line1.
This could be catched by something like:
As it seems that null values are also allowed, a more complete check would be
Note that this would allow to teleport a train to another city by
The requirements do not specify whether this should be allowed or not.