Sha256: 9c610c8406d844ac00bb08e41b4db1d49e87fcf4d3abee0f8789e3b8f536d2a0

Contents?: true

Size: 1.32 KB

Versions: 2

Compression:

Stored size: 1.32 KB

Contents

module StructuredWarnings
  module Test
    # This warner is used in Assertions#assert_warn and 
    # Assertions#assert_no_warn blocks. It captures all warnings in format and 
    # provides access to them using the warned? method.
    class Warner < StructuredWarnings::Warner
      # Overrides the public interface of StructuredWarnings::Warner. This
      # method always returns nil to avoid warnings on stdout during assert_warn
      # and assert_no_warn blocks.
      def format(warning, message, call_stack)
        given_warnings << warning.new(message)
        nil
      end

      # Returns true if any warning or a subclass of warning was emitted.
      def warned?(warning, message = nil)
        case message
        when Regexp
          given_warnings.any? {|w| w.is_a?(warning) && w.message =~ message}
        when String
          given_warnings.any? {|w| w.is_a?(warning) && w.message == message}
        when nil
          given_warnings.any? {|w| w.is_a?(warning)}
        else
          raise ArgumentError, "Unkown argument for 'message'"
        end
      end

      # :stopdoc:
    protected
      # Returns an array of all warning classes, that were given to this
      # warner's format method, including duplications.
      def given_warnings
        @given_warnings ||= []
      end
      # :startdoc:
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
structured_warnings-0.1.3 lib/structured_warnings/test/warner.rb
structured_warnings-0.1.2 lib/structured_warnings/test/warner.rb