xmatch 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -29,8 +29,8 @@ Custom matchers
29
29
  The actual values of some xml elements are hard to know in advance (timestamps and ids being typical examples). XMatch allows custom matchers to be applied
30
30
  to provide a good guess at a match in advance of the match being run. Custom matchers are predicates provided as Ruby Procs, identified by the xpath of the element they should be applied to.
31
31
 
32
- custom_matchers = { "/bookstore/@id" => lambda {|elem| elem.value.match(/\d+/) }
33
- xml = Matcher::Xml.new("<bookstore id='1'></bookstore>", custom_matchers)
32
+ xml = Matcher::Xml.new("<bookstore id='1'></bookstore>")
33
+ xml.match_on("/bookstore/@id") { |actual| actual =~ /\d+/ }
34
34
  xml.match("<bookstore id='2'></bookstore>") # ==> true
35
35
 
36
36
  Formatting match results
@@ -37,7 +37,8 @@ module Matcher
37
37
  :line => elem.line,
38
38
  :path => elem.path,
39
39
  :expected => info ? info.expected : Matcher::Xml::EXISTENCE,
40
- :actual => info ? info.actual : Matcher::Xml::UNMATCHED)
40
+ :actual => info ? info.actual : Matcher::Xml::UNMATCHED,
41
+ :custom_matched => info ? info.was_custom_matched : false)
41
42
  end
42
43
 
43
44
  def generate_html(data)
@@ -59,7 +59,7 @@ module Nokogiri
59
59
  return false unless other_elem
60
60
 
61
61
  custom_matcher = matcher.custom_matchers[path]
62
- match = custom_matcher ? custom_matcher.call(other_elem) : (content == other_elem.content)
62
+ match = custom_matcher ? custom_matcher.call(other_elem.content) : (content == other_elem.content)
63
63
  @matcher.record(self.path, match, content, other_elem.content)
64
64
  match
65
65
  end
@@ -73,7 +73,7 @@ module Nokogiri
73
73
  return false unless other_elem
74
74
 
75
75
  custom_matcher = matcher.custom_matchers[path]
76
- match = custom_matcher ? custom_matcher.call(other_elem) : (value == other_elem.value)
76
+ match = custom_matcher ? custom_matcher.call(other_elem.value) : (value == other_elem.value)
77
77
  matcher.record(self.path, match, value, other_elem.value)
78
78
  match
79
79
  end
@@ -1,3 +1,3 @@
1
1
  module Representative
2
- VERSION = "0.1.5".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
@@ -42,7 +42,11 @@ Expected xml document is <b><%= completedness %>%</b> 'matched'.
42
42
  <tr class=<%= match.result %> >
43
43
  <td><%= match.line %></td>
44
44
  <td><%= match.path %></td>
45
+ <% if match.custom_matched == true%>
46
+ <td><i>(something like) <%= match.expected %></i></td>
47
+ <% else %>
45
48
  <td><%= match.expected %></td>
49
+ <% end %>
46
50
  <td><%= match.actual %></td>
47
51
  </tr>
48
52
  <% end %>
data/lib/matcher/xml.rb CHANGED
@@ -17,6 +17,10 @@ module Matcher
17
17
  @results = {}
18
18
  end
19
19
 
20
+ def match_on(path, &blk)
21
+ @custom_matchers[path] = blk
22
+ end
23
+
20
24
  def match(actual)
21
25
  @results.clear
22
26
  @rhs = parse(actual)
@@ -26,7 +30,8 @@ module Matcher
26
30
  def record(path, result, expected, actual)
27
31
  # support 0 as true (for regex matches)
28
32
  r = !result || result.nil? ? false : true
29
- @results[path] = OpenStruct.new(:result => r, :expected => expected, :actual => actual)
33
+ was_custom_matched = @custom_matchers[path] ? true : false
34
+ @results[path] = OpenStruct.new(:result => r, :expected => expected, :actual => actual, :was_custom_matched => was_custom_matched)
30
35
  end
