Skip to content

Commit

Permalink
Write tests over WorldCommand (unfinished)
Browse files Browse the repository at this point in the history
  • Loading branch information
nskins committed Apr 23, 2017
1 parent 73c8d4b commit cc46065
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/world_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ module WorldCommand
# String literal for the special commands header.
SPECIAL_COMMANDS_HEADER = "* Special commands: "

NO_ITEM_DROP_ERROR = "You can't drop what you don't have!\n\n"

# Prints the commands that are available everywhere.
def display_default_commands
print DEFAULT_COMMANDS
Expand Down Expand Up @@ -94,7 +96,7 @@ def interpret_command(command, player)
player.remove_item(item, 1)
print "You have dropped #{item}.\n\n"
else
print "You can't drop what you don't have!\n\n"
print NO_ITEM_DROP_ERROR
end
return
elsif words[0].casecmp("equip").zero?
Expand Down
78 changes: 67 additions & 11 deletions spec/world_command_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
@player = Player.new(max_hp: 10,
hp: 3,
inventory: [ Couple.new(Food.new(name: "Banana", recovers: 5), 1),
Couple.new(Food.new(name: "Onion", disposable: false), 1) ],
Couple.new(Food.new(name: "Onion", disposable: false), 1),
Couple.new(Item.new(name: "Big Book of Stuff"), 1),
Couple.new(Helmet.new, 1) ],
map: @map,
location: Couple.new(0, 0))
end
Expand Down Expand Up @@ -83,7 +85,7 @@
# TODO: test the input of all possible commands.
# TODO: test use/drop/equip/unequip multi-word items.
context "interpret command" do

context "lowercase" do
it "should correctly move the player around" do
interpret_command("s", @player)
Expand All @@ -101,28 +103,82 @@
WorldCommand::DEFAULT_COMMANDS).to_stdout
end

it "should use the specified item" do
interpret_command("use banana", @player)
expect(@player.has_item("Banana")).to be_nil
expect(@player.hp).to eq 8
it "should print the map" do
interpret_command("map", @player)
# TODO: expect the map output.
end

it "should print error text for using nonexistent item" do
expect { interpret_command("use apple", @player) }.to output(
Entity::NO_SUCH_ITEM_ERROR).to_stdout
it "should print the inventory" do
interpret_command("inv", @player)
# TODO: expect the inventory output.
end

it "should print the status" do
interpret_command("status", @player)
# TODO: expect the status output.
end

it "should save the game" do
# Rename the original file.
random_string = "n483oR38Avdis3"
File.rename("player.yaml", random_string) if File.exists?("player.yaml")

interpret_command("save", @player)
expect(File.exists?("player.yaml")).to be true
File.delete("player.yaml")

# Return the original data to the file.
File.rename(random_string, "player.yaml") if File.exists?(random_string)
end

it "should drop a disposable item" do
interpret_command("drop banana", @player)
expect(@player.has_item("Banana")).to be_nil
end

it "should drop the item composed of multiple words" do
interpret_command("drop big book of stuff", @player)
expect(@player.has_item("Big Book of Stuff")).to be_nil
end

it "should not drop a non-disposable item" do
interpret_command("drop onion", @player)
expect(@player.has_item("Onion")).to eq 1
end

it "should print error text for dropping nonexistent item" do
expect { interpret_command("drop orange", @player) }.to output(
WorldCommand::NO_ITEM_DROP_ERROR).to_stdout
end

it "should equip and unequip the specified item" do
interpret_command("equip helmet", @player)
expect(@player.has_item("Helmet")).to be_nil
expect(@player.outfit[:helmet]).to eq Helmet.new
interpret_command("unequip helmet", @player)
expect(@player.has_item("Helmet")).not_to be_nil
expect(@player.outfit[:helmet]).to be_nil
end

it "should use the specified item" do
interpret_command("use banana", @player)
expect(@player.has_item("Banana")).to be_nil
expect(@player.hp).to eq 8
end

it "should print error text for using nonexistent item" do
expect { interpret_command("use apple", @player) }.to output(
Entity::NO_SUCH_ITEM_ERROR).to_stdout
end

it "should run the event on the tile" do
@player.move_right
expect { interpret_command("talk\n", @player) }.to output(
"NPC: Hello!\n\n").to_stdout
end

end

context "case-insensitive" do
it "should correctly move the player around" do
interpret_command("S", @player)
Expand All @@ -135,7 +191,7 @@
expect(@player.location).to eq Couple.new(0, 0)
end
end

end

end

0 comments on commit cc46065

Please sign in to comment.