Skip to content
basicxman edited this page Aug 31, 2011 · 2 revisions

Overview

  • Tests are very important throughout development and pull requests without accompanying tests will be closed without merging.
  • Try to write tests before writing the functionality.
  • Currently the server is generally mocked by using an instance of the Minecraft::Extensions class with a StringIO instance, like such: @ext = Minecraft::Extensions.new(StringIO.new, {})

Write tests under the test/ directory.

# Help command.
sandbox_test "should display help contents for regular users" do
  @ext = Minecraft::Extensions.new(StringIO.new, {})
  @ext.users = ["blizzard4U"]
  @ext.call_command("blizzard4U", "help")
  assert_match "rules", @ext.server.string
  assert_match "list", @ext.server.string
  refute_match "give", @ext.server.string
end

sandbox_test "should display help contents for half ops" do
  @ext = Minecraft::Extensions.new(StringIO.new, {})
  @ext.users = ["mike_n_7"]
  @ext.hops  = ["mike_n_7"]
  @ext.call_command("mike_n_7", "help")
  assert_match "rules", @ext.server.string
  assert_match "give", @ext.server.string
  refute_match "morning", @ext.server.string
end

sandbox_test "should display help contents for ops" do
  @ext = Minecraft::Extensions.new(StringIO.new, {})
  @ext.users = ["basicxman"]
  @ext.ops   = ["basicxman"]
  @ext.call_command("basicxman", "help")
  assert_match "rules", @ext.server.string
  assert_match "give", @ext.server.string
  assert_match "morning", @ext.server.string
end

Run rake and watch the tests fail, I recommend the use of guard for continuous testing.

CommandsTest:
     PASS test_should_print_a_leaderboard (0.00s) 
     ...
     FAIL test_should_display_help_contents_for_regular_users (0.00s) 
          Expected /rules/ to match "say !tp target_user\nsay !kit kit_name\nsay !give item quantity\nsay !nom\nsay !list\nsay !addtimer item frequency\nsay !deltimer item\n".
          /Users/andrew/Dropbox/Development/minecraft/test/commands_test.rb:206:in `block in <class:CommandsTest>'

     FAIL test_should_display_help_contents_for_ops (0.00s) 
          Expected /rules/ to match "say !tp target_user\nsay !kit kit_name\nsay !give item quantity\nsay !nom\nsay !list\nsay !addtimer item frequency\nsay !deltimer item\n".
          /Users/andrew/Dropbox/Development/minecraft/test/commands_test.rb:226:in `block in <class:CommandsTest>'

     PASS test_should_initiate_a_kickvote_against_a_user (0.00s) 
     FAIL test_should_display_help_contents_for_half_ops (0.00s) 
          Expected /rules/ to match "say !tp target_user\nsay !kit kit_name\nsay !give item quantity\nsay !nom\nsay !list\nsay !addtimer item frequency\nsay !deltimer item\n".
          /Users/andrew/Dropbox/Development/minecraft/test/commands_test.rb:216:in `block in <class:CommandsTest>'

Check why the tests have failed, start writing code to fix.

# Prints the available commands for the user.
#
# @param [String] user The requesting user.
# @example
#   help("basicxman")
def help(user)
  commands = @commands.keys.inject([]) { |arr, key|
    priv = @commands[key][:ops]
    if is_op? user
      arr << key
    elsif is_hop? user
      priv == :op ? arr : arr << key
    else
      priv == :none ? arr << key : arr
    end
  }.map { |s| "!" + s.to_s }
  @server.puts "say Commands: #{commands.join(", ")}"
end

Check your tests!

CommandsTest:
     PASS test_should_expire_a_kickvote_against_a_user (0.00s) 
     ...
     PASS test_should_display_help_contents_for_half_ops (0.00s) 
     PASS test_should_display_help_contents_for_ops (0.00s) 
     PASS test_should_display_help_contents_for_regular_users (0.00s) 

Feature is implemented, tests are written, no server was interrupted, functionality is documented.