
// Unit tests for reactive-property.
var assert = require("assert");

// If using from the NPM package, this line would be
// var Graph = require("graph-data-structure");
var Graph = require("./index.js");

describe("Graph", function() {

  describe("Data structure", function() {

    it("Should add nodes and list them.", function (){
      var graph = Graph();
      graph.addNode("a");
      graph.addNode("b");
      assert.equal(graph.nodes().length, 2);
      assert(contains(graph.nodes(), "a"));
      assert(contains(graph.nodes(), "b"));
    });

    it("Should remove nodes.", function (){
      var graph = Graph();
      graph.addNode("a");
      graph.addNode("b");
      graph.removeNode("a");
      graph.removeNode("b");
      assert.equal(graph.nodes().length, 0);
    });

    it("Should add edges and query for adjacent nodes.", function (){
      var graph = Graph();
      graph.addNode("a");
      graph.addNode("b");
      graph.addEdge("a", "b");
      assert.equal(graph.adjacent("a").length, 1);
      assert.equal(graph.adjacent("a")[0], "b");
    });

    it("Should implicitly add nodes when edges are added.", function (){
      var graph = Graph();
      graph.addEdge("a", "b");
      assert.equal(graph.adjacent("a").length, 1);
      assert.equal(graph.adjacent("a")[0], "b");
      assert.equal(graph.nodes().length, 2);
      assert(contains(graph.nodes(), "a"));
      assert(contains(graph.nodes(), "b"));
    });

    it("Should remove edges.", function (){
      var graph = Graph();
      graph.addEdge("a", "b");
      graph.removeEdge("a", "b");
      assert.equal(graph.adjacent("a").length, 0);
    });

    it("Should not remove nodes when edges are removed.", function (){
      var graph = Graph();
      graph.addEdge("a", "b");
      graph.removeEdge("a", "b");
      assert.equal(graph.nodes().length, 2);
      assert(contains(graph.nodes(), "a"));
      assert(contains(graph.nodes(), "b"));
    });

    it("Should remove outgoing edges when a node is removed.", function (){
      var graph = Graph();
      graph.addEdge("a", "b");
      graph.removeNode("a");
      assert.equal(graph.adjacent("a").length, 0);
    });

    it("Should remove incoming edges when a node is removed.", function (){
      var graph = Graph();
      graph.addEdge("a", "b");
      graph.removeNode("b");
      assert.equal(graph.adjacent("a").length, 0);
    });

  });


  describe("Edge cases and error handling", function() {

    it("Should return empty array of adjacent nodes for unknown nodes.", function (){
      var graph = Graph();
      assert.equal(graph.adjacent("a").length, 0);
      assert.equal(graph.nodes(), 0);
    });

    it("Should do nothing if removing an edge that does not exist.", function (){
      assert.doesNotThrow(function (){
        var graph = Graph();
        graph.removeEdge("a", "b");
      });
    });

  });
});

function contains(arr, item){
  return arr.filter(function (d){
    return d === item;
  }).length > 0;
}

@@ -2403,16 +2403,372 @@
 %0A  %7D);%0A%0A
+  describe(%22Algorithms%22, function() %7B%0A    it(%22Should compute topological sort.%22, function ()%7B%0A      var graph = Graph();%0A      graph.addEdge(%22a%22, %22b%22);%0A      graph.addEdge(%22b%22, %22c%22);%0A      var sorted = graph.topologicalSort(%5B%22a%22%5D);%0A      assert.equal(sorted.length, 2);%0A      assert.equal(sorted%5B0%5D, %22b%22);%0A      assert.equal(sorted%5B1%5D, %22c%22);%0A    %7D);%0A  %7D);%0A
 %0A  descr

Test topological sort