-
-
Save practicingruby/7753863 to your computer and use it in GitHub Desktop.
Tests for a game object in "Hunt The Wumpus". Please help improve its clarity.
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 "../helper" | |
describe "A room" do | |
let(:room) { Wumpus::Room.new(12) } | |
it "has a number" do | |
room.number.must_equal(12) | |
end | |
it "may contain hazards" do | |
# rooms start out safe | |
assert room.safe? | |
# hazards can be added | |
room.add(:wumpus) | |
room.add(:bats) | |
# a room with hazards isn't safe | |
refute room.safe? | |
# hazards can be detected by name | |
assert room.has?(:wumpus) | |
assert room.has?(:bats) | |
refute room.has?(:alf) | |
# hazards can be removed | |
room.remove(:bats) | |
refute room.has?(:bats) | |
end | |
describe "with neighbors" do | |
let(:exit_numbers) { [11, 3, 7] } | |
before do | |
exit_numbers.each { |i| room.connect(Wumpus::Room.new(i)) } | |
end | |
it "has two-way connections to neighbors" do | |
exit_numbers.each do |i| | |
# a neighbor can be looked up by room number | |
room.neighbor(i).number.must_equal(i) | |
# Room connections are bidirectional | |
room.neighbor(i).neighbor(room.number).must_equal(room) | |
end | |
end | |
it "knows the numbers of all neighboring rooms" do | |
room.exits.must_equal(exit_numbers) | |
end | |
it "can choose a neighbor randomly" do | |
exit_numbers.must_include(room.random_neighbor.number) | |
end | |
it "is not safe if its neighbors have hazards" do | |
room.random_neighbor.add(:wumpus) | |
refute room.safe? | |
end | |
it "is safe when it and its neighbors have no hazards" do | |
assert room.safe? | |
end | |
end | |
end |
Thanks guys, I revised based on the feedback. The only test I didn't modify much is the hazards test... yes, it's long and has many assertions... but it's testing a bunch of basic data manipulations and queries that go hand-in-hand with each other, so I can't see how to separate them without it seeming a bit too contrived.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@locks: Simply because
assert thing.has?(x)
is more expressive and at a higher level thanthing.has?(x).must_equal(true)
.