Skip to content

Commit 728a043

Browse files
RangerMauveRangerMauve
authored andcommitted
Implemented tests and fixed bugs
1 parent 4b57b20 commit 728a043

3 files changed

Lines changed: 118 additions & 11 deletions

File tree

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,5 @@ function addEdge(graph, edge) {
4545
var source = edge.source;
4646
var target = edge.target;
4747
var id = edge.id;
48-
graph.setEdge(source, target, edge, label);
48+
graph.setEdge(source, target, edge, id);
4949
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
"homepage": "https://github.com/jsongraph/graphlib-json-graph",
2424
"dependencies": {
2525
"graphlib": "^1.0.4",
26-
"mocha": "^2.2.5",
2726
"par": "^0.3.0",
2827
"prop": "^0.1.1"
28+
},
29+
"devDependencies": {
30+
"chai": "^3.0.0",
31+
"mocha": "^2.2.5"
2932
}
3033
}

test/index.js

Lines changed: 113 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,117 @@
1+
var assert = require("chai").assert;
12
var toGraph = require("../");
23

34
describe("toGraph()", function() {
4-
it.skip("should be able to parse a single graph", function() {});
5-
it.skip("should be able to parse multiple graphs", function() {});
6-
it.skip("should throw an error if there are no graphs", function() {});
7-
it.skip("should support graphs without nodes", function() {});
8-
it.skip("should support graphs without edges", function() {});
9-
it.skip("should respect the directed property of a graph", function() {});
10-
it.skip("should make a graph directed if any of the edges are directed", function() {});
11-
it.skip("should be a multigraph", function() {});
12-
it.skip("should use the id for distinguishing edges between the same nodes", function() {});
5+
var defaultGraph = {
6+
graph: {
7+
nodes: [{
8+
id: "1"
9+
}, {
10+
id: "2"
11+
}],
12+
edges: [{
13+
source: "1",
14+
target: "2"
15+
}]
16+
}
17+
};
18+
19+
it("should be able to parse a single graph", function() {
20+
var graph = toGraph(defaultGraph);
21+
});
22+
23+
it("should be able to parse multiple graphs", function() {
24+
var graphs = toGraph({
25+
graphs: [defaultGraph, defaultGraph, defaultGraph]
26+
});
27+
28+
assert.lengthOf(graphs, 3, "parsed three graphs");
29+
});
30+
31+
it("should throw an error if there are no graphs", function(done) {
32+
try {
33+
toGraph({});
34+
} catch (e) {
35+
done();
36+
}
37+
});
38+
39+
it("should support graphs without edges", function() {
40+
toGraph({
41+
graph: {
42+
nodes: [{
43+
id: 1
44+
}]
45+
}
46+
});
47+
});
48+
49+
it("should support graphs without nodes", function() {
50+
toGraph({
51+
graph: {}
52+
});
53+
});
54+
55+
it("should respect the directed property of a graph", function() {
56+
assert.notOk(toGraph({
57+
graph: {
58+
directed: false
59+
}
60+
}).isDirected(), "Graph is not directed");
61+
62+
assert.ok(toGraph({
63+
graph: {
64+
directed: true
65+
}
66+
}).isDirected(), "Graph is directed");
67+
});
68+
69+
it("should make a graph directed if any of the edges are directed", function() {
70+
assert.ok(toGraph({
71+
graph: {
72+
nodes: [{
73+
id: "1"
74+
}, {
75+
id: "2"
76+
}],
77+
edges: [{
78+
directed: true,
79+
source: 1,
80+
target: 1
81+
}]
82+
}
83+
}).isDirected(), "Graph is directed");
84+
});
85+
86+
it("should be a multigraph", function() {
87+
assert.ok(toGraph(defaultGraph).isMultigraph(), "Is a multigraph")
88+
});
89+
90+
it("should use the id for distinguishing edges between the same nodes", function() {
91+
var graph = toGraph({
92+
graph: {
93+
nodes: [{
94+
id: "1"
95+
}, {
96+
id: "2"
97+
}],
98+
edges: [{
99+
id: "e1",
100+
directed: true,
101+
source: "1",
102+
target: "2"
103+
}, {
104+
id: "e2",
105+
directed: true,
106+
source: "1",
107+
target: "2"
108+
}]
109+
}
110+
});
111+
112+
var e1 = graph.edge("1", "2", "e1");
113+
var e2 = graph.edge("1", "2", "e2");
114+
115+
assert.notEqual(e1, e2, "Edges were different")
116+
});
13117
});

0 commit comments

Comments
 (0)