class Spec::Matchers::Be

:nodoc:

def args_to_s

def args_to_s
  @args.empty? ? "" : parenthesize(inspected_args.join(', '))
end

def args_to_sentence

def args_to_sentence
  to_sentence(@args)
end

def compare_to(expected, opts)

def compare_to(expected, opts)
  @expected, @comparison_method = expected, opts[:using]
end

def comparison

def comparison
  @comparison_method.nil? ? " " : "be #{@comparison_method.to_s} "
end

def comparison_method

def comparison_method
  @comparison_method || :equal?
end

def description

def description
  "#{prefix_to_sentence}#{comparison} #{expected_to_sentence}#{args_to_sentence}".gsub(/\s+/,' ')
end

def expected

def expected
  @expected
end

def expected_to_sentence

def expected_to_sentence
  split_words(expected)
end

def failure_message_for_should

def failure_message_for_should
  handling_predicate? ?
    "expected #{predicate}#{args_to_s} to return true, got #{@result.inspect}" :
    "expected #{@comparison_method} #{expected}, got #{@actual.inspect}".gsub('  ',' ')
end

def failure_message_for_should_not

def failure_message_for_should_not
  if handling_predicate?
    "expected #{predicate}#{args_to_s} to return false, got #{@result.inspect}"
  else
    message = <<-MESSAGE
d_not be #{@comparison_method} #{expected}' not only FAILED,
a bit confusing.
    MESSAGE
    
    raise message << ([:===,:==].include?(@comparison_method) ?
      "It might be more clearly expressed without the \"be\"?" :
      "It might be more clearly expressed in the positive?")
  end
end

def handling_predicate!

def handling_predicate!
  @handling_predicate = true
end

def handling_predicate?

def handling_predicate?
  return false if [true, false, nil].include?(expected)
  # FIXME - this is a bit goofy - but we get failures
  # if just defining @handling_predicate = nil or false in initialize
  return defined?(@handling_predicate) ? @handling_predicate : nil
end

def initialize(*args)

def initialize(*args)
  @expected = args.empty? ? true : set_expected(args.shift)
  @args = args
  @comparison_method = nil
end

def inspected_args

def inspected_args
  @args.collect{|a| a.inspect}
end

def match_or_compare(actual)

def match_or_compare(actual)
  TrueClass === @expected ? @actual : @actual.__send__(comparison_method, @expected)
end

def matches?(actual)

def matches?(actual)
  @actual = actual
  handling_predicate? ? run_predicate_on(actual) : match_or_compare(actual)
end

def parenthesize(string)

def parenthesize(string)
  return "(#{string})"
end

def parse_expected(expected)

def parse_expected(expected)
  ["be_an_","be_a_","be_"].each do |prefix|
    handling_predicate!
    if expected.to_s =~ /^#{prefix}/
      set_prefix(prefix)
      expected = expected.to_s.sub(prefix,"")
      [true, false, nil].each do |val|
        return val if val.to_s == expected
      end
      return expected.to_sym
    end
  end
end

def predicate

def predicate
  "#{@expected.to_s}?".to_sym
end

def prefix

def prefix
  # FIXME - this is a bit goofy - but we get failures
  # if just defining @prefix = nil in initialize
  @prefix = nil unless defined?(@prefix)
  @prefix
end

def prefix_to_sentence

def prefix_to_sentence
  split_words(prefix)
end

def present_tense_predicate

def present_tense_predicate
  "#{@expected.to_s}s?".to_sym
end

def run_predicate_on(actual)

def run_predicate_on(actual)
  begin
    return @result = actual.__send__(predicate, *@args)
  rescue NameError => predicate_missing_error
    "this needs to be here or rcov will not count this branch even though it's executed in a code example"
  end
  begin
    return @result = actual.__send__(present_tense_predicate, *@args)
  rescue NameError
    raise predicate_missing_error
  end
end

def set_expected(expected)

def set_expected(expected)
  Symbol === expected ? parse_expected(expected) : expected
end

def set_prefix(prefix)

def set_prefix(prefix)
  @prefix = prefix
end