Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
tree: 82da02f4b4
Fetching contributors…

Cannot retrieve contributors at this time

340 lines (270 sloc) 8.991 kB
require_relative "oo_sixdegrees"
describe Person do
let(:bob) { Person.new(:bob) }
let(:ann) { Person.new('ann') }
let(:carl) { Person.new(:carl) }
let(:dan) { Person.new('dan') }
let(:eva) { Person.new(:eva) }
let(:frank) { Person.new('frank') }
it "is comparable by name with someone else" do
bob.should be > ann
bob.should_not == ann
bob.should_not be < ann
end
it "can talk to another person" do
bob.talk_to ann
bob.talking_to.should include(ann)
end
it "can't talk to oneself" do
bob.talk_to carl
bob.talk_to bob
bob.talking_to.should include(carl)
bob.talking_to.should_not include(bob)
end
it "remembers the people talking to oneself" do
bob.talk_to ann
ann.talked_by.should include(bob)
end
it "can tell who it is by identity or name" do
ann.is?(ann).should be_true
ann.is?(:ann).should be_true
ann.is?("ann").should be_true
ann.is?(bob).should be_false
ann.is?(:bob).should be_false
ann.is?("bob").should be_false
end
it "is connected to another person when they talk each other" do
bob.talk_to dan
dan.talk_to bob
bob.should be_connected_to(dan)
dan.should be_connected_to(bob)
bob.connected.should include(dan)
dan.connected.should include(bob)
end
it "can be directly connected to another person" do
bob.connect_to ann
bob.connected.should include(ann)
bob.connected.should_not include(carl)
end
it "can check if is connected to another person" do
bob.connect_to ann
eva.talk_to frank
frank.talk_to eva
dan.talk_to eva
bob.should be_connected_to(ann)
bob.should_not be_connected_to(carl)
eva.should be_connected_to(frank)
frank.should be_connected_to(eva)
eva.should_not be_connected_to(dan)
dan.should_not be_connected_to(eva)
end
it "creates a two-way connection when connected to another person" do
bob.connect_to ann
bob.should be_connected_to(ann)
ann.should be_connected_to(bob)
end
it "can't be connected to oneself" do
ann.connect_to(ann)
ann.connect_to(carl)
ann.connected.should include(carl)
ann.connected.should_not include(ann)
end
describe "#talking_to" do
it "doesn't contain duplicates" do
ann.talk_to bob
ann.talk_to bob
ann.talking_to.should == SortedSet.new([bob])
end
end
describe "#talked_by" do
it "doesn't contain duplicates" do
ann.talk_to bob
ann.talk_to bob
bob.talked_by.should == SortedSet.new([ann])
end
end
describe "#to_sym" do
it "returns the name" do
ann.to_sym.should == ann.name
bob.to_sym.should == bob.name
end
end
describe "#hash" do
let(:ann_1) { Person.new("ann") }
let(:ann_2) { Person.new(:ann) }
it "returns the hash of the name" do
ann.hash.should == ann.name.hash
ann.hash.should == :ann.hash
end
it "allows to refer to a single person in a hash depending on its name" do
hash = {}
hash[ann_1] = 1
hash[ann_2] = 2
hash.should == {Person.new(ann) => 2}
hash.should == {ann => 2}
end
it "allows to refer to a single person in a set or hash depending on its name" do
set = SortedSet.new
set << ann_1 << ann_2 << ann
set.size.should be(1)
set.should == SortedSet.new([Person.new(:ann)])
end
end
describe "#to_s" do
it "returns the name as a string" do
ann.to_s.should == ann.name.to_s
end
end
describe "#network_levels" do
it "returns an array of arrays of connected people at different degrees, without duplicates" do
ann.connect_to carl
ann.connect_to bob
bob.connect_to carl
bob.connect_to dan
bob.connect_to ann
ann.network_levels.should == [[ann], [bob, carl], [dan]]
ann.network_levels_names.should == [[:ann], [:bob, :carl], [:dan]]
end
end
end
describe SocialNetwork do
let(:bob) { Person.new(:bob) }
let(:ann) { Person.new('ann') }
let(:carl) { Person.new(:carl) }
let(:dan) { Person.new('dan') }
let(:eva) { Person.new(:eva) }
let(:frank) { Person.new('frank') }
let(:social_network) { SocialNetwork.new }
describe "#add" do
it "adds a person only if no one exists with that name" do
people = SortedSet.new
social_network = SocialNetwork.new(people)
people.should_receive(:include?).with(ann) { false }
people.should_receive(:<<).with(ann) { SortedSet.new([ann]) }
people.should_receive(:include?).with(ann) { true }
social_network.add ann
social_network.add Person.new("ann")
end
end
describe "#[]" do
it "can find the person directly or by name" do
social_network << ann << bob << carl
social_network['ann'].should == ann
social_network[:ann].should == ann
social_network[ann].should == ann
end
it "it returns nil if the person is not in the network" do
social_network << ann << bob << carl
social_network['dan'].should == nil
social_network[:dan].should == nil
social_network[dan].should == nil
end
end
describe "#people" do
it "returns a sorted set of people" do
social_network << carl
social_network << ann
social_network << bob
social_network.people.should == SortedSet.new([ann, bob, carl])
end
end
describe "#active_people" do
it "returns a sorted set of people who are talking to someone" do
social_network << carl
social_network << ann
social_network << bob
carl.talk_to ann
ann.talk_to bob
social_network.active_people.should == SortedSet.new([carl, ann])
social_network.active_people.should_not include(bob)
end
end
describe "#connect" do
it "can connect two persons" do
social_network.connect(bob, ann).should ==(social_network)
social_network.people.should == SortedSet.new([bob, ann])
end
it "adds the two mentioned persons" do
social_network.connect(ann, carl)
social_network['ann'].should == ann
social_network[:carl].should == carl
end
it "connects the two mentioned persons" do
social_network.connect(ann, carl)
ann.should be_connected_to(carl)
end
end
describe "#talk" do
it "lets two persons talk" do
social_network.talk dan, eva
eva.should be_talked_by(dan)
dan.should be_talking_to(eva)
end
end
describe "#talking" do
let(:ann) { double("Person") }
let(:bob) { double("Person") }
let(:carl) { double("Person") }
let(:dan) { double("Person") }
let(:eva) { double("Person") }
let(:frank) { double("Person") }
before do
ann.should_receive(:talk_to).with(bob)
ann.should_receive(:talk_to).with(carl)
ann.should_receive(:talk_to).with(frank)
bob.should_receive(:talk_to).with(dan)
bob.should_receive(:talk_to).with(eva)
end
it "receives a hash of senders talking to recipients" do
social_network.talking ann => [carl, frank, bob],
bob => [dan, eva]
end
it "receives an associative array of senders talking to recipients" do
bob.should_receive(:to_a).and_return([bob])
dan.should_receive(:to_a).and_return([dan])
social_network.talking [
[ann, bob ],
[ann, [carl, frank]],
[bob, dan ],
[bob, [eva] ]
]
end
end
end
describe Message do
let(:message_text) { "mariana: this makes botany the most alluring of studies, and wins it from the farmer and the herb-woman :D /cc @terrell_senger, @florence_vonrueden" }
let(:message) { Message.new(message_text) }
it "extracts the name of the sender" do
message.sender_name.should == "mariana"
end
it "extracts the names of the recipients" do
message.recipients_names.should == ["florence_vonrueden", "terrell_senger"]
end
end
describe SocialNetworkReporter do
let(:social_network) { SocialNetwork.new }
let(:input_filepath) { "#{File.expand_path("..", __FILE__)}/complex_input.txt" }
let(:output_filepath) { "#{File.expand_path("..", __FILE__)}/complex_output_test.txt" }
let(:reference_filepath) { "#{File.expand_path("..", __FILE__)}/complex_output_ok.txt" }
let(:output_file) { File.open(output_filepath, "w+") }
let(:reference_file) { File.open(reference_filepath, "r") }
subject do
SocialNetworkReporter.new(
:input_filepath => input_filepath,
:output_filepath => output_filepath,
:social_network => social_network
)
end
after do
output_file.close
reference_file.close
end
it "produces a correct output file with connected people for every person" do
subject.scan.report
output_file.rewind
reference_file.rewind
output_file.each_line do |line|
line.should == reference_file.next
end
end
end
Jump to Line
Something went wrong with that request. Please try again.