Class: RSpec::Unit::Assertions::AssertionMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/unit/assertions.rb

Overview

:stopdoc:

Defined Under Namespace

Classes: Literal, Template

Constant Summary

MICRONAUTUNIT_FILE_SEPARATORS =
%r{[\\/:]}
MICRONAUTUNIT_PREFIX =
__FILE__.split(MICRONAUTUNIT_FILE_SEPARATORS)[0..-3]
MICRONAUTUNIT_RB_FILE =
/\.rb\Z/

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(head, template_string, parameters) ⇒ AssertionMessage

Returns a new instance of AssertionMessage



561
562
563
564
565
# File 'lib/rspec/unit/assertions.rb', line 561

def initialize(head, template_string, parameters)
  @head = head
  @template_string = template_string
  @parameters = parameters
end

Class Attribute Details

.use_ppObject

Returns the value of attribute use_pp



524
525
526
# File 'lib/rspec/unit/assertions.rb', line 524

def use_pp
  @use_pp
end

Class Method Details

.literal(value) ⇒ Object



557
558
559
# File 'lib/rspec/unit/assertions.rb', line 557

def self.literal(value)
  Literal.new(value)
end

Instance Method Details

#add_period(string) ⇒ Object



596
597
598
# File 'lib/rspec/unit/assertions.rb', line 596

def add_period(string)
  (string =~ /\.\Z/ ? string : string + '.')
end

#convert(object) ⇒ Object



567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/rspec/unit/assertions.rb', line 567

def convert(object)
  case object
    when Exception
      <<EOM.chop
Class: <#{convert(object.class)}>
Message: <#{convert(object.message)}>
---Backtrace---
#{filter_backtrace(object.backtrace).join("\n")}
---------------
EOM
    else
      if(self.class.use_pp)
        begin
          require 'pp'
        rescue LoadError
          self.class.use_pp = false
          return object.inspect
        end unless(defined?(PP))
        PP.pp(object, '').chomp
      else
        object.inspect
      end
  end
end

#filter_backtrace(backtrace, prefix = nil) ⇒ Object



617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/rspec/unit/assertions.rb', line 617

def filter_backtrace(backtrace, prefix=nil)
  return ["No backtrace"] unless(backtrace)
  split_p = if(prefix)
    prefix.split(MICRONAUTUNIT_FILE_SEPARATORS)
  else
    MICRONAUTUNIT_PREFIX
  end
  match = proc do |e|
    split_e = e.split(MICRONAUTUNIT_FILE_SEPARATORS)[0, split_p.size]
    next false unless(split_e[0..-2] == split_p[0..-2])
    split_e[-1].sub(MICRONAUTUNIT_RB_FILE, '') == split_p[-1]
  end
  return backtrace unless(backtrace.detect(&match))
  found_prefix = false
  new_backtrace = backtrace.reverse.reject do |e|
    if(match[e])
      found_prefix = true
      true
    elsif(found_prefix)
      false
    else
      true
    end
  end.reverse
  new_backtrace = (new_backtrace.empty? ? backtrace : new_backtrace)
  new_backtrace = new_backtrace.reject(&match)
  new_backtrace.empty? ? backtrace : new_backtrace
end

#templateObject



592
593
594
# File 'lib/rspec/unit/assertions.rb', line 592

def template
  @template ||= Template.create(@template_string)
end

#to_sObject



600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/rspec/unit/assertions.rb', line 600

def to_s
  message_parts = []
  if (@head)
    head = @head.to_s 
    unless(head.empty?)
      message_parts << add_period(head)
    end
  end
  tail = template.result(@parameters.collect{|e| convert(e)})
  message_parts << tail unless(tail.empty?)
  message_parts.join("\n")
end