Module: HammerCLI::Testing::CommandAssertions

Defined in:
lib/hammer_cli/testing/command_assertions.rb

Defined Under Namespace

Classes: CommandExpectation, CommandRunResult

Instance Method Summary collapse

Instance Method Details

#assert_cmd(expectation, actual_result) ⇒ Object



56
57
58
# File 'lib/hammer_cli/testing/command_assertions.rb', line 56

def assert_cmd(expectation, actual_result)
  expectation.assert_match(actual_result)
end

#assert_equal_or_match(expected, actual) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/hammer_cli/testing/command_assertions.rb', line 60

def assert_equal_or_match(expected, actual)
  case expected
  when String
    assert_equal(expected, actual)
  when MatcherBase
    expected.assert_match(actual)
  else
    msg = actual
    assert_match(expected, actual, msg)
  end
end

#assert_exit_code_equal(expected_code, actual_code) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/hammer_cli/testing/command_assertions.rb', line 48

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) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/hammer_cli/testing/command_assertions.rb', line 88

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) ⇒ Object



107
108
109
110
111
112
# File 'lib/hammer_cli/testing/command_assertions.rb', line 107

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_mapObject



39
40
41
42
43
44
45
46
# File 'lib/hammer_cli/testing/command_assertions.rb', line 39

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) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hammer_cli/testing/command_assertions.rb', line 31

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) ⇒ Object



114
115
116
# File 'lib/hammer_cli/testing/command_assertions.rb', line 114

def success_result(message)
  CommandExpectation.new(message)
end

#usage_error(command, message, heading = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/hammer_cli/testing/command_assertions.rb', line 72

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) ⇒ Object



100
101
102
103
104
105
# File 'lib/hammer_cli/testing/command_assertions.rb', line 100

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