Skip to content

Commit

Permalink
+ Updated Hoe setup.
Browse files Browse the repository at this point in the history
Sorted test and impl methods.
+ Switched to minitest.
[git-p4: depot-paths = "//src/graph/dev/": change = 5683]
  • Loading branch information
zenspider committed Mar 5, 2010
1 parent f057064 commit c4888a7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 63 deletions.
22 changes: 3 additions & 19 deletions .autotest
Expand Up @@ -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
9 changes: 6 additions & 3 deletions Rakefile
Expand Up @@ -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
66 changes: 37 additions & 29 deletions lib/graph.rb
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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
24 changes: 12 additions & 12 deletions 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
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit c4888a7

Please sign in to comment.