Skip to content
This repository

Custom spec helpers for IR specs related to Vertex.java #904

Merged
merged 8 commits into from 12 months ago

2 participants

प्रथमेश Thomas E Enebo
This page is out of date. Refresh to see the latest.
30 spec/helpers/ir/edge_helpers.rb
... ... @@ -0,0 +1,30 @@
  1 +require 'java'
  2 +
  3 +import 'org.jruby.ir.util.Edge'
  4 +
  5 +class EdgeType
  6 +
  7 + def initialize(method, type)
  8 + @method = method
  9 + @edge_type = type
  10 + end
  11 +
  12 + def type
  13 + @actual.__send__(@method)
  14 + end
  15 +
  16 + def matches?(actual)
  17 + @actual = actual
  18 + type == @edge_type
  19 + end
  20 +end
  21 +
  22 +module HaveType
  23 + def have_type(type)
  24 + EdgeType.new(:getType, type)
  25 + end
  26 +end
  27 +
  28 +class Object
  29 + include HaveType
  30 +end
92 spec/helpers/ir/vertex_helpers.rb
... ... @@ -0,0 +1,92 @@
  1 +require 'java'
  2 +
  3 +import 'org.jruby.ir.util.Vertex'
  4 +
  5 +class DegreeMatcher
  6 +
  7 + def initialize(method, degree)
  8 + @method = method
  9 + @value = degree
  10 + end
  11 +
  12 + def degree
  13 + @actual.__send__(@method)
  14 + end
  15 +
  16 + def matches?(actual)
  17 + @actual = actual
  18 + degree == @value
  19 + end
  20 +
  21 +end
  22 +
  23 +module HaveInDegree
  24 + def have_in_degree(degree)
  25 + DegreeMatcher.new(:inDegree, degree)
  26 + end
  27 +end
  28 +
  29 +module HaveOutDegree
  30 + def have_out_degree(degree)
  31 + DegreeMatcher.new(:outDegree, degree)
  32 + end
  33 +end
  34 +
  35 +class Object
  36 + include HaveInDegree
  37 + include HaveOutDegree
  38 +end
  39 +
  40 +class Vertex
  41 + def add_edge(options=nil)
  42 + self.addEdgeTo(options[:to], options[:type])
  43 + end
  44 +
  45 + def remove_edge(options=nil)
  46 + self.removeEdgeTo(options[:to])
  47 + end
  48 +
  49 + def remove_edges(options={})
  50 + case options[:direction]
  51 + when :in
  52 + self.removeAllIncomingEdges()
  53 + when :out
  54 + self.removeAllOutgoingEdges()
  55 + else
  56 + self.removeAllEdges()
  57 + end
  58 + end
  59 +
  60 + def outgoing_edge(options=nil)
  61 + if options.nil?
  62 + self.getOutgoingEdge
  63 + else
  64 + self.getOutgoingEdgeOfType(options[:type])
  65 + end
  66 + end
  67 +
  68 + def incoming_edge(options=nil)
  69 + if options.nil?
  70 + self.getIncomingEdge
  71 + else
  72 + self.getIncomingEdgeOfType(options[:type])
  73 + end
  74 + end
  75 +
  76 + def data(options={})
  77 + case options[:direction]
  78 + when :in
  79 + if options.keys.include?(:type)
  80 + self.getIncomingSourceDataOfType(options[:type])
  81 + else
  82 + self.getIncomingSourceData()
  83 + end
  84 + when :out
  85 + if options.keys.include?(:type)
  86 + self.getOutgoingDestinationDataOfType(options[:type])
  87 + else
  88 + self.getOutgoingDestinationData()
  89 + end
  90 + end
  91 + end
  92 +end
6 spec/ir/directed_graph/edge_type_iterator_spec.rb
... ... @@ -1,3 +1,7 @@
  1 +$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../helpers/ir"
  2 +
  3 +require 'edge_helpers'
  4 +
1 5 import 'org.jruby.ir.util.DirectedGraph'
2 6 import 'org.jruby.ir.util.EdgeTypeIterator'
3 7 import 'java.util.NoSuchElementException'
@@ -105,7 +109,7 @@
105 109
106 110 it "returns the next edge" do
107 111 iterator = EdgeTypeIterator.new(@graph.edges(), "foo", false)
108   - expect(iterator.next.getType).to eq "foo"
  112 + expect(iterator.next).to have_type("foo")
109 113 end
110 114 end
111 115
186 spec/ir/directed_graph/vertex_spec.rb
... ... @@ -1,3 +1,8 @@
  1 +$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../helpers/ir"
  2 +
  3 +require 'vertex_helpers'
  4 +require 'edge_helpers'
  5 +
1 6 import 'org.jruby.ir.util.DirectedGraph'
2 7 import 'org.jruby.ir.util.Vertex'
3 8
@@ -12,15 +17,15 @@
12 17 describe "Adding an edge from source to destination" do
13 18
14 19 before do
15   - @source.addEdgeTo(@dest)
  20 + @source.add_edge(to: @dest)
16 21 end
17 22
18 23 it "adds outgoing edge to source" do
19   - expect(@source.outDegree).to eq 1
  24 + expect(@source).to have_out_degree 1
20 25 end
21 26
22 27 it "adds incoming edge to destination" do
23   - expect(@dest.inDegree).to eq 1
  28 + expect(@dest).to have_in_degree 1
24 29 end
25 30
26 31 it "adds the edge to the graph containing source" do
@@ -28,13 +33,13 @@
28 33 end
29 34
30 35 it "sets edge type to null if is not provided" do
31   - expect(@graph.edges().first.getType).to be nil
  36 + expect(@graph.edges().first).to have_type(nil)
32 37 end
33 38
34 39 it "sets edge type to the given value if is provided" do
35   - @source.removeEdgeTo(@dest)
36   - @source.addEdgeTo(@dest, "foobar")
37   - expect(@graph.edges.first.getType).to eq "foobar"
  40 + @source.remove_edge(to: @dest)
  41 + @source.add_edge(to: @dest, type: "foobar")
  42 + expect(@graph.edges.first).to have_type("foobar")
38 43 end
39 44
40 45 end
@@ -42,19 +47,19 @@
42 47 describe "Removing an outgoing edge from current vertex" do
43 48
44 49 before do
45   - @source.addEdgeTo(@dest)
  50 + @source.add_edge(to: @dest)
46 51 end
47 52
48 53 context "Destination of any one of the outgoing edges from the current vertex matched with given destination" do
49 54
50 55 it "removes an edge from outgoing edges of the source vertex" do
51   - @source.removeEdgeTo(@dest)
52   - expect(@source.outDegree).to eq 0
  56 + @source.remove_edge(to: @dest)
  57 + expect(@source).to have_out_degree 0
53 58 end
54 59
55 60 it "removes an edge from incoming edges of the destination vertex" do
56   - @source.removeEdgeTo(@dest)
57   - expect(@dest.inDegree).to eq 0
  61 + @source.remove_edge(to: @dest)
  62 + expect(@dest).to have_in_degree 0
58 63 end
59 64
60 65 end
@@ -62,7 +67,7 @@
62 67 context "Destination of all of the outgoing edges from the current vertex doesn't match with given destination" do
63 68 it "returns false" do
64 69 non_existent_destination = Vertex.new(@graph, "baz", 3)
65   - expect(@source.removeEdgeTo(non_existent_destination)).to be false
  70 + expect(@source.remove_edge(to: non_existent_destination)).to be false
66 71 end
67 72 end
68 73
@@ -72,13 +77,13 @@
72 77
73 78 before do
74 79 @interim = Vertex.new(@graph, "interim", 3)
75   - @dest.addEdgeTo(@source)
76   - @interim.addEdgeTo(@source)
  80 + @dest.add_edge(to: @source)
  81 + @interim.add_edge(to: @source)
77 82 end
78 83
79 84 it "removes all incoming edges to the vertex" do
80   - @source.removeAllIncomingEdges()
81   - expect(@source.inDegree).to eq 0
  85 + @source.remove_edges(direction: :in)
  86 + expect(@source).to have_in_degree 0
82 87 end
83 88
84 89 end
@@ -87,13 +92,13 @@
87 92
88 93 before do
89 94 @interim = Vertex.new(@graph, "interim", 3)
90   - @source.addEdgeTo(@dest)
91   - @source.addEdgeTo(@interim)
  95 + @source.add_edge(to: @dest)
  96 + @source.add_edge(to: @interim)
92 97 end
93 98
94 99 it "removes all outgoing edges from the vertex" do
95   - @source.removeAllOutgoingEdges()
96   - expect(@source.outDegree).to eq 0
  100 + @source.remove_edges(direction: :out)
  101 + expect(@source).to have_out_degree 0
97 102 end
98 103
99 104 end
@@ -102,16 +107,16 @@
102 107
103 108 before do
104 109 @interim = Vertex.new(@graph, "interim", 3)
105   - @source.addEdgeTo(@dest)
106   - @source.addEdgeTo(@interim)
107   - @dest.addEdgeTo(@source)
108   - @interim.addEdgeTo(@source)
  110 + @source.add_edge(to: @dest)
  111 + @source.add_edge(to: @interim)
  112 + @dest.add_edge(to: @source)
  113 + @interim.add_edge(to: @source)
109 114 end
110 115
111 116 it "removes all edges from the vertex" do
112   - @source.removeAllEdges()
113   - expect(@source.outDegree).to eq 0
114   - expect(@source.inDegree).to eq 0
  117 + @source.remove_edges
  118 + expect(@source).to have_out_degree 0
  119 + expect(@source).to have_in_degree 0
115 120 end
116 121
117 122 end
@@ -123,16 +128,15 @@
123 128 end
124 129
125 130 it "returns first outgoing edge from the vertex not of type 'null'" do
126   - @source.addEdgeTo(@dest, "not_null")
127   - @source.addEdgeTo(@null_vertex, nil)
128   - expect(@source.getOutgoingEdge.getType).to_not be nil
129   - expect(@source.getOutgoingEdge.getType).to eq "not_null"
  131 + @source.add_edge(to: @dest, type: "not_null")
  132 + @source.add_edge(to: @null_vertex, type: nil)
  133 + expect(@source.outgoing_edge).to have_type("not_null")
130 134 end
131 135
132 136 it "returns null when all outgoing edges from the vertex are of type 'null'" do
133   - @source.addEdgeTo(@dest)
134   - @source.addEdgeTo(@null_vertex, nil)
135   - expect(@source.getOutgoingEdge).to be nil
  137 + @source.add_edge(to: @dest)
  138 + @source.add_edge(to: @null_vertex, type: nil)
  139 + expect(@source.outgoing_edge).to be nil
136 140 end
137 141 end
138 142
@@ -143,16 +147,15 @@
143 147 end
144 148
145 149 it "returns first incoming edge to the vertex not of type 'null'" do
146   - @source.addEdgeTo(@dest, "not_null")
147   - @null_vertex.addEdgeTo(@dest, nil)
148   - expect(@dest.getIncomingEdge.getType).to_not be nil
149   - expect(@dest.getIncomingEdge.getType).to eq "not_null"
  150 + @source.add_edge(to: @dest, type: "not_null")
  151 + @null_vertex.add_edge(to: @dest, type: nil)
  152 + expect(@dest.incoming_edge).to have_type("not_null")
