gviz 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +12 -11
  2. data/lib/gviz/version.rb +1 -1
  3. data/spec/gviz_spec.rb +453 -283
  4. metadata +2 -2
data/README.md CHANGED
@@ -24,19 +24,22 @@ A simple example.
24
24
  # save to files with dot and png formats(save method)
25
25
  require "gviz"
26
26
 
27
- gv = Gviz.new
28
- gv.graph do
27
+ Graph do
29
28
  route :main => [:init, :parse, :cleanup, :printf]
30
29
  route :init => :make, :parse => :execute
31
30
  route :execute => [:make, :compare, :printf]
31
+
32
+ save(:sample1, :png)
32
33
  end
33
34
 
34
- gv.save(:sample1, :png)
35
+ `Graph` is a shortcut for `Gviz.new.graph`.
35
36
 
36
37
  This outputs `sample1.dot` and `sample1.png` files shown as below.
37
38
 
38
39
  ![sample1](http://github.com/melborne/Gviz/raw/master/examples/sample1.png)
39
40
 
41
+
42
+
40
43
  Add some attributes to the graph, nodes, edges.
41
44
 
42
45
  ## add color to all nodes with color set(nodes, nodeset and node methods)
@@ -45,8 +48,7 @@ Add some attributes to the graph, nodes, edges.
45
48
  ## add bgcolor to a graph(global method)
46
49
  require "gviz"
47
50
 
48
- gv = Gviz.new
49
- gv.graph do
51
+ Graph.graph do
50
52
  route :main => [:init, :parse, :cleanup, :printf]
51
53
  route :init => :make, :parse => :execute
52
54
  route :execute => [:make, :compare, :printf]
@@ -56,9 +58,9 @@ Add some attributes to the graph, nodes, edges.
56
58
  edges(arrowhead:'onormal', style:'bold', color:'magenta4')
57
59
  edge(:main_printf, arrowtail:'diamond', dir:'both', color:'#3355FF')
58
60
  global(bgcolor:'powderblue')
61
+
62
+ save(:sample2, :png)
59
63
  end
60
-
61
- gv.save(:sample2, :png)
62
64
 
63
65
  This outputs below.
64
66
 
@@ -72,8 +74,7 @@ Modify some.
72
74
  ## define subgraph(subgraph method)
73
75
  require "gviz"
74
76
 
75
- gv = Gviz.new
76
- gv.graph do
77
+ Graph.graph do
77
78
  route :main => [:init, :parse, :cleanup, :printf]
78
79
  route :init => :make, :parse => :execute
79
80
  route :execute => [:make, :compare, :printf]
@@ -93,9 +94,9 @@ Modify some.
93
94
  node :init
94
95
  node :make
95
96
  end
97
+
98
+ save(:sample3, :png)
96
99
  end
97
-
98
- gv.save(:sample3, :png)
99
100
 
100
101
  This outputs below.
101
102
 
@@ -1,3 +1,3 @@
1
1
  module Gviz
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -1,241 +1,356 @@
1
1
  # encoding: UTF-8
2
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
 
4
- describe Gviz do
5
- before(:each) do
6
- @g = Gviz.new
7
- end
8
-
9
- context "node" do
10
- it "add a node" do
11
- node = @g.node(:a)
12
- node.should be_a_instance_of Gviz::Node
13
- node.id.should eql :a
14
- node.attrs.should be_empty
15
- @g.nodeset.map(&:id).should eql [:a]
4
+ describe Gviz::Node do
5
+ describe ".new" do
6
+ context "when only a symbol passed" do
7
+ subject { Gviz::Node.new(:a) }
8
+ it { should be_a_instance_of Gviz::Node }
9
+ its(:id) { should be :a }
10
+ its(:attrs) { should be_empty }
16
11
  end
17
-
18
- it "add nodes" do
19
- @g.node(:a)
20
- @g.node(:b)
21
- @g.nodeset.map(&:id).should eql [:a, :b]
12
+
13
+ context "when only a string passed" do
14
+ subject { Gviz::Node.new('a') }
15
+ it { should be_a_instance_of Gviz::Node }
16
+ its(:id) { should eq 'a' }
17
+ its(:attrs) { should be_empty }
22
18
  end
23
19
 
24
- it "add a node with attrs" do
25
- node = @g.node(:a, color:'blue', label:'hello')
26
- node.should be_a_instance_of Gviz::Node
27
- node.id.should eql :a
28
- node.attrs.should == { color:'blue', label:'hello' }
29
- @g.nodeset.map(&:attrs).should eql [node.attrs]
20
+ context "when a symbol and hash options passed" do
21
+ opts = { shape:'circle', style:'filled' }
22
+ subject { Gviz::Node.new(:a, opts) }
23
+ its(:id) { should be :a }
24
+ its(:attrs) { should eq opts }
30
25
  end
31
26
 
32
- it "update node attrs" do
33
- @g.node(:a, color:'red', label:'hello')
34
- @g.node(:a, color:'blue', shape:'box')
35
- @g.nodeset.first.attrs.should == { color:'blue', label:'hello', shape:'box' }
27
+ context "when a symbol with underscore passed" do
28
+ it "raise an error" do
29
+ ->{ Gviz::Node.new(:a_b) }.should raise_error(ArgumentError)
30
+ end
36
31
  end
32
+ end
37
33
 
38
- it "raise an error with a string" do
39
- ->{ @g.node('a') }.should raise_error(ArgumentError)
34
+ describe "#to_s" do
35
+ subject { Gviz::Node.new(:a, style:'filled').to_s }
36
+ it { should eq "a" }
37
+ end
38
+ end
39
+
40
+ describe Gviz::Edge do
41
+ describe ".new" do
42
+ context "when a symbol with an underscore passed" do
43
+ subject { Gviz::Edge.new(:a_b) }
44
+ it { should be_a_instance_of Gviz::Edge }
45
+ its(:id) { should be :a_b }
46
+ its(:st) { should be :a }
47
+ its(:ed) { should be :b }
48
+ its(:seq) { should eq 0 }
49
+ its(:st_port) { should be_nil }
50
+ its(:ed_port) { should be_nil }
51
+ its(:to_s) { should eq "a -> b" }
52
+ end
53
+
54
+ context "when a string with an underscore passed" do
55
+ subject { Gviz::Edge.new('a_b') }
56
+ it { should be_a_instance_of Gviz::Edge }
57
+ its(:id) { should be :a_b }
58
+ its(:st) { should be :a }
59
+ its(:ed) { should be :b }
60
+ its(:seq) { should eq 0 }
61
+ its(:st_port) { should be_nil }
62
+ its(:ed_port) { should be_nil }
63
+ its(:to_s) { should eq "a -> b" }
64
+ end
65
+
66
+ context "when a symbol with two underscores passed" do
67
+ subject { Gviz::Edge.new('a_b_1') }
68
+ it { should be_a_instance_of Gviz::Edge }
69
+ its(:id) { should be :a_b_1 }
70
+ its(:st) { should be :a }
71
+ its(:ed) { should be :b }
72
+ its(:seq) { should eq 1 }
73
+ its(:st_port) { should be_nil }
74
+ its(:ed_port) { should be_nil }
75
+ its(:to_s) { should eq "a -> b" }
76
+ end
77
+
78
+ context "when a string with a colon passed" do
79
+ subject { Gviz::Edge.new('a:x_b:y') }
80
+ it { should be_a_instance_of Gviz::Edge }
81
+ its(:id) { should be :a_b }
82
+ its(:st) { should be :a }
83
+ its(:ed) { should be :b }
84
+ its(:seq) { should eq 0 }
85
+ its(:st_port) { should be :x }
86
+ its(:ed_port) { should be :y }
87
+ its(:to_s) { should eq "a:x -> b:y" }
88
+ end
89
+
90
+ context "when a symbol and hash options passed" do
91
+ opts = { style:'bold', color:'red' }
92
+ subject { Gviz::Edge.new(:a_b, opts) }
93
+ its(:id) { should be :a_b }
94
+ its(:attrs) { should eq opts }
95
+ end
96
+
97
+ context "when a string with other than words or colon passed" do
98
+ it "raise an error" do
99
+ ->{ Gviz::Edge.new('a!b_c') }.should raise_error(ArgumentError)
100
+ end
40
101
  end
102
+ end
103
+ end
104
+
105
+ describe Gviz do
106
+ let(:gv) { Gviz.new }
41
107
 
42
- it "raise an error with a symbol which include underscores" do
43
- ->{ @g.node(:a_b) }.should raise_error(ArgumentError)
108
+ describe "#node" do
109
+ context "define a node" do
110
+ before { gv.node(:a) }
111
+ subject { gv.nodeset }
112
+ its(:size) { should eql 1}
113
+ its(:first) { should be_a_instance_of Gviz::Node }
44
114
  end
45
- end
46
115
 
47
- context "edge" do
48
- it "add a edge" do
49
- edge = @g.edge(:a_b)
50
- edge.should be_a_instance_of Gviz::Edge
51
- edge.st.should eql :a
52
- edge.ed.should eql :b
53
- edge.attrs.should be_empty
54
- @g.edgeset.map(&:to_s).should eql ["a -> b"]
116
+ context "define several nodes" do
117
+ before do
118
+ gv.node(:a)
119
+ gv.node(:b)
120
+ end
121
+ subject { gv.nodeset }
122
+ its(:size) { should eql 2 }
55
123
  end
56
124
 
57
- it "add a edge with attrs" do
58
- edge = @g.edge(:a_b, color:'red', arrowhead:'none')
59
- edge.should be_a_instance_of Gviz::Edge
60
- edge.st.should eql :a
61
- edge.ed.should eql :b
62
- edge.attrs.should == { color:'red', arrowhead:'none' }
63
- @g.edgeset.map(&:to_s).should eql ["a -> b"]
125
+ context "define a node with attrs" do
126
+ subject { gv.node(:a, color:'blue', label:'hello') }
127
+ its(:id) { should eql :a }
128
+ its(:attrs) { should be { color:'blue', label:'hello' } }
64
129
  end
65
130
 
66
- it "update edge attrs" do
67
- @g.edge(:a_b, color:'blue', style:'bold')
68
- @g.edge(:a_b, color:'red')
69
- @g.edgeset.map(&:to_s).should eql ["a -> b"]
70
- @g.edgeset.first.attrs.should == { color:'red', style:'bold' }
131
+ context "update node attrs" do
132
+ before do
133
+ gv.node(:a, color:'red', label:'hello')
134
+ gv.node(:a, color:'blue', shape:'box')
135
+ end
136
+ subject { gv.nodeset.first.attrs }
137
+ it { should be { color:'blue', label:'hello', shape:'box' } }
71
138
  end
72
139
 
73
- it "add different edges, but same name" do
74
- edge1 = @g.edge(:a_b)
75
- edge2 = @g.edge(:a_b_1)
76
- edge1.seq.should eql 0
77
- edge2.seq.should eql 1
78
- @g.edgeset.map(&:to_s).should eql ["a -> b", "a -> b"]
140
+ context "when pass a string" do
141
+ it "raise an error" do
142
+ ->{ gv.node('a') }.should raise_error(ArgumentError)
143
+ end
79
144
  end
80
145
 
81
- it "can accept a string id" do
82
- @g.edge('a_b')
83
- @g.edgeset.first.to_s.should eql "a -> b"
146
+ context "when pass a symbol with an underscore" do
147
+ it "raise an error" do
148
+ ->{ gv.node(:a_b) }.should raise_error(ArgumentError)
149
+ end
84
150
  end
151
+ end
85
152
 
86
- it "can take ports with a string id" do
87
- @g.edge("a:n_b:f")
88
- edge = @g.edgeset.first
89
- edge.st.should eql :a
90
- edge.ed.should eql :b
91
- edge.st_port.should eql :n
92
- edge.ed_port.should eql :f
93
- edge.to_s.should eql "a:n -> b:f"
153
+ describe "#edge" do
154
+ context "define a edge" do
155
+ before { gv.edge(:a_b) }
156
+ subject { gv.edgeset }
157
+ its(:size) { should eql 1}
158
+ its(:first) { should be_a_instance_of Gviz::Edge }
94
159
  end
95
-
96
- it "can take ports with a string id 2" do
97
- @g.add(:a => :b)
98
- @g.edge("a:n_b:f", color:'red')
99
- @g.edgeset.map(&:id).should eql [:a_b]
100
-
160
+
161
+ context "update edge attrs" do
162
+ before do
163
+ gv.edge(:a_b, color:'blue', style:'bold')
164
+ gv.edge(:a_b, color:'red')
165
+ end
166
+ subject { gv.edgeset.first }
167
+ its(:attrs) { should be { color:'red', style:'bold'} }
101
168
  end
102
169
 
103
- it "raise an error with a node which include other than words or colon" do
104
- ->{ @g.edge "b c" }.should raise_error(ArgumentError)
105
- ->{ @g.edge "b/c" }.should raise_error(ArgumentError)
106
- ->{ @g.edge "bc!" }.should raise_error(ArgumentError)
170
+ context "define different edges with same name" do
171
+ before do
172
+ gv.edge(:a_b)
173
+ gv.edge(:a_b_1)
174
+ end
175
+ subject { gv.edgeset }
176
+ its(:size) { should eql 2 }
107
177
  end
108
178
  end
109
179
 
110
- context "add" do
180
+ describe "#add" do
111
181
  context "edges" do
112
- it "one to many" do
113
- @g.add :a => [:b, :c, :d]
114
- @g.edgeset.map(&:to_s).should eql ['a -> b', 'a -> c', 'a -> d']
115
- @g.nodeset.map(&:id).should eql [:a, :b, :c, :d]
182
+ context "one to many" do
183
+ before { gv.add :a => [:b, :c, :d] }
184
+ subject { gv.edgeset }
185
+ its(:size) { should eq 3 }
186
+ describe "nodes" do
187
+ subject { gv.nodeset }
188
+ its(:size) { should eq 4 }
189
+ end
116
190
  end
117
191
 
118
- it "many to many" do
119
- @g.add [:main, :sub] => [:a, :b, :c]
120
- @g.edgeset.map(&:to_s)
121
- .should eql ['main -> a', 'main -> b', 'main -> c',
122
- 'sub -> a', 'sub -> b', 'sub -> c']
123
- @g.nodeset.map(&:id).should eql [:main, :a, :b, :c, :sub]
192
+ context "many to many" do
193
+ before { gv.add [:main, :sub] => [:a, :b, :c] }
194
+ subject { gv.edgeset }
195
+ its(:size) { should eq 6 }
196
+ describe "nodes" do
197
+ subject { gv.nodeset }
198
+ its(:size) { should eq 5 }
199
+ end
124
200
  end
125
201
 
126
- it "sequence" do
127
- @g.add :main => :sub, :sub => [:a, :b]
128
- @g.edgeset.map(&:to_s)
129
- .should eql ['main -> sub', 'sub -> a', 'sub -> b']
130
- @g.nodeset.map(&:id).should eql [:main, :sub, :a, :b]
202
+ context "pass two routes" do
203
+ before { gv.add :main => :sub, :sub => [:a, :b] }
204
+ subject { gv.edgeset }
205
+ its(:size) { should eq 3 }
206
+ describe "nodes" do
207
+ subject { gv.nodeset }
208
+ its(:size) { should eq 4 }
209
+ end
131
210
  end
132
211
 
133
- it "several sequences" do
134
- @g.add :main => :a, :a => [:c, :d]
135
- @g.add :main => :b, :b => [:e, :f]
136
- @g.edgeset.map(&:to_s)
137
- .should eql ['main -> a', 'a -> c', 'a -> d',
138
- 'main -> b', 'b -> e', 'b -> f']
139
- @g.nodeset.map(&:id).should eql [:main, :a, :c, :d, :b, :e, :f]
212
+ context "when call twice" do
213
+ before do
214
+ gv.add :main => :a, :a => [:c, :d]
215
+ gv.add :main => :b, :b => [:e, :f]
216
+ end
217
+ subject { gv.edgeset }
218
+ its(:size) { should eq 6 }
219
+ describe "nodes" do
220
+ subject { gv.nodeset }
221
+ its(:size) { should eq 7 }
222
+ end
140
223
  end
141
224
  end
142
225
 
143
226
  context "nodes" do
144
- it "add a node" do
145
- @g.add :a
146
- @g.nodeset.map(&:id).should eql [:a]
227
+ context "one" do
228
+ before { gv.add :a }
229
+ subject { gv.nodeset }
230
+ its(:size) { should eql 1 }
147
231
  end
148
232
 
149
- it "add several nodes" do
150
- @g.add :a, :b, :c
151
- @g.nodeset.map(&:id).should eql [:a, :b, :c]
233
+ context "several" do
234
+ before { gv.add :a, :b, :c }
235
+ subject { gv.nodeset }
236
+ its(:size) { should eql 3 }
152
237
  end
153
238
 
154
- it "raise error with a string" do
155
- ->{ @g.add 'a' }.should raise_error(ArgumentError)
239
+ context "pass a string" do
240
+ it "raise an error" do
241
+ ->{ gv.add 'a' }.should raise_error(ArgumentError)
242
+ end
156
243
  end
157
244
  end
158
245
  end
159
246
 
160
- context "graph" do
161
- it "add routes" do
162
- @g.graph do
163
- add :main => [:a, :b, :c]
164
- add :a => [:d, :e]
247
+ describe "#graph" do
248
+ context "when add routes" do
249
+ before do
250
+ gv.graph do
251
+ add :main => [:a, :b, :c]
252
+ add :a => [:d, :e]
253
+ end
165
254
  end
166
- @g.edgeset.map(&:to_s)
167
- .should eql ['main -> a', 'main -> b', 'main -> c',
168
- 'a -> d', 'a -> e']
169
- @g.nodeset.map(&:id).should eql [:main, :a, :b, :c, :d, :e]
255
+ subject { gv.edgeset }
256
+ its(:size) { should eq 5 }
170
257
  end
171
258
  end
172
259
 
173
- context "subgraph" do
174
- it "add subgraph" do
175
- sub = @g.subgraph do
176
- add :main => [:a, :b]
260
+ describe "#subgraph" do
261
+ context "when add one" do
262
+ before do
263
+ gv.subgraph { add :main => [:a, :b] }
177
264
  end
178
- @g.subgraphs.should eql [sub]
265
+ subject { gv.subgraphs }
266
+ its(:size) { should eq 1 }
179
267
  end
180
268
 
181
- it "raise error when name is not start with 'cluster'" do
182
- ->{ @g.subgraph(:clu) {} }.should raise_error
269
+ context "when add one" do
270
+ before do
271
+ gv.subgraph { add :main => [:a, :b] }
272
+ gv.subgraph { add :main => [:a, :b] }
273
+ end
274
+ subject { gv.subgraphs }
275
+ its(:size) { should eq 2 }
276
+ end
277
+
278
+ context "it has a name other than `cluster**`" do
279
+ it "raise an error" do
280
+ ->{ gv.subgraph(:clu) {} }.should raise_error
281
+ end
183
282
  end
184
283
  end
185
284
 
186
- context "nodes" do
187
- it "set nodes attributes globally" do
188
- attr = { style:"filled", color:"purple" }
189
- @g.nodes(attr)
190
- @g.gnode_attrs.should == attr
285
+ describe "#nodes" do
286
+ context "set node attributes" do
287
+ opts = { style:"filled", color:"purple" }
288
+ before { gv.nodes(opts) }
289
+ subject { gv.gnode_attrs }
290
+ it { should eql opts }
191
291
  end
192
292
 
193
- it "add nodes attributes globally" do
194
- attr = { style:"filled", color:"purple" }
195
- attr2 = { color:"red", shape:"box" }
196
- @g.nodes(attr)
197
- @g.nodes(attr2)
198
- @g.gnode_attrs.should == { style:"filled", color:"red", shape:"box" }
293
+ context "update node attributes" do
294
+ opts1 = { style:"filled", color:"purple" }
295
+ opts2 = { color:"red", shape:"box" }
296
+ before do
297
+ gv.nodes(opts1)
298
+ gv.nodes(opts2)
299
+ end
300
+ subject { gv.gnode_attrs }
301
+ it { should be { style:"filled", color:"red", shape:"box" } }
199
302
  end
200
303
  end
201
304
 
202
- context "edges" do
203
- it "set edges attributes globally" do
204
- attr = { style:"dotted", color:"purple" }
205
- @g.edges(attr)
206
- @g.gedge_attrs.should == attr
305
+ describe "#edges" do
306
+ context "set edge attributes" do
307
+ opts = { style:"dotted", color:"purple" }
308
+ before { gv.edges(opts) }
309
+ subject { gv.gedge_attrs }
310
+ it { should eq opts }
207
311
  end
208
312
 
209
- it "add edges attributes globally" do
210
- attr = { style:"dotted", color:"purple" }
211
- attr2 = { color:"red", arrowhead:"none" }
212
- @g.edges(attr)
213
- @g.edges(attr2)
214
- @g.gedge_attrs.should == { style:"dotted", color:"red", arrowhead:"none" }
313
+ context "update edge attributes" do
314
+ opts = { style:"dotted", color:"purple" }
315
+ opts2 = { color:"red", arrowhead:"none" }
316
+ before do
317
+ gv.edges(opts)
318
+ gv.edges(opts2)
319
+ end
320
+ subject { gv.gedge_attrs }
321
+ it { should be { style:"dotted", color:"red", arrowhead:"none" } }
215
322
  end
216
323
  end
217
324
 
218
- context "global" do
219
- it "set global graph attributes" do
220
- attrs = { label:"A simple graph", rankdir:"LR" }
221
- @g.global(attrs)
222
- @g.graph_attrs.should == attrs
325
+ describe "#global" do
326
+ context "set graph attributes" do
327
+ opts = { label:"A simple graph", rankdir:"LR" }
328
+ before { gv.global(opts) }
329
+ subject { gv.graph_attrs }
330
+ it { should eq opts }
223
331
  end
224
332
  end
225
333
 
226
- context "rank" do
227
- it "let nodes be same rank" do
228
- @g.route(:a => [:b, :c], :b => [:d, :e])
229
- @g.rank(:same, :b, :d, :e)
230
- @g.ranks.first.should eql [:same, [:b, :d, :e]]
334
+ describe "#rank" do
335
+ context "set same" do
336
+ before do
337
+ gv.route(:a => [:b, :c], :b => [:d, :e])
338
+ gv.rank(:same, :b, :d, :e)
339
+ end
340
+ subject { gv.ranks.first }
341
+ it { should eq [:same, [:b, :d, :e]] }
231
342
  end
232
343
  end
233
344
 
234
- context "to_s(output dot data)" do
235
- it "without attrs" do
236
- @g.add :main => [:init, :parse]
237
- @g.add :init => :printf
238
- @g.to_s.should eql ~<<-EOS
345
+ describe "#to_s" do
346
+ context "without attrs" do
347
+ before do
348
+ gv.add :main => [:init, :parse]
349
+ gv.add :init => :printf
350
+ end
351
+ subject { gv.to_s }
352
+ it do
353
+ should eq ~<<-EOS
239
354
  digraph G {
240
355
  main;
241
356
  init;
@@ -246,166 +361,221 @@ describe Gviz do
246
361
  init -> printf;
247
362
  }
248
363
  EOS
364
+ end
249
365
  end
250
366
 
251
- it "with node attrs" do
252
- @g.add :a => :b
253
- @g.node(:a, color:'red', style:'filled')
254
- @g.to_s.should eql ~<<-EOS
367
+ context "with node attrs" do
368
+ before do
369
+ gv.add :a => :b
370
+ gv.node(:a, color:'red', style:'filled')
371
+ end
372
+ subject { gv.to_s }
373
+ it do
374
+ should eq ~<<-EOS
255
375
  digraph G {
256
376
  a[color="red",style="filled"];
257
377
  b;
258
378
  a -> b;
259
379
  }
260
380
  EOS
381
+ end
261
382
  end
262
383
 
263
- it "with edge attrs" do
264
- @g.edge(:a_b, color:'red')
265
- @g.to_s.should eql ~<<-EOS
384
+ context "with edge attrs" do
385
+ before do
386
+ gv.edge(:a_b, color:'red')
387
+ end
388
+ subject { gv.to_s }
389
+ it do
390
+ should eq ~<<-EOS
266
391
  digraph G {
267
392
  a;
268
393
  b;
269
394
  a -> b[color="red"];
270
395
  }
271
396
  EOS
397
+ end
272
398
  end
273
399
 
274
- it "with 2 edges with different attrs" do
275
- @g.edge(:a_b, color:'red')
276
- @g.edge(:a_b_1, color:'blue')
277
- @g.to_s.should eql ~<<-EOS
278
- digraph G {
279
- a;
280
- b;
281
- a -> b[color="red"];
282
- a -> b[color="blue"];
283
- }
284
- EOS
400
+ context "with 2 edges with different attrs" do
401
+ before do
402
+ gv.edge(:a_b, color:'red')
403
+ gv.edge(:a_b_1, color:'blue')
404
+ end
405
+ subject { gv.to_s }
406
+ it do
407
+ should eq ~<<-EOS
408
+ digraph G {
409
+ a;
410
+ b;
411
+ a -> b[color="red"];
412
+ a -> b[color="blue"];
413
+ }
414
+ EOS
415
+ end
285
416
  end
286
417
 
287
- it "with global node attributes" do
288
- @g.nodes(shape:'box', style:'filled')
289
- @g.add(:a => :b)
290
- @g.to_s.should eql ~<<-EOS
291
- digraph G {
292
- node[shape="box",style="filled"];
293
- a;
294
- b;
295
- a -> b;
296
- }
297
- EOS
418
+ context "with global node attributes" do
419
+ before do
420
+ gv.nodes(shape:'box', style:'filled')
421
+ gv.add(:a => :b)
422
+ end
423
+ subject { gv.to_s }
424
+ it do
425
+ should eql ~<<-EOS
426
+ digraph G {
427
+ node[shape="box",style="filled"];
428
+ a;
429
+ b;
430
+ a -> b;
431
+ }
432
+ EOS
433
+ end
298
434
  end
299
435
 
300
- it "with global edges attributes" do
301
- @g.edges(style:'dotted', color:'red')
302
- @g.add(:a => :b)
303
- @g.to_s.should eql ~<<-EOS
304
- digraph G {
305
- edge[style="dotted",color="red"];
306
- a;
307
- b;
308
- a -> b;
309
- }
310
- EOS
436
+ context "with global edge attributes" do
437
+ before do
438
+ gv.edges(style:'dotted', color:'red')
439
+ gv.add(:a => :b)
440
+ end
441
+ subject { gv.to_s }
442
+ it do
443
+ should eql ~<<-EOS
444
+ digraph G {
445
+ edge[style="dotted",color="red"];
446
+ a;
447
+ b;
448
+ a -> b;
449
+ }
450
+ EOS
451
+ end
311
452
  end
312
453
 
313
- it "with global attributes" do
314
- @g.global(label:"A Simple Graph", rankdir:"LR")
315
- @g.add(:a => :b)
316
- @g.to_s.should eql ~<<-EOS
317
- digraph G {
318
- label="A Simple Graph";
319
- rankdir="LR";
320
- a;
321
- b;
322
- a -> b;
323
- }
324
- EOS
454
+ context "with graph attributes" do
455
+ before do
456
+ gv.global(label:"A Simple Graph", rankdir:"LR")
457
+ gv.add(:a => :b)
458
+ end
459
+ subject { gv.to_s }
460
+ it do
461
+ should eql ~<<-EOS
462
+ digraph G {
463
+ label="A Simple Graph";
464
+ rankdir="LR";
465
+ a;
466
+ b;
467
+ a -> b;
468
+ }
469
+ EOS
470
+ end
325
471
  end
326
472
 
327
- it "handle newline in a label nicely" do
328
- @g.node(:a, label:"hello\nworld")
329
- @g.to_s.should eql ~<<-EOS
330
- digraph G {
331
- a[label="hello\\nworld"];
332
- }
333
- EOS
473
+ context "when a label include `\\n`" do
474
+ before { gv.node(:a, label:"hello\nworld") }
475
+ subject { gv.to_s }
476
+ it do
477
+ should eql ~<<-EOS
478
+ digraph G {
479
+ a[label="hello\\nworld"];
480
+ }
481
+ EOS
482
+ end
334
483
  end
335
484
 
336
- it "can handle unicode labels" do
337
- @g.node(:a, label:"こんにちは、世界!")
338
- @g.to_s.should eql ~<<-EOS
339
- digraph G {
340
- a[label="こんにちは、世界!"];
341
- }
342
- EOS
485
+ context "when a label include unicode" do
486
+ before { gv.node(:a, label:"こんにちは、世界!") }
487
+ subject { gv.to_s }
488
+ it do
489
+ should eql ~<<-EOS
490
+ digraph G {
491
+ a[label="こんにちは、世界!"];
492
+ }
493
+ EOS
494
+ end
343
495
  end
344
496
 
345
- it "take port on edges" do
346
- @g.route(:a => [:b, :c])
347
- @g.edge("a:n_c:f")
348
- @g.node(:a, label:"<n> a | b |<p> c")
349
- @g.node(:c, label:"<o> d | e |<f> f")
350
- @g.to_s.should eql ~<<-EOS
351
- digraph G {
352
- a[label="<n> a | b |<p> c"];
353
- b;
354
- c[label="<o> d | e |<f> f"];
355
- a -> b;
356
- a:n -> c:f;
357
- }
358
- EOS
497
+ context "take ports on edges" do
498
+ before do
499
+ gv.route(:a => [:b, :c])
500
+ gv.edge("a:n_c:f")
501
+ gv.node(:a, label:"<n> a | b |<p> c")
502
+ gv.node(:c, label:"<o> d | e |<f> f")
503
+ end
504
+ subject { gv.to_s }
505
+ it do
506
+ should eql ~<<-EOS
507
+ digraph G {
508
+ a[label="<n> a | b |<p> c"];
509
+ b;
510
+ c[label="<o> d | e |<f> f"];
511
+ a -> b;
512
+ a:n -> c:f;
513
+ }
514
+ EOS
515
+ end
359
516
  end
360
517
 
361
- it "with subgraph" do
362
- @g.route(:a => :b)
363
- @g.subgraph do
364
- route :c => :d
518
+ context "with subgraph" do
519
+ before do
520
+ gv.route(:a => :b)
521
+ gv.subgraph { route :c => :d }
365
522
  end
366
- @g.to_s.should eql ~<<-EOS
367
- digraph G {
368
- a;
369
- b;
370
- a -> b;
371
- subgraph cluster0 {
372
- c;
373
- d;
374
- c -> d;
523
+ subject { gv.to_s }
524
+ it do
525
+ should eql ~<<-EOS
526
+ digraph G {
527
+ a;
528
+ b;
529
+ a -> b;
530
+ subgraph cluster0 {
531
+ c;
532
+ d;
533
+ c -> d;
534
+ }
375
535
  }
376
- }
377
- EOS
536
+ EOS
537
+ end
378
538
  end
379
539
 
380
- it "with ranks" do
381
- @g.route(:a => [:b, :c], :b => [:d, :e])
382
- @g.rank(:same, :b, :d, :e)
383
- @g.rank(:min, :c)
384
- @g.to_s.should eql ~<<-EOS
385
- digraph G {
386
- a;
387
- b;
388
- c;
389
- d;
390
- e;
391
- a -> b;
392
- a -> c;
393
- b -> d;
394
- b -> e;
395
- { rank=same; b; d; e; }
396
- { rank=min; c; }
397
- }
398
- EOS
540
+ context "with ranks" do
541
+ before do
542
+ gv.route(:a => [:b, :c], :b => [:d, :e])
543
+ gv.rank(:same, :b, :d, :e)
544
+ gv.rank(:min, :c)
545
+ end
546
+ subject { gv.to_s }
547
+ it do
548
+ should eql ~<<-EOS
549
+ digraph G {
550
+ a;
551
+ b;
552
+ c;
553
+ d;
554
+ e;
555
+ a -> b;
556
+ a -> c;
557
+ b -> d;
558
+ b -> e;
559
+ { rank=same; b; d; e; }
560
+ { rank=min; c; }
561
+ }
562
+ EOS
563
+ end
399
564
  end
400
565
  end
566
+ end
401
567
 
402
- context "Graph class method" do
403
- it "create a gviz object" do
404
- g = Graph(:X) do
568
+ describe '#Graph' do
569
+ context "build a shortcut of Gviz.new.graph" do
570
+ before do
571
+ @gv = Graph(:X) do
405
572
  add :a => [:b, :c]
406
573
  end
407
- g.should be_a_instance_of Gviz
408
- g.to_s.should eql ~<<-EOS
574
+ end
575
+ subject { @gv }
576
+ it { should be_a_instance_of Gviz }
577
+ its(:to_s) do
578
+ should eql ~<<-EOS
409
579
  digraph X {
410
580
  a;
411
581
  b;
@@ -416,4 +586,4 @@ describe Gviz do
416
586
  EOS
417
587
  end
418
588
  end
419
- end
589
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gviz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000 Z
12
+ date: 2012-10-07 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Ruby's interface of graphviz
15
15
  email: