From c4888a778b818c303fcce3ca21a6985d62b60b43 Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Fri, 5 Mar 2010 14:49:28 -0800 Subject: [PATCH] + Updated Hoe setup. Sorted test and impl methods. + Switched to minitest. [git-p4: depot-paths = "//src/graph/dev/": change = 5683] --- .autotest | 22 +++------------- Rakefile | 9 ++++--- lib/graph.rb | 66 ++++++++++++++++++++++++++-------------------- test/test_graph.rb | 24 ++++++++--------- 4 files changed, 58 insertions(+), 63 deletions(-) diff --git a/.autotest b/.autotest index 2fef613..f4207d0 100644 --- a/.autotest +++ b/.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 diff --git a/Rakefile b/Rakefile index 64c9ed0..d1b9114 100644 --- a/Rakefile +++ b/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 diff --git a/lib/graph.rb b/lib/graph.rb index 02bd474..253cd9d 100755 --- a/lib/graph.rb +++ b/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 diff --git a/test/test_graph.rb b/test/test_graph.rb index 5eb3792..b4e8e58 100644 --- a/test/test_graph.rb +++ b/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