150 153 end
151 154
152 155 it "returns null when all incoming edges to the vertex are of type 'null'" do
153   - @source.addEdgeTo(@dest)
154   - @null_vertex.addEdgeTo(@dest, nil)
155   - expect(@dest.getIncomingEdge).to be nil
  156 + @source.add_edge(to: @dest)
  157 + @null_vertex.add_edge(to: @dest, type: nil)
  158 + expect(@dest.incoming_edge).to be nil
156 159 end
157 160 end
158 161
@@ -160,15 +163,15 @@
160 163
161 164 context "when the edge of given type exists" do
162 165 it "returns first outgoing edge of the given type" do
163   - @source.addEdgeTo(@dest, "baz")
164   - expect(@source.getOutgoingEdgeOfType("baz").getType).to eq "baz"
  166 + @source.add_edge(to: @dest, type: "baz")
  167 + expect(@source.outgoing_edge(type: "baz")).to have_type("baz")
165 168 end
166 169 end
167 170
168 171 context "when the edge of given type does not exist" do
169 172 it "returns null" do
170   - @source.addEdgeTo(@dest, "foobarbaz")
171   - expect(@source.getOutgoingEdgeOfType("foo-bar-baz")).to be nil
  173 + @source.add_edge(to: @dest, type: "foobarbaz")
  174 + expect(@source.outgoing_edge(type: "foo-bar-baz")).to be nil
172 175 end
173 176 end
174 177 end
@@ -177,15 +180,15 @@
177 180
178 181 context "when the edge of given type exists" do
179 182 it "returns first incoming edge of the given type" do
180   - @source.addEdgeTo(@dest, "baz")
181   - expect(@dest.getIncomingEdgeOfType("baz").getType).to eq "baz"
  183 + @source.add_edge(to: @dest, type: "baz")
  184 + expect(@dest.incoming_edge(type: "baz")).to have_type("baz")
182 185 end
183 186 end
184 187
185 188 context "when the edge of given type does not exist" do
186 189 it "returns null" do
187   - @source.addEdgeTo(@dest, "foobarbaz")
188   - expect(@dest.getIncomingEdgeOfType("foo-bar-baz")).to be nil
  190 + @source.add_edge(to: @dest, type: "foobarbaz")
  191 + expect(@dest.incoming_edge(type: "foo-bar-baz")).to be nil
189 192 end
190 193 end
191 194 end
@@ -194,15 +197,15 @@
194 197
195 198 context "when there is atleast one incoming edge to the current vertex" do
196 199 it "returns data of the source of that first incoming edge" do
197   - @source.addEdgeTo(@dest)
198   - expect(@dest.getIncomingSourceData).to eq "foo"
  200 + @source.add_edge(to: @dest)
  201 + expect(@dest.data(direction: :in)).to eq "foo"
199 202 end
200 203 end
201 204
202 205 context "when there is no incoming edge to the current vertex" do
203 206 it "returns null" do
204   - @source.addEdgeTo(@dest)
205   - expect(@source.getIncomingSourceData).to be nil
  207 + @source.add_edge(to: @dest)
  208 + expect(@source.data(direction: :in)).to be nil
206 209 end
207 210 end
208 211
@@ -212,15 +215,15 @@
212 215
213 216 context "when there is atleast one incoming edge to the current vertex of the given type" do
214 217 it "returns data of the source of that first incoming edge of given type" do
215   - @source.addEdgeTo(@dest)
216   - expect(@dest.getIncomingSourceDataOfType(nil)).to eq "foo"
  218 + @source.add_edge(to: @dest)
  219 + expect(@dest.data(direction: :in, type: nil)).to eq "foo"
217 220 end
218 221 end
219 222
220 223 context "when there is no incoming edge to the current vertex of given type" do
221 224 it "returns null" do
222   - @source.addEdgeTo(@dest, "foo")
223   - expect(@dest.getIncomingEdgeOfType(nil)).to be nil
  225 + @source.add_edge(to: @dest, type: "foo")
  226 + expect(@dest.incoming_edge(type: nil)).to eq nil
