module HammerCLI::Testing::CommandAssertions

Public Instance Methods

assert_cmd(expectation, actual_result) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 54
def assert_cmd(expectation, actual_result)
  expectation.assert_match(self, actual_result)
end
assert_equal_or_match(expected, actual) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 58
def assert_equal_or_match(expected, actual)
  if expected.is_a? String
    assert_equal(expected, actual)
  elsif expected.respond_to? :assert_match
    expected.assert_match(self, actual)
  else
    msg = actual
    assert_match(expected, actual, msg)
  end
end
assert_exit_code_equal(expected_code, actual_code) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 46
def assert_exit_code_equal(expected_code, actual_code)
  expected_info = "#{exit_code_map[expected_code]} (#{expected_code})"
  actual_info = "#{exit_code_map[actual_code]} (#{actual_code})"

  msg = "The exit code was expected to be #{expected_info}, but it was #{actual_info}"
  assert(expected_code == actual_code, msg)
end
common_error(command, message, heading=nil) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 85
def common_error(command, message, heading=nil)
  command = (['hammer'] + command).join(' ')
  if heading.nil?
    ["Error: #{message}",
     ""].join("\n")
  else
    ["#{heading}:",
     "  Error: #{message}",
     ""].join("\n")
  end
end
common_error_result(command, message, heading=nil) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 104
def common_error_result(command, message, heading=nil)
  expected_result = CommandExpectation.new
  expected_result.expected_err = common_error(command, message, heading)
  expected_result.expected_exit_code = HammerCLI::EX_SOFTWARE
  expected_result
end
exit_code_map() click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 37
def exit_code_map
  return @exit_code_map unless @exit_code_map.nil?

  hammer_exit_codes = HammerCLI.constants.select{|c| c.to_s.start_with?('EX_')}
  @exit_code_map = hammer_exit_codes.inject({}) do |code_map, code|
    code_map.update(HammerCLI.const_get(code) => code)
  end
end
run_cmd(options, context={}, cmd_class=HammerCLI::MainCommand) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 29
def run_cmd(options, context={}, cmd_class=HammerCLI::MainCommand)
  result = CommandRunResult.new
  result.out, result.err = capture_io do
    result.exit_code = cmd_class.run('hammer', options, context)
  end
  result
end
success_result(message) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 111
def success_result(message)
  CommandExpectation.new(message)
end
usage_error(command, message, heading=nil) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 69
def usage_error(command, message, heading=nil)
  command = (['hammer'] + command).join(' ')
  if heading.nil?
    ["Error: #{message}",
     "",
     "See: '#{command} --help'.",
     ""].join("\n")
  else
    ["#{heading}:",
     "  Error: #{message}",
     "  ",
     "  See: '#{command} --help'.",
     ""].join("\n")
  end
end
usage_error_result(command, message, heading=nil) click to toggle source
# File lib/hammer_cli/testing/command_assertions.rb, line 97
def usage_error_result(command, message, heading=nil)
  expected_result = CommandExpectation.new
  expected_result.expected_err = usage_error(command, message, heading)
  expected_result.expected_exit_code = HammerCLI::EX_USAGE
  expected_result
end