Created
August 14, 2013 06:03
-
-
Save aokolish/6228403 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Dependencies | |
def initialize | |
@dependencies = Hash.new {|h,k| h[k] = []} | |
end | |
def add_direct item, new_dependencies | |
@dependencies[item] += new_dependencies | |
end | |
def dependencies_for item | |
direct = @dependencies[item] | |
direct.inject(direct) do |memo, dependency| | |
memo += dependencies_for dependency | |
end.uniq.sort | |
end | |
end | |
require 'minitest/autorun' | |
describe 'Dependencies' do | |
it 'works' do | |
dep = Dependencies.new | |
dep.add_direct('A', %w{ B C } ) | |
dep.add_direct('B', %w{ C E } ) | |
dep.add_direct('C', %w{ G } ) | |
dep.add_direct('D', %w{ A F } ) | |
dep.add_direct('E', %w{ F } ) | |
dep.add_direct('F', %w{ H } ) | |
dep.dependencies_for('A').must_equal %w{ B C E F G H } | |
dep.dependencies_for('B').must_equal %w{ C E F G H } | |
dep.dependencies_for('C').must_equal %w{ G } | |
dep.dependencies_for('D').must_equal %w{ A B C E F G H } | |
dep.dependencies_for('E').must_equal %w{ F H } | |
dep.dependencies_for('F').must_equal %w{ H } | |
end | |
it 'with circular dependencies!!!' do | |
# fails | |
dep = Dependencies.new | |
dep.add_direct('A', %w{ B } ) | |
dep.add_direct('B', %w{ A } ) | |
dep.dependencies_for('A').must_equal %w{ B C E F G H } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment