Module: Amrita2::TestSupport

Included in:
Object
Defined in:
lib/amrita2/testsupport.rb

Overview

:nodoc: all

Instance Method Summary collapse

Instance Method Details

#assert_equal_as_xml(expected, actual) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/amrita2/testsupport.rb', line 51

def assert_equal_as_xml(expected, actual)
  #assert_equal(expected.as_normalized_xml, actual.as_normalized_xml)
  assert_equal(normalize(expected), normalize(actual))
rescue REXML::ParseException
  puts expected
  puts actual
  puts msg = diff_as_string(expected, actual)
  raise
rescue
  msg = diff_as_string(normalize(expected),normalize(actual))
  msg.gsub!('?', ' ')
  msg = build_message "not equal as xml ", msg
  raise Test::Unit::AssertionFailedError.new(msg)
end

#diff_as_string(data_old, data_new) ⇒ Object

copied from rspec spec/expectations/differs/default.rb



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/amrita2/testsupport.rb', line 67

def diff_as_string(data_old, data_new)
  context_lines = 3
  #format = :context
  format = :unified
  data_old = data_old.split(/\n/).map! { |e| e.chomp }
  data_new = data_new.split(/\n/).map! { |e| e.chomp }
  output = ""
  diffs = Diff::LCS.diff(data_old, data_new)
  return output if diffs.empty?
  oldhunk = hunk = nil  
  file_length_difference = 0
  diffs.each do |piece|
    begin
      hunk = Diff::LCS::Hunk.new(data_old, data_new, piece, context_lines,
                                 file_length_difference)
      file_length_difference = hunk.file_length_difference      
      next unless oldhunk      
      # Hunks may overlap, which is why we need to be careful when our
      # diff includes lines of context. Otherwise, we might print
      # redundant lines.
      if (context_lines > 0) and hunk.overlaps?(oldhunk)
        hunk.unshift(oldhunk)
      else
        output << oldhunk.diff(format)
      end
    ensure
      oldhunk = hunk
      output << "\n"
    end
  end  
  #Handle the last remaining hunk
  output << oldhunk.diff(format) << "\n"
end

#normalize(x) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/amrita2/testsupport.rb', line 101

def normalize(x)
  case x
  when String
    normalize(Hpricot.make(x))
  when Hpricot::Text
    x.to_s.strip.gsub(/\s+/, " ")
  when Hpricot::Elem
    if x.empty?
      x.stag.output("", :style => :empty)
    else
      [
        x.stag.output(""),
        normalize(x.children),
        x.etag ? x.etag.output("", :style => :end) : ''
      ].flatten.join("\n").strip
    end
  when Array
    x.collect { |e| normalize(e) }.join("\n").strip.gsub(/\>(\n\s)+/, ">\n")
  when Hpricot::BogusETag
    x.to_s
  else
    x.to_s
  end
end

#should_be_samexml_as(expected) ⇒ Object



126
127
128
# File 'lib/amrita2/testsupport.rb', line 126

def should_be_samexml_as(expected)
  normalize(self).should == normalize(expected)
end