List not being passed as method parameter

1

I need to implement is the RoutesBetween method to trace the route between one point and another (in this case, point A and E ).

Test Method:

public void TestRoutesBetweenTwoPoints()
{
    var links = new ILink<string>[]
    {
        new Link<string>("a","b"),
        new Link<string>("b","c"),
        new Link<string>("c","b"),
        new Link<string>("b","a"),
        new Link<string>("c","d"),
        new Link<string>("d","e"),
        new Link<string>("d","a"),
        new Link<string>("a","h"),
        new Link<string>("h","g"),
        new Link<string>("g","f"),
        new Link<string>("f","e"),
    };

    var graph = new Graph<string>(links);
    var paths = graph.RoutesBetween("a", "e");

    var list = paths.ToEnumerable().ToArray();
    Assert.AreEqual(list.Length, 2);

    Assert.IsTrue(list.Any(l => String.Join("-", 1) == "a-b-c-d-e"));
    Assert.IsTrue(list.Any(l => String.Join("-", 1) == "a-h-g-f-e"));
}

How can I check the list ( links ) if it is not being passed as a method parameter?

The class, constructor and method:

namespace Graph
{
    public interface IGraph<T>
    {
        IObservable<IEnumerable<T>> RoutesBetween(T source, T target);
    }

    public class Graph<T> : IGraph<T>
    {
        public Graph(IEnumerable<ILink<T>> links)
        {

        }
    }

    publi IObservable<IEnumerable<T>> RoutesBetween(T source, T target)
    {
        throw new NotImplementedException();
    }
}
    
asked by anonymous 21.12.2018 / 01:23

1 answer

1

You can save the "links" reference using the argument passed to the constructor:

public class Graph<T> : IGraph<T>
{
    private IEnumerable<ILink<T>> links;
    public Graph(IEnumerable<ILink<T>> links)
    {
        this.links = links;
    }
}

And then access the links variable in the RoutesBetween method.

public IObservable<IEnumerable<T>> RoutesBetween(T source, T target)
{
   // links -> tem valor
}
    
10.01.2019 / 12:51