Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #160 from pvande/assertions/assert_output

Factoring out custom assertions, and adding assert_output.
  • Loading branch information...
commit a10de183d03bab45978a001574c9c04a44260673 2 parents cf36e50 + 5f4b4c3
@justinstoller justinstoller authored
Showing with 63 additions and 1 deletion.
  1. +61 −0 lib/assertions.rb
  2. +2 −1  lib/test_case.rb
View
61 lib/assertions.rb
@@ -0,0 +1,61 @@
+module Assertions
+ include Test::Unit::Assertions
+
+ # Make assertions about the content of console output.
+ #
+ # By default, each line of +output+ is assumed to come from STDOUT. You may
+ # specify the stream explicitly by annotating the line with a stream marker.
+ # (If your line literally requires any stream marker at the beginning of a
+ # line, you must prefix the line with an explicit stream marker.) The
+ # currently recognized markers are:
+ #
+ # * "STDOUT> "
+ # * "STDERR> "
+ # * "OUT> "
+ # * "ERR> "
+ # * "1> "
+ # * "2> "
+ #
+ # Any leading common indentation is automatically removed from the +output+
+ # parameter. For cases where this matters (e.g. every line should be
+ # indented), you should prefix the line with an explicit stream marker.
+ #
+ # @example Assert order of interleaved output streams
+ # !!!plain
+ # assert_output <<-CONSOLE
+ # STDOUT> 0123456789
+ # STDERR> ^- This is left aligned
+ # STDOUT> 01234567890
+ # STDERR> ^- This is indented 2 characters.
+ # CONSOLE
+ #
+ # @example Assert all content went to STDOUT
+ # !!!plain
+ # assert_output <<-CONSOLE
+ # 0123456789
+ # ^- This is left aligned
+ # 01234567890
+ # ^- This is indented 2 characters.
+ # CONSOLE
+ #
+ # @param [String] output The expected console output, optionally annotated
+ # with stream markers.
+ # @param [String] msg An explanatory message about why the test failure is
+ # relevant.
+ def assert_output(output, msg='Output lines did not match')
+ # Remove the minimal consistent indentation from the input; useful for clean HEREDOCs.
+ indentation = output.lines.map { |line| line[/^ */].length }.min
+ output = output.gsub(/^ {#{indentation}}/, '')
+
+ # Divide output based on expected destination
+ out, err = output.lines.partition { |line| line !~ /^((STD)?ERR|2)> / }
+ out, err, output = [out.join, err.join, output].map do |str|
+ str.gsub(/^((STD)?(ERR|OUT)|[12])> /, '')
+ end
+
+ # Exercise assertions about output
+ assert_equal output, (result.nil? ? '' : result.output), msg
+ assert_equal out, (result.nil? ? '' : result.stdout), 'The contents of STDOUT did not match expectations'
+ assert_equal err, (result.nil? ? '' : result.stderr), 'The contents of STDERR did not match expectations'
+ end
+end
View
3  lib/test_case.rb
@@ -3,9 +3,10 @@ class TestCase
require 'tempfile'
require 'benchmark'
require 'stringio'
+ require 'lib/assertions'
require 'lib/puppet_commands'
- include Test::Unit::Assertions
+ include Assertions
include PuppetCommands
class PendingTest < Exception; end
Please sign in to comment.
Something went wrong with that request. Please try again.