fizx-assertion_fu 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,19 +1,20 @@
1
- # To gain access to the assertions, you can let your tests extend
2
- # AssertionFu::AbstractTest, or you can include AssertionFu in your testing
3
- # class.
1
+ # To gain access to the assertions, include AssertionFu in your testing
2
+ # classes. I often find it helpful to subclass an AbstractTest class,
3
+ # like so:
4
+ #
5
+ # class AbstractTest < Test::Unit::TestCase
6
+ # include AssertionFu
7
+ #
8
+ # def test_assertion_fu_included #:nodoc:
9
+ # assert true
10
+ # end
11
+ # end
12
+ #
13
+ # class MyTest < AbstractTest
14
+ # # tests go here ...
15
+ # end
4
16
  module AssertionFu
5
-
6
- # # To gain access to the assertions, you can let your tests extend
7
- # # AssertionFu::AbstractTest, or you can include AssertionFu in your testing
8
- # # class.
9
- # class AbstractTest < Test::Unit::TestCase
10
- # include AssertionFu
11
- #
12
- #
13
- # def test_assertion_fu_included #:nodoc:
14
- # assert true
15
- # end
16
- # end
17
+ class Error < RuntimeError; end
17
18
 
18
19
  # The contents of the file at the path should match the regex or
19
20
  # include the string
@@ -77,5 +78,56 @@ module AssertionFu
77
78
  assert !File.exists?(path), "#{path} exists"
78
79
  end
79
80
 
81
+ # Gives more consistent diffs, and is insensitive to the ordering
82
+ # of attributes, and changes in whitespace
83
+ def assert_xml_equal(a, b)
84
+ look_for "libxml"
85
+ a = xml_node(a)
86
+ b = xml_node(b)
87
+ if a.element? && b.element?
88
+ assert_equal a.name, b.name
89
+ assert_equal a.attributes.to_h, b.attributes.to_h
90
+ a.children.zip(b.children).each do |c, d|
91
+ assert_xml_equal c, d
92
+ end
93
+ else
94
+ assert_equal a.to_s.strip, b.to_s.strip
95
+ end
96
+ end
80
97
 
98
+ def assert_xml_not_equal(a, b)
99
+ begin
100
+ assert_xml_equal(a, b)
101
+ rescue Test::Unit::AssertionFailedError
102
+ # ok!
103
+ else
104
+ assert false, "XML was equal (#{a.inspect})"
105
+ end
106
+ end
107
+
108
+ def assert_same_path(a, b)
109
+ assert_equal File.expand_path(a), File.expand_path(b)
110
+ end
111
+
112
+ private
113
+
114
+ def look_for(name) #:nodoc:
115
+ require name
116
+ rescue LoadError
117
+ begin
118
+ require "rubygems"
119
+ require name
120
+ rescue LoadError
121
+ raise AssertionFu::Error.new("This assertion requires #{name} to be available")
122
+ end
123
+ end
124
+
125
+ def xml_node(obj) #:nodoc:
126
+ case obj
127
+ when LibXML::XML::Node: obj
128
+ when LibXML::XML::Document: obj.root
129
+ else
130
+ LibXML::XML::Document.string(obj.to_s).root
131
+ end
132
+ end
81
133
  end
@@ -19,4 +19,14 @@ class AssertionFuTest < Test::Unit::TestCase #:nodoc:
19
19
  def test_length
20
20
  assert_length 3, %w[a b c]
21
21
  end
22
+
23
+ def test_xml
24
+ assert_xml_equal(%[<a />], %[<a />])
25
+ assert_xml_equal(%[<a b="c"/>], %[<a b="c"/>])
26
+ assert_xml_equal(%[<a b="c" c="d"/>], %[<a c="d" b="c"/>])
27
+ assert_raises(Test::Unit::AssertionFailedError) do
28
+ assert_xml_not_equal(%[<a b="c" c="d"/>], %[<a c="d" b="c"/>])
29
+ end
30
+ assert_xml_not_equal(%[<a b="c" c="d"/>], %[<z c="d" b="c"/>])
31
+ end
22
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fizx-assertion_fu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kyle Maxwell
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-02-12 00:00:00 -08:00
12
+ date: 2009-02-05 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies: []
15
15