module RSpec::RubyContentMatchers class HaveBlock attr_reader :name, :args, :block_args def initialize(name, options={}) @name = name @args = options[:args] @block_args = options[:block_args] end def matches?(content) match_res = (content =~ /#{name}\s+#{args_expr}do\s+#{block_args_expr}(.*?)end/m) block_content = $1 || $3 if block_given? && block_content ruby_content = block_content.strip.extend(RSpec::RubyContent::Helpers) yield ruby_content else match_res end end def failure_message return "Expected there to be a block #{name} with arguments #{args}, but there wasn't" end def negative_failure_message return "Did not expect there to be a block #{name} with arguments #{args}, but there was" end protected def args_expr args ? '(\()?\s*' + args + '\s*(\))?\s+' : '' end def block_args_expr block_args ? '\|\s*' + block_args + '\s*\|' : '' end end def have_block(name, options={}) HaveBlock.new(name, options) end end