Created
October 15, 2014 16:16
-
-
Save naush/729ffb9588ac75124732 to your computer and use it in GitHub Desktop.
wioletta
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 Employee | |
attr_accessor :boss | |
attr_accessor :subordinates | |
def initialize | |
@subordinates = [] | |
end | |
def children | |
[].tap do |array| | |
subordinates.each do |subordinate| | |
array << subordinate | |
subordinate.subordinates.each do |subordinate| | |
array << subordinate | |
end | |
end | |
end | |
end | |
end |
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
require_relative 'employee' | |
describe Employee do | |
it 'has a boss' do | |
employee = Employee.new | |
boss = Employee.new | |
employee.boss = boss | |
employee.boss.should == boss | |
end | |
describe '#subordinates' do | |
let(:manager) { Employee.new} | |
let(:subordinate) {Employee.new} | |
it 'returns no subordinates' do | |
expect(manager.subordinates).to be_empty | |
end | |
it 'returns list of subordinates' do | |
manager.subordinates << subordinate | |
expect(manager.subordinates).to include subordinate | |
end | |
end | |
describe '#children' do | |
let(:manager) { Employee.new } | |
let(:subordinate_1) { Employee.new } | |
let(:subordinate_2) { Employee.new } | |
let(:subordinate_3) { Employee.new } | |
it 'returns a list of everyone on the next two levels' do | |
manager.subordinates << subordinate_1 | |
subordinate_1.subordinates << subordinate_2 | |
expect(manager.children).to include(subordinate_1) | |
expect(manager.children).to include(subordinate_2) | |
end | |
it 'returns a list of everyone on the subtree' do | |
manager.subordinates << subordinate_1 | |
subordinate_1.subordinates << subordinate_2 | |
subordinate_2.subordinates << subordinate_3 | |
expect(manager.children).to include(subordinate_1) | |
expect(manager.children).to include(subordinate_2) | |
expect(manager.children).to include(subordinate_3) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment