@@ -3,27 +3,42 @@ import 'package:test/test.dart';
3
3
import 'package:algorithms/graph/vertex.dart' ;
4
4
5
5
void main () {
6
- Vertex root;
7
- Vertex rootWithValue;
8
- Vertex connectedVertex;
9
- Vertex toBeAdded;
10
- Vertex anotherVertex;
11
-
12
- setUp (() {
6
+ Vertex root,
7
+ rootWithValue,
8
+ connectedVertex,
9
+ toBeAdded,
10
+ anotherVertex,
11
+ a,
12
+ b,
13
+ c;
14
+
15
+ void _initializeVertices () {
13
16
root = Vertex ('A' );
14
17
rootWithValue = Vertex ('a' , 'Wake up' );
15
-
16
18
connectedVertex = Vertex ('0' );
17
19
toBeAdded = Vertex ('1' );
18
20
anotherVertex = Vertex ('2' );
21
+ a = Vertex ('a' );
22
+ b = Vertex ('b' );
23
+ c = Vertex ('c' );
24
+ }
25
+
26
+ void _unlockVertices () {
27
+ unlockVertices (< Vertex > {
28
+ root,
29
+ rootWithValue,
30
+ connectedVertex,
31
+ toBeAdded,
32
+ anotherVertex,
33
+ a,
34
+ b,
35
+ c
36
+ });
37
+ }
19
38
20
- // For test purposes, unlock all vertices
21
- root.unlock ();
22
- rootWithValue.unlock ();
23
- connectedVertex.unlock ();
24
- toBeAdded.unlock ();
25
- anotherVertex.unlock ();
26
-
39
+ setUp (() {
40
+ _initializeVertices ();
41
+ _unlockVertices ();
27
42
connectedVertex.addConnection (toBeAdded);
28
43
connectedVertex.addConnection (anotherVertex);
29
44
});
@@ -36,10 +51,6 @@ void main() {
36
51
});
37
52
38
53
test ('Successfully add a vertex' , () {
39
- var b = Vertex ('B' );
40
- var c = Vertex ('C' );
41
- b.unlock ();
42
- c.unlock ();
43
54
expect (root.addConnection (b), isTrue);
44
55
expect (root.addConnection (c), isTrue);
45
56
expect (root.outgoingConnections.containsKey (b), isTrue);
@@ -49,8 +60,6 @@ void main() {
49
60
});
50
61
51
62
test ('Unsuccessfully add a vertex' , () {
52
- var b = Vertex ('B' );
53
- b.unlock ();
54
63
root.addConnection (b);
55
64
expect (root.addConnection (b), isFalse);
56
65
});
@@ -62,44 +71,42 @@ void main() {
62
71
});
63
72
64
73
test ('Unsuccessfully remove a vertex' , () {
65
- var aVertex = Vertex ('-1' );
66
- aVertex.unlock ();
67
- expect (connectedVertex.removeConnection (aVertex), isFalse);
74
+ expect (connectedVertex.removeConnection (a), isFalse);
68
75
});
69
76
70
77
test ('Trying to add to a locked vertex throws error' , () {
71
78
var locked = Vertex ('PROTECTED' );
72
- expect (() => locked.addConnection (root), throwsA (isA <Error >()));
79
+ expect (() => locked.addConnection (root), throwsA (isA <UnsupportedError >()));
73
80
root.lock ();
74
- expect (() => root.addConnection (root), throwsA (isA <Error >()));
81
+ expect (() => root.addConnection (root), throwsA (isA <UnsupportedError >()));
75
82
});
76
83
77
84
test ('Trying to add a locked vertex throws error' , () {
78
85
var locked = Vertex ('PROTECTED' );
79
- expect (() => root.addConnection (locked), throwsA (isA <Error >()));
86
+ expect (() => root.addConnection (locked), throwsA (isA <UnsupportedError >()));
80
87
locked.unlock ();
81
88
root.lock ();
82
- expect (() => locked.addConnection (root), throwsA (isA <Error >()));
89
+ expect (() => locked.addConnection (root), throwsA (isA <UnsupportedError >()));
83
90
});
84
91
85
92
test ('Trying to remove from a locked vertex throws error' , () {
86
93
toBeAdded.lock ();
87
94
expect (() => connectedVertex.removeConnection (toBeAdded),
88
- throwsA (isA <Error >()));
95
+ throwsA (isA <UnsupportedError >()));
89
96
});
90
97
91
98
test ('Trying to remove a locked vertex throws error' , () {
92
99
connectedVertex.lock ();
93
100
expect (() => connectedVertex.removeConnection (toBeAdded),
94
- throwsA (isA <Error >()));
101
+ throwsA (isA <UnsupportedError >()));
95
102
});
96
103
97
104
test ('Trying to remove a locked vertex throws error' , () {
98
105
var locked = Vertex ('PROTECTED' );
99
- expect (() => root.addConnection (locked), throwsA (isA <Error >()));
106
+ expect (() => root.addConnection (locked), throwsA (isA <UnsupportedError >()));
100
107
locked.unlock ();
101
108
root.lock ();
102
- expect (() => locked.addConnection (root), throwsA (isA <Error >()));
109
+ expect (() => locked.addConnection (root), throwsA (isA <UnsupportedError >()));
103
110
});
104
111
105
112
test ('Check for vertex containment' , () {
@@ -110,12 +117,6 @@ void main() {
110
117
});
111
118
112
119
test ('Get a list of vertices' , () {
113
- var a = Vertex ('A' );
114
- var b = Vertex ('B' );
115
- var c = Vertex ('C' );
116
- a.unlock ();
117
- b.unlock ();
118
- c.unlock ();
119
120
root.addConnection (a);
120
121
root.addConnection (b);
121
122
root.addConnection (c);
@@ -131,8 +132,6 @@ void main() {
131
132
});
132
133
133
134
test ('Get out degree for vertex' , () {
134
- var c = Vertex ('c' );
135
- c.unlock ();
136
135
expect (root.outDegree, equals (0 ));
137
136
connectedVertex.addConnection (c);
138
137
expect (connectedVertex.outDegree, equals (3 ));
0 commit comments