UNPKG

5.9 kBJavaScriptView Raw
1var IperEdge, IperElement, IperGraph, IperNode, iper, should;
2
3iper = require('../index');
4
5should = require('should');
6
7IperEdge = iper.IperEdge;
8
9IperElement = iper.IperElement;
10
11IperGraph = iper.IperGraph;
12
13IperNode = iper.IperNode;
14
15describe('IperGraph', function() {
16 describe('inheritance', function() {
17 return it('is an IperElement', function() {
18 var graph;
19 graph = new IperGraph();
20 return graph.should.be.instanceOf(IperElement);
21 });
22 });
23 describe('constructor', function() {
24 it('has signature ()', function() {
25 var graph;
26 graph = new IperGraph();
27 return graph.should.be.instanceOf(IperGraph);
28 });
29 return it('has signature (data)', function() {
30 var data, graph;
31 data = {
32 nodes: {
33 1: 'foo',
34 2: 'bar'
35 },
36 edges: {
37 3: [1, 2]
38 }
39 };
40 graph = new IperGraph(data);
41 return graph.should.be.instanceOf(IperGraph);
42 });
43 });
44 describe('accessors', function() {
45 return describe('#data', function() {
46 return it('returns graph data', function() {
47 var data, edgeId1, graph, nodeId1, nodeId2;
48 graph = new IperGraph();
49 nodeId1 = graph.createNode('foo');
50 nodeId2 = graph.createNode([1, 2]);
51 edgeId1 = graph.createEdge([nodeId1, nodeId2]);
52 data = {};
53 data.nodes = {};
54 data.edges = {};
55 data.nodes[nodeId1] = 'foo';
56 data.nodes[nodeId2] = [1, 2];
57 data.edges[edgeId1] = [nodeId1, nodeId2];
58 return graph.data.should.eql(data);
59 });
60 });
61 });
62 return describe('methods', function() {
63 var data, graph;
64 graph = new IperGraph();
65 data = 'foo';
66 describe('#createNode()', function() {
67 return it('has signature (data), returns nodeId', function() {
68 var id;
69 id = graph.createNode(data);
70 return id.should.be.defined;
71 });
72 });
73 describe('#getNode()', function() {
74 it('has signature (id), returns node', function() {
75 var id, node;
76 id = graph.createNode(data);
77 node = graph.getNode(id);
78 node.should.be.instanceOf(IperNode);
79 return node.id.should.be.eql(id);
80 });
81 return it('throws error if edge does not exists', function() {
82 return (function() {
83 return graph.getNode(-1);
84 }).should.throwError();
85 });
86 });
87 describe('#check(data)', function() {
88 it('checks data is valid', function() {
89 data = {
90 nodes: {
91 1: 'foo',
92 2: 'bar'
93 },
94 edges: {
95 3: [5, 6]
96 }
97 };
98 return (function() {
99 return graph.check(data);
100 }).should.throwError();
101 });
102 return it('returns trus on success', function() {
103 graph = new IperGraph();
104 data = {
105 nodes: {
106 1: 'foo',
107 2: 'bar',
108 3: 'quz'
109 },
110 edges: {
111 4: [1, 2, 3]
112 }
113 };
114 return graph.check(data).should.be["true"];
115 });
116 });
117 describe('#load(data)', function() {
118 it('loads data', function() {
119 graph = new IperGraph();
120 data = {
121 nodes: {
122 1: 'foo'
123 },
124 edges: {
125 2: [1, 1]
126 }
127 };
128 graph.load(data);
129 return graph.check(graph.data).should.be["true"];
130 });
131 return it('checks data is valid', function() {
132 data = {
133 edges: {
134 1: [5, 6],
135 2: [3, 4]
136 }
137 };
138 return (function() {
139 return graph.load(data);
140 }).should.throwError();
141 });
142 });
143 describe('#removeNode()', function() {
144 it('has signature (id), removes node from its graph', function() {
145 var nodeId;
146 graph = new IperGraph();
147 nodeId = graph.createNode();
148 graph.removeNode(nodeId);
149 return (function() {
150 return graph.getNode(nodeid);
151 }).should.throwError();
152 });
153 return it('removes edges left without nodes', function() {});
154 });
155 describe('#getEdge()', function() {
156 it('has signature (id), returns edge', function() {
157 var edge, id, nodeId1, nodeId2, nodeIds;
158 nodeId1 = graph.createNode(1);
159 nodeId2 = graph.createNode(2);
160 nodeIds = [nodeId1, nodeId2];
161 id = graph.createEdge(nodeIds);
162 edge = graph.getEdge(id);
163 edge.should.be.instanceOf(IperEdge);
164 return edge.id.should.be.eql(id);
165 });
166 return it('throws error if edge does not exists', function() {
167 return (function() {
168 return graph.getEdge(-1);
169 }).should.throwError();
170 });
171 });
172 describe('#createEdge()', function() {
173 return it('has signature ([id1, id2, ...]), returns edge', function() {
174 var edge, id, nodeId1, nodeId2, nodeIds;
175 nodeId1 = graph.createNode(1);
176 nodeId2 = graph.createNode(2);
177 nodeIds = [nodeId1, nodeId2];
178 id = graph.createEdge(nodeIds);
179 edge = graph.getEdge(id);
180 edge.should.be.instanceOf(IperEdge);
181 return edge.id.should.be.eql(id);
182 });
183 });
184 return describe('#removeEdge()', function() {
185 return it('has signature (id), removes edge from its graph', function() {
186 var edgeId, nodeId1, nodeId2;
187 graph = new IperGraph();
188 nodeId1 = graph.createNode();
189 nodeId2 = graph.createNode();
190 edgeId = graph.createEdge([nodeId1, nodeId2]);
191 graph.removeEdge(edgeId);
192 return (function() {
193 return graph.getEdge(edgeId);
194 }).should.throwError();
195 });
196 });
197 });
198});