Class: RSpec::RubyContentMatchers::HaveBlock

Inherits:
RSpec::RubyContentMatcher show all
Defined in:
lib/code_spec/matchers/have_block.rb

Defined Under Namespace

Modules: BlockArgs

Constant Summary

Constants inherited from RSpec::RubyContentMatcher

RSpec::RubyContentMatcher::ANY_GROUP, RSpec::RubyContentMatcher::LPAR, RSpec::RubyContentMatcher::OPT_ARGS, RSpec::RubyContentMatcher::OPT_SPACES, RSpec::RubyContentMatcher::Q_ANY_GROUP, RSpec::RubyContentMatcher::RPAR, RSpec::RubyContentMatcher::SPACES

Instance Attribute Summary collapse

Attributes inherited from RSpec::RubyContentMatcher

#content, #content_matches

Instance Method Summary collapse

Methods inherited from RSpec::RubyContentMatcher

#any_args_expr, #args_msg, #comment_end, #debug, #debug?, #debug_content, #end_expr, #get_expr, #index, #is_match?, #opt

Constructor Details

#initialize(name, options = {}) ⇒ HaveBlock

Returns a new instance of HaveBlock.



5
6
7
8
9
10
11
# File 'lib/code_spec/matchers/have_block.rb', line 5

def initialize(name, options={})
  super options[:end] || name      
  @name = name.to_s
  @args = options[:args] 
  @do_option = options[:do].nil? ? true : options[:do]      
  @block_args = options[:block_args]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def args
  @args
end

#block_argsObject (readonly)

Returns the value of attribute block_args.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def block_args
  @block_args
end

#block_argumentsObject (readonly)

Returns the value of attribute block_arguments.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def block_arguments
  @block_arguments
end

#do_optionObject (readonly)

Returns the value of attribute do_option.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def do_option
  @do_option
end

#end_optionObject (readonly)

Returns the value of attribute end_option.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def end_option
  @end_option
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/code_spec/matchers/have_block.rb', line 3

def name
  @name
end

Instance Method Details

#failure_messageObject



46
47
48
49
# File 'lib/code_spec/matchers/have_block.rb', line 46

def failure_message   
  super
  return "Expected there to be a block #{name}#{args_msg}#{block_args_msg}, but there wasn't"
end

#handle_result(content, match, &block) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/code_spec/matchers/have_block.rb', line 35

def handle_result content, match, &block 
  if block && match && content
    ruby_content = content.strip  
    ruby_content.extend(BlockArgs)
    ruby_content.block_args = block_arguments
    yield ruby_content        
  end
  match
end

#matches?(content, &block) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/code_spec/matchers/have_block.rb', line 21

def matches? content, &block
  @content = content
  blockargs = content.scan(/do\s+\|(.*?)\|/).flatten[0]
  @block_arguments = blockargs.split ',' if blockargs
  
  match = is_match? content                                                        
  content_to_yield = if indexes
    content_matches[indexes.first] || content_matches[indexes.last]
  else
    content_matches[index]
  end          
  handle_result(content_to_yield, match, &block)
end

#negative_failure_messageObject



51
52
53
54
# File 'lib/code_spec/matchers/have_block.rb', line 51

def negative_failure_message  
  super
  return "Did not expect there to be a block #{name}#{args_msg}, but there was"
end