31
36
 
32
37
  def result_for(path)
@@ -308,25 +308,41 @@ describe Matcher::Xml do
308
308
  end
309
309
 
310
310
  it "can be used on an attribute value" do
311
- custom_matchers = { "/bookstore/@id" => lambda {|elem| elem.value == '2'} }
311
+ custom_matchers = { "/bookstore/@id" => lambda {|actual| actual == '2'} }
312
312
  xml = Matcher::Xml.new("<bookstore id='1'></bookstore>", custom_matchers)
313
313
  xml.match("<bookstore id='2'></bookstore>").should be_true
314
314
  end
315
315
 
316
316
  it "can be used on an element value" do
317
- custom_matchers = { "/bookstore/book/text()" => lambda {|elem| elem.content == 'bar'} }
317
+ custom_matchers = { "/bookstore/book/text()" => lambda {|actual| actual == 'bar'} }
318
318
  xml = Matcher::Xml.new("<bookstore><book>foo</book></bookstore", custom_matchers)
319
319
  xml.match("<bookstore><book>bar</book></bookstore").should be_true
320
320
  end
321
321
 
322
322
  it "supports regex style matching" do
323
- custom_matchers = { "/bookstore/book/text()" => lambda {|elem| elem.content =~ /bar/} }
323
+ custom_matchers = { "/bookstore/book/text()" => lambda {|actual| actual =~ /bar/} }
324
324
  xml = Matcher::Xml.new("<bookstore><book>foo</book></bookstore", custom_matchers)
325
325
  xml.match("<bookstore><book>bar</book></bookstore").should be_true
326
326
  xml.mismatches.should be_empty
327
327
  xml.matches.should have(3).matches
328
328
  end
329
329
 
330
+ it "can be matched on" do
331
+ xml = Matcher::Xml.new("<bookstore><book>foo</book></bookstore")
332
+ xml.match_on("/bookstore/book/text()") { |actual| actual =~ /bar/ }
333
+ xml.match("<bookstore><book>bar</book></bookstore").should be_true
334
+ xml.mismatches.should be_empty
335
+ xml.matches.should have(3).matches
336
+ end
337
+
338
+ it "tells if a custom matcher was used" do
339
+ xml = Matcher::Xml.new("<bookstore><book>foo</book></bookstore")
340
+ xml.match_on("/bookstore/book/text()") { |actual| actual =~ /bar/ }
341
+ xml.match("<bookstore><book>bar</book></bookstore").should be_true
342
+ xml.matches["/bookstore/book/text()"].was_custom_matched.should be_true
343
+ xml.matches["/bookstore/book"].was_custom_matched.should be_false
344
+ end
345
+
330
346
  end
331
347
 
332
348
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmatch
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 5
10
- version: 0.1.5
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Peter Moran
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-23 00:00:00 +11:00
18
+ date: 2010-11-24 00:00:00 +11:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -93,7 +93,6 @@ files:
93
93
  - spec/fixtures/books7.xml
94
94
  - spec/fixtures/books8.xml
95
95
  - spec/fixtures/books9.xml
96
- - spec/matcher/html_formatter_spec.rb
97
96
  - spec/matcher/smoke_spec.rb
98
97
  - spec/matcher/xml_spec.rb
99
98
  - spec/spec_helper.rb
@@ -143,7 +142,6 @@ test_files:
143
142
  - spec/fixtures/books7.xml
144
143
  - spec/fixtures/books8.xml
145
144
  - spec/fixtures/books9.xml
146
- - spec/matcher/html_formatter_spec.rb
147
145
  - spec/matcher/smoke_spec.rb
148
146
  - spec/matcher/xml_spec.rb
149
147
  - spec/spec_helper.rb
@@ -1,8 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
-
3
- require 'nokogiri'
4
-
5
- describe Matcher::HtmlFormatter do
6
-
7
-
8
- end