Module: Methadone::IntegrationTestAssertions
- Included in:
- BaseIntegrationTest
- Defined in:
- lib/methadone/test/integration_test_assertions.rb
Instance Method Summary collapse
-
#assert_banner(stdout, bin_name, takes_options:, takes_arguments: {}) ⇒ Object
Assert that the stdout contains an appropriate banner for your app.
-
#assert_file(filename, contains:) ⇒ Object
Assert that a file’s contents contains one or more regexps.
-
#assert_oneline_summary(stdout) ⇒ Object
- Assert that your app has a one-line summary stdout
-
The standard out, presumably of running ‘«your-app» –help`.
-
#assert_option(stdout, *options) ⇒ Object
Assert that your app takes the given option(s).
Instance Method Details
#assert_banner(stdout, bin_name, takes_options:, takes_arguments: {}) ⇒ Object
Assert that the stdout contains an appropriate banner for your app
- stdout
-
The standard out, presumably of running ‘«your-app» –help`
- bin_name
-
The binary name of your app
- takes_options
-
set this to true if your app should take options
- takes_arguments
-
set this to a hash of the arguments your app should take, with the key being the arg name and the value
being either ‘:required` or `:optional`
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/methadone/test/integration_test_assertions.rb', line 20 def (stdout, bin_name, takes_options: , takes_arguments: {}) if assert_match(/Options/, stdout) if takes_arguments == false || takes_arguments.empty? assert_match(/Usage: #{Regexp.escape(bin_name)}.*\[options\]\s*$/,stdout) else expected_args = takes_arguments.map { |arg, required| if required == :required arg.to_s else "[#{arg}]" end }.join(" ") assert_match(/Usage: #{Regexp.escape(bin_name)}.*\[options\]\s*#{Regexp.escape(expected_args)}$/,stdout) end else assert_match(/Usage: #{Regexp.escape(bin_name)}\s*$/,stdout) end end |
#assert_file(filename, contains:) ⇒ Object
Assert that a file’s contents contains one or more regexps
- filename
-
The file whose contents to check
- contains
-
either a regexp or array of regexpts that the file’s contents must match
6 7 8 9 10 11 |
# File 'lib/methadone/test/integration_test_assertions.rb', line 6 def assert_file(filename, contains:) contents = File.read(filename) Array(contains).each do |regexp| assert_match(regexp,contents,"Expected #{filename} to contain #{regexp}") end end |
#assert_oneline_summary(stdout) ⇒ Object
Assert that your app has a one-line summary
- stdout
-
The standard out, presumably of running ‘«your-app» –help`
57 58 59 60 61 62 |
# File 'lib/methadone/test/integration_test_assertions.rb', line 57 def assert_oneline_summary(stdout) output = stdout.split(/\n/) assert output.size >= 3, "Expected 3 or more lines:\n#{stdout}" assert_match(/^\s*$/,output[1],"Should be a blank line after the banner") assert_match(/^\w+\s+\w+/,output[2],"Should be at least two words describing your app") end |
#assert_option(stdout, *options) ⇒ Object
Assert that your app takes the given option(s)
- stdout
-
The standard out, presumably of running ‘«your-app» –help`
- options
-
options your app should take. Put the literal value in here e.g. ‘–foo` or `–[no-]bar`. The array form is to
allow you to assert long and short options for readable tests:
assert_option(stdout, "--version")
assert_option(stdout, "-h", "--help")
49 50 51 52 53 |
# File 'lib/methadone/test/integration_test_assertions.rb', line 49 def assert_option(stdout, *) .each do |option| assert_match(/#{Regexp.escape(option)}/,stdout) end end |