224 227 end
225 228 end
226 229
@@ -230,15 +233,15 @@
230 233
231 234 context "when there is atleast one outgoing edge from the current vertex" do
232 235 it "returns data of the destination of that first outgoing edge" do
233   - @source.addEdgeTo(@dest)
234   - expect(@source.getOutgoingDestinationData).to eq "bar"
  236 + @source.add_edge(to: @dest)
  237 + expect(@source.data(direction: :out)).to eq "bar"
235 238 end
236 239 end
237 240
238 241 context "when there is no outgoing edge from the current vertex" do
239 242 it "returns null" do
240   - @source.addEdgeTo(@dest)
241   - expect(@dest.getOutgoingDestinationData).to be nil
  243 + @source.add_edge(to: @dest)
  244 + expect(@dest.data(direction: :out)).to be nil
242 245 end
243 246 end
244 247
@@ -248,18 +251,69 @@
248 251
249 252 context "when there is atleast one outgoing edge from the current vertex of the given type" do
250 253 it "returns data of the source of that first outgoing edge of given type" do
251   - @source.addEdgeTo(@dest)
252   - expect(@source.getOutgoingDestinationDataOfType(nil)).to eq "bar"
  254 + @source.add_edge(to: @dest)
  255 + expect(@source.data(direction: :out, type: nil)).to eq "bar"
253 256 end
254 257 end
255 258
256 259 context "when there is no outgoing edge from the current vertex of given type" do
257 260 it "returns null" do
258   - @source.addEdgeTo(@dest, "foo")
259   - expect(@source.getOutgoingDestinationDataOfType(nil)).to be nil
  261 + @source.add_edge(to: @dest, type: "foo")
  262 + expect(@source.data(direction: :out, type: nil)).to be nil
260 263 end
261 264 end
262 265
263 266 end
264 267
  268 + describe "toString" do
  269 +
  270 + before do
  271 + @interim = Vertex.new(@graph, "interim", 3)
  272 + end
  273 +
  274 + context "when vertex has no edges" do
  275 + it "returns string representation of the vertex" do
  276 + expect(@source.toString).to eq "foo:\n"
  277 + end
  278 + end
  279 +
  280 + context "when vertex has only one outgoing edge" do
  281 + it "returns string representation of the vertex" do
  282 + @source.add_edge(to: @dest)
  283 + expect(@source.toString).to eq "foo:>[2]\n"
  284 + end
  285 + end
  286 +
  287 + context "when vertex has many outgoing edges" do
  288 + it "returns string representation of the vertex" do
  289 + @source.add_edge(to: @dest)
  290 + @source.add_edge(to: @interim)
  291 + expect(["foo:>[2,3]\n", "foo:>[3,2]\n"]).to include @source.toString
  292 + end
  293 + end
  294 +
  295 + context "when vertex has only one incoming edge" do
  296 + it "returns string representation of the vertex" do
  297 + @source.add_edge(to: @dest)
  298 + expect(@dest.toString).to eq "bar:<[1]\n"
  299 + end
  300 + end
  301 +
  302 + context "when vertex has many incoming edges" do
  303 + it "returns string representation of the vertex" do
  304 + @source.add_edge(to: @dest)
  305 + @interim.add_edge(to: @dest)
  306 + expect(["bar:<[1,3]\n", "bar:<[3,1]\n"]).to include @dest.toString
  307 + end
  308 + end
  309 +
  310 + context "when vertex has both incoming and outgoing edges" do
  311 + it "returns string representation of the vertex" do
  312 + @source.add_edge(to: @interim)
  313 + @interim.add_edge(to: @dest)
  314 + expect(@interim.toString).to eq "interim:>[2], <[1]\n"
  315 + end
  316 + end
  317 +
  318 + end
265 319 end

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.