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



54
55
56
# 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) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Object



46
47
48
49
50
51
52
# 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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object



118
119
120
121
122
123
# File 'lib/hammer_cli/testing/command_assertions.rb', line 118

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



37
38
39
40
41
42
43
44
# 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

#missing_args_error(command, opts, heading = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hammer_cli/testing/command_assertions.rb', line 97

def missing_args_error(command, opts, heading = nil)
  opts = Array(opts).map { |o| "'#{o}'" }.join(', ')
  message = "  Missing arguments for #{opts}."
  if heading.nil?
    ["Could not #{command[-1]} the #{command[-2]}:",
     message,
     ''].join("\n")
  else
    ["#{heading}:",
     message,
     ''].join("\n")
  end
end

#missing_args_error_result(command, opts, heading = nil) ⇒ Object



132
133
134
135
136
137
# File 'lib/hammer_cli/testing/command_assertions.rb', line 132

def missing_args_error_result(command, opts, heading = nil)
  expected_result = CommandExpectation.new
  expected_result.expected_err = missing_args_error(command, opts, heading)
  expected_result.expected_exit_code = HammerCLI::EX_USAGE
  expected_result
end

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



125
126
127
128
129
130
# File 'lib/hammer_cli/testing/command_assertions.rb', line 125

def not_found_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_NOT_FOUND
  expected_result
end

#run_cmd(options, context = {}, cmd_class = HammerCLI::MainCommand) ⇒ Object



29
30
31
32
33
34
35
# 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) ⇒ Object



139
140
141
# File 'lib/hammer_cli/testing/command_assertions.rb', line 139

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

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# 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: '#{HammerCLI.expand_invocation_path(command)} --help'.",
     ""].join("\n")
  else
    ["#{heading}:",
     "  Error: #{message}",
     "  ",
     "  See: '#{HammerCLI.expand_invocation_path(command)} --help'.",
     ""].join("\n")
  end
end

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



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

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