Browse files

+ Updated Hoe setup.

Sorted test and impl methods.
+ Switched to minitest.
[git-p4: depot-paths = "//src/graph/dev/": change = 5683]
  • Loading branch information...
1 parent f057064 commit c4888a778b818c303fcce3ca21a6985d62b60b43 @zenspider zenspider committed Mar 5, 2010
Showing with 58 additions and 63 deletions.
  1. +3 −19 .autotest
  2. +6 −3 Rakefile
  3. +37 −29 lib/graph.rb
  4. +12 −12 test/test_graph.rb
View
22 .autotest
@@ -3,22 +3,6 @@
require 'autotest/restart'
require 'autotest/rcov'
-# Autotest.add_hook :initialize do |at|
-# at.extra_files << "../some/external/dependency.rb"
-#
-# at.libs << ":../some/external"
-#
-# at.add_exception 'vendor'
-#
-# at.add_mapping(/dependency.rb/) do |f, _|
-# at.files_matching(/test_.*rb$/)
-# end
-#
-# %w(TestA TestB).each do |klass|
-# at.extra_class_map[klass] = "test/test_misc.rb"
-# end
-# end
-
-# Autotest.add_hook :run_command do |at|
-# system "rake build"
-# end
+Autotest.add_hook :initialize do |at|
+ at.testlib = "minitest/autorun"
+end
View
9 Rakefile
@@ -4,9 +4,12 @@ require 'rubygems'
require 'hoe'
require './lib/graph.rb'
-Hoe.new('graph', Graph::VERSION) do |p|
- p.rubyforge_name = 'seattlerb'
- p.developer('Ryan Davis', 'ryand-ruby@zenspider.com')
+Hoe.plugin :seattlerb
+
+Hoe.spec 'graph' do
+ developer 'Ryan Davis', 'ryand-ruby@zenspider.com'
+
+ self.rubyforge_name = 'seattlerb'
end
# vim: syntax=Ruby
View
66 lib/graph.rb
@@ -8,29 +8,30 @@ class Graph < Hash
attr_reader :order
attr_reader :edge
- def initialize
- super { |h,k| h[k] = [] }
- @prefix = []
- @order = []
- @attribs = Hash.new { |h,k| h[k] = [] }
- @edge = Hash.new { |h,k| h[k] = Hash.new { |h2,k2| h2[k2] = [] } }
- end
-
def []= key, val
@order << key unless self.has_key? key
super
end
- def delete key
- @order.delete key
+ def clear
super
+ @prefix.clear
+ @order.clear
+ @attribs.clear
+ @edge.clear
end
- def filter_size minimum
- counts.each do |node, count|
- next unless count < minimum
- delete node
+ def counts
+ result = Hash.new 0
+ each_pair do |from, to|
+ result[from] += 1
end
+ result
+ end
+
+ def delete key
+ @order.delete key
+ super
end
def each_pair
@@ -41,18 +42,25 @@ def each_pair
end
end
- def invert
- result = self.class.new
- each_pair do |from, to|
- result[to] << from
+ def filter_size minimum
+ counts.each do |node, count|
+ next unless count < minimum
+ delete node
end
- result
end
- def counts
- result = Hash.new 0
+ def initialize
+ super { |h,k| h[k] = [] }
+ @prefix = []
+ @order = []
+ @attribs = Hash.new { |h,k| h[k] = [] }
+ @edge = Hash.new { |h,k| h[k] = Hash.new { |h2,k2| h2[k2] = [] } }
+ end
+
+ def invert
+ result = self.class.new
each_pair do |from, to|
- result[from] += 1
+ result[to] << from
end
result
end
@@ -61,6 +69,13 @@ def keys_by_count
counts.sort_by { |key, count| -count }.map {|key, count| key }
end
+ def save path, type="png"
+ File.open "#{path}.dot", "w" do |f|
+ f.write self.to_s
+ end
+ system "dot -T#{type} #{path}.dot > #{path}.#{type}" if type
+ end
+
def to_s
result = []
result << "digraph absent"
@@ -83,11 +98,4 @@ def to_s
result << " }"
result.join "\n"
end
-
- def save path, type="png"
- File.open "#{path}.dot", "w" do |f|
- f.write self.to_s
- end
- system "dot -T#{type} #{path}.dot > #{path}.#{type}" if type
- end
end
View
24 test/test_graph.rb
@@ -1,17 +1,13 @@
-require "test/unit"
+require "minitest/autorun"
require "tmpdir"
require "graph"
-class TestGraph < Test::Unit::TestCase
+class TestGraph < MiniTest::Unit::TestCase
def setup
@graph = Graph.new
@graph["a"] << "b"
end
- def test_to_s_empty
- assert_equal util_dot, Graph.new.to_s
- end
-
def test_delete
assert_equal %w(b), @graph.delete("a")
assert_equal [], @graph.order
@@ -57,19 +53,23 @@ def test_to_s
assert_equal expected, @graph.to_s
end
- def test_to_s_prefix
- @graph.prefix << "blah"
+ def test_to_s_attrib
+ @graph.attribs["a"] << "color = blue"
@graph["a"] << "c"
- expected = util_dot('blah', '"a" -> "b"', '"a" -> "c"')
+ expected = util_dot('"a" [ color = blue ]', '"a" -> "b"', '"a" -> "c"')
assert_equal expected, @graph.to_s
end
- def test_to_s_attrib
- @graph.attribs["a"] << "color = blue"
+ def test_to_s_empty
+ assert_equal util_dot, Graph.new.to_s
+ end
+
+ def test_to_s_prefix
+ @graph.prefix << "blah"
@graph["a"] << "c"
- expected = util_dot('"a" [ color = blue ]', '"a" -> "b"', '"a" -> "c"')
+ expected = util_dot('blah', '"a" -> "b"', '"a" -> "c"')
assert_equal expected, @graph.to_s
end

0 comments on commit c4888a7

Please sign in to comment.