How to remove stations from a line and re-added them on the line?

  • Thread starter Thread starter BarbraFleg
  • Start date Start date
B

BarbraFleg

Guest
I have interface ILine that is inherited by the class Line. Iline has following properties and methods: string Name { get; } ICity City { get; } IEnumerable<IStation> Stations { get; } IEnumerable<ITrain> Trains { get; } IStation Previous(IStation s) IStation Next(IStation s)void AddBefore(IStation toAdd, IStation before = null) void Remove(IStation s). In the current context I am working with in the method void Remove (IStation s) and I am trying to pass following unit test that tests stations can be removed and then re-added.

The Unit Test:

public void stations_can_be_removed()
{
ICity c = CityFactory.CreateCity("Paris");
ILine l = c.AddLine("RER B");
IStation s = c.AddStation("Opera", 0, 0);
Action action = (() => l.Remove(s));
action.ShouldThrow<ArgumentException>();
l.AddBefore(s);
action = (() => l.Remove(s));
action.ShouldNotThrow();
l.Stations.Count().Should().Be(0);
s.Lines.Count().Should().Be(0);
}

public void stations_can_be_removed_then_re_added()
{
ICity c = CityFactory.CreateCity("Paris");
ILine l = c.AddLine("RER B");
IStation s = c.AddStation("Opera", 0, 0);

l.AddBefore(s);
s.Lines.Count().Should().Be(1);
l.Remove(s);
s.Lines.Count().Should().Be(0);

Action action = (() => l.AddBefore(s));
action.ShouldNotThrow();
l.Stations.Count().Should().Be(1);
s.Lines.Count().Should().Be(1);

}

I have tried to do the following code to pass the test mainly in the function void Remove. However, the unit test breaks at the line 7 where I throw exception. I guess my approach is wrong how to test this unit test mainly how I defined my exception by

if (((List<ILine>)s.Lines).Remove(s)) {throw new ArgumentException(); }

public IEnumerable<IStation> Stations
{
get{ return _stations; }
}

public IStation Next(IStation s)
{
if (!_stations.Contains(s))
throw new ArgumentException();
var index = _stations.IndexOf(s);
var isLast = index == _stations.Count -1;
if (isLast) { return null;}
return _stations[index + 1];
}

public IStation Previous(IStation s)
{
if (!_stations.Contains(s))
throw new ArgumentException();
var index = _stations.IndexOf(s);
var isFirst = index == 0;
if (isFirst) { return null;}
return _stations[index - 1];
}

public void AddBefore(IStation toAdd, IStation before = null)
{
if (toAdd == null || _stations.Contains(toAdd))
throw new ArgumentException();
((List<Line>)toAdd.Lines).Add(this);

var index = (before != null) ? _stations.IndexOf(before) : -1;
if (index > -1)
_stations.Insert(index, toAdd);
else
_stations.Add(toAdd);
}

public void Remove(IStation s)
{
_stations.Remove(s);
if (((List<ILine>)s.Lines).Remove(s))
throw new ArgumentException();

}

How can I define the method void remove correctly? Mainly where I am going wrong in my logic?

Continue reading...
 
Back
Top