spp_nokogiri_ext 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +40 -0
- data/README +0 -0
- data/Rakefile +10 -0
- data/build +11 -0
- data/build.rb +11 -0
- data/lib/spp_nokogiri_ext/dom_matcher.rb +77 -0
- data/lib/spp_nokogiri_ext/spec_helpers.rb +2 -0
- data/lib/spp_nokogiri_ext/xml_matcher.rb +48 -0
- data/lib/spp_nokogiri_ext.rb +8 -0
- data/lib/tasks/gem.rake +18 -0
- data/lib/version.rb +4 -0
- data/rubyversion +7 -0
- data/spec/matchers/dom_matcher_spec.rb +64 -0
- data/spec/spec_helper.rb +2 -0
- data/spp_nokogiri_ext.gemspec +27 -0
- metadata +153 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use `./rubyversion` > /dev/null
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
spp_nokogiri_ext (0.0.4)
|
5
|
+
activesupport
|
6
|
+
nokogiri
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://gems.devint.lpo/
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
activesupport (3.2.6)
|
13
|
+
i18n (~> 0.6)
|
14
|
+
multi_json (~> 1.0)
|
15
|
+
diff-lcs (1.1.3)
|
16
|
+
i18n (0.6.0)
|
17
|
+
multi_json (1.3.6)
|
18
|
+
nokogiri (1.5.2)
|
19
|
+
nokogiri-diff (0.1.1)
|
20
|
+
nokogiri (~> 1.5)
|
21
|
+
tdiff (~> 0.3.2)
|
22
|
+
rake (0.9.2.2)
|
23
|
+
rspec (2.10.0)
|
24
|
+
rspec-core (~> 2.10.0)
|
25
|
+
rspec-expectations (~> 2.10.0)
|
26
|
+
rspec-mocks (~> 2.10.0)
|
27
|
+
rspec-core (2.10.1)
|
28
|
+
rspec-expectations (2.10.0)
|
29
|
+
diff-lcs (~> 1.1.3)
|
30
|
+
rspec-mocks (2.10.1)
|
31
|
+
tdiff (0.3.2)
|
32
|
+
|
33
|
+
PLATFORMS
|
34
|
+
ruby
|
35
|
+
|
36
|
+
DEPENDENCIES
|
37
|
+
nokogiri-diff
|
38
|
+
rake
|
39
|
+
rspec
|
40
|
+
spp_nokogiri_ext!
|
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
load 'lib/tasks/gem.rake'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
desc 'Default: run specs.'
|
4
|
+
task :default => :spec
|
5
|
+
|
6
|
+
desc "Run specs"
|
7
|
+
RSpec::Core::RakeTask.new do |t|
|
8
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
9
|
+
# Put spec opts in a file named .rspec in root
|
10
|
+
end
|
data/build
ADDED
data/build.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
#!/usr/bin/env ruby
|
4
|
+
Dir.chdir(File.dirname(__FILE__))
|
5
|
+
puts `gem install bundler` unless `gem list`.match /bundler/
|
6
|
+
system('bundle install --path $HOME/.bundle')
|
7
|
+
require 'rubygems'
|
8
|
+
require 'bundler/setup'
|
9
|
+
|
10
|
+
rake_args = ARGV.empty? ? [] : ARGV
|
11
|
+
exec(*(%w{bundle exec rake} + rake_args))
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'nokogiri/diff'
|
5
|
+
|
6
|
+
|
7
|
+
if defined?(RSpec) && defined?(RSpec::Matchers)
|
8
|
+
RSpec::Matchers
|
9
|
+
else
|
10
|
+
Spec::Matchers
|
11
|
+
end.define :match_the_dom_of do |expected_xml_string|
|
12
|
+
|
13
|
+
def describe(string)
|
14
|
+
string ? string.to_s.gsub('"', "'").strip.inspect : 'NIL'
|
15
|
+
end
|
16
|
+
|
17
|
+
failure_message_for_should_not do |actual|
|
18
|
+
"expected that #{describe(actual)} would not match the dom of #{describe(expected)}"
|
19
|
+
end
|
20
|
+
|
21
|
+
failure_message_for_should do |actual|
|
22
|
+
actual = actual.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_DECLARATION) if actual.is_a? Nokogiri::XML::Document
|
23
|
+
<<-MESSAGE
|
24
|
+
|
25
|
+
expected:
|
26
|
+
#{describe(expected.first.squish) }
|
27
|
+
|
28
|
+
to equal:
|
29
|
+
#{describe(actual.squish) }
|
30
|
+
|
31
|
+
diff:
|
32
|
+
#{pretty_diff(expected.first, actual)}
|
33
|
+
(compared using Hash.from_xml.eql?)
|
34
|
+
MESSAGE
|
35
|
+
end
|
36
|
+
|
37
|
+
failure_message_for_should_not do |actual|
|
38
|
+
<<-MESSAGE
|
39
|
+
|
40
|
+
expected:
|
41
|
+
#{describe(expected.squish) }
|
42
|
+
|
43
|
+
not to equal actual:
|
44
|
+
#{describe(actual.squish) }
|
45
|
+
|
46
|
+
diff:
|
47
|
+
#{pretty_diff(expected.first, actual)}
|
48
|
+
(compared using Hash.from_xml.eql?)
|
49
|
+
MESSAGE
|
50
|
+
end
|
51
|
+
|
52
|
+
match do |actual_xml_string|
|
53
|
+
expected_hash = Hash.from_xml(hacky_canonicalise(expected_xml_string))
|
54
|
+
actual_hash = Hash.from_xml(hacky_canonicalise(actual_xml_string))
|
55
|
+
(expected_hash == actual_hash)
|
56
|
+
end
|
57
|
+
|
58
|
+
def hacky_canonicalise(xml_string)
|
59
|
+
xml_string.to_s.gsub(' ', '').gsub("\n", '')
|
60
|
+
end
|
61
|
+
|
62
|
+
def pretty_diff(expected_xml_string, actual_xml_string)
|
63
|
+
expected_doc = Nokogiri::XML.parse(hacky_canonicalise(expected_xml_string))
|
64
|
+
actual_doc = Nokogiri::XML.parse(hacky_canonicalise(actual_xml_string))
|
65
|
+
diff = ''
|
66
|
+
|
67
|
+
expected_doc.diff(actual_doc, :added => true) do |change, node|
|
68
|
+
diff << "+ #{node.to_html.ljust(80) + node.parent.path}\n"
|
69
|
+
end
|
70
|
+
|
71
|
+
expected_doc.diff(actual_doc, :removed => true) do |change, node|
|
72
|
+
diff << "- #{node.to_html.ljust(80) + node.parent.path}\n"
|
73
|
+
end
|
74
|
+
|
75
|
+
diff
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
if defined?(RSpec) && defined?(RSpec::Matchers)
|
4
|
+
RSpec::Matchers
|
5
|
+
else
|
6
|
+
Spec::Matchers
|
7
|
+
end.define :match_xml do |expected_xml|
|
8
|
+
|
9
|
+
failure_message_for_should do |actual|
|
10
|
+
<<-MESSAGE
|
11
|
+
|
12
|
+
expected first, but got second:
|
13
|
+
#{expected}
|
14
|
+
|
15
|
+
actual:
|
16
|
+
#{actual}
|
17
|
+
MESSAGE
|
18
|
+
end
|
19
|
+
|
20
|
+
failure_message_for_should_not do |actual|
|
21
|
+
<<-MESSAGE
|
22
|
+
|
23
|
+
expected actual not to equal expected:
|
24
|
+
#{expected}
|
25
|
+
|
26
|
+
actual:
|
27
|
+
#{actual}
|
28
|
+
MESSAGE
|
29
|
+
end
|
30
|
+
|
31
|
+
match do |actual_xml|
|
32
|
+
begin
|
33
|
+
expected_hash = Hash.from_xml(expected_xml.to_s.gsub(' ', '').gsub("\n", ''))
|
34
|
+
actual_hash = Hash.from_xml(actual_xml.to_s.gsub(' ', '').gsub("\n", ''))
|
35
|
+
rescue NoMethodError, REXML::ParseException => e # Hash.from_xml uses rexml, yech
|
36
|
+
puts "failed XML parse: #{e.name}"
|
37
|
+
# Returns false immediately.
|
38
|
+
next false
|
39
|
+
end
|
40
|
+
|
41
|
+
unless expected_hash == actual_hash
|
42
|
+
puts "Expected: " + expected_hash.inspect
|
43
|
+
puts "Actual: " + actual_hash.inspect
|
44
|
+
end
|
45
|
+
|
46
|
+
(expected_hash == actual_hash)
|
47
|
+
end
|
48
|
+
end
|
data/lib/tasks/gem.rake
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'lib/version'
|
2
|
+
namespace "gem" do
|
3
|
+
|
4
|
+
desc "Build a gem ready to push to gems.devint.lpo"
|
5
|
+
task "package" do
|
6
|
+
sh "gem build spp_nokogiri_ext.gemspec"
|
7
|
+
end
|
8
|
+
|
9
|
+
desc "Push the current version of the gem to gems.devint.lpo"
|
10
|
+
task "push_to_devint" do
|
11
|
+
sh "scp spp_nokogiri_ext-#{SppNokogiriExt::VERSION}.gem gems.devint.lpo:/lp/gem-repo/gems"
|
12
|
+
sh "ssh gems.devint.lpo 'gem generate_index -d /lp/gem-repo'"
|
13
|
+
end
|
14
|
+
|
15
|
+
desc "Build the current version & push it to gems.devint.lpo"
|
16
|
+
task "build_and_push" => ['gem:package', 'gem:push_to_devint']
|
17
|
+
|
18
|
+
end
|
data/lib/version.rb
ADDED
data/rubyversion
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'DOM matcher' do
|
4
|
+
|
5
|
+
it 'correctly identifies identical DOMs' do
|
6
|
+
'<foo><bar>baz</bar></foo>'.should match_the_dom_of '<foo><bar>baz</bar></foo>'
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'correctly identifies different DOMs' do
|
10
|
+
'<foo><bar>baz</bar></foo>'.should_not match_the_dom_of '<foo><qux>baz</qux>quo</foo>'
|
11
|
+
end
|
12
|
+
|
13
|
+
# DB: this may be existing behaviour but I believe it's wrong; I don't think they _are_ equivalent
|
14
|
+
it 'treats whitespace between tags as equivalent to an empty closed tag' do
|
15
|
+
'<foo> </foo>'.should match_the_dom_of '<foo />'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'raises an exception when the expected XML is invalid' do
|
19
|
+
lambda { '<foo>'.should match_the_dom_of '<foo />'}.should raise_exception
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'raises an exception when the actual XML is invalid' do
|
23
|
+
lambda { '<foo />'.should match_the_dom_of '<foo>'}.should raise_exception
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'prints a pretty diff when the match' do
|
27
|
+
before do
|
28
|
+
@pretty_failure = <<-PRETTY
|
29
|
+
|
30
|
+
expected:
|
31
|
+
"<foo><qux>baz</qux>quo</foo>"
|
32
|
+
|
33
|
+
to equal:
|
34
|
+
"<foo><bar>baz</bar></foo>"
|
35
|
+
|
36
|
+
diff:
|
37
|
+
+ <bar>baz</bar> /foo
|
38
|
+
- <qux>baz</qux> /foo
|
39
|
+
- quo /foo
|
40
|
+
|
41
|
+
(compared using Hash.from_xml.eql?)
|
42
|
+
PRETTY
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'fails for two strings' do
|
46
|
+
expectation = lambda { '<foo><bar>baz</bar></foo>'.should match_the_dom_of '<foo><qux>baz</qux>quo</foo>'}
|
47
|
+
output_for_failing_expectation(expectation).should == @pretty_failure
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'fails for a Nokogiri doc and a string' do
|
51
|
+
expectation = lambda { Nokogiri::XML('<foo><bar>baz</bar></foo>').should match_the_dom_of '<foo><qux>baz</qux>quo</foo>'}
|
52
|
+
output_for_failing_expectation(expectation).should == @pretty_failure
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def output_for_failing_expectation(expectation)
|
57
|
+
begin
|
58
|
+
expectation.call
|
59
|
+
rescue RSpec::Expectations::ExpectationNotMetError => e
|
60
|
+
return e.to_s
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
# Metadata
|
7
|
+
s.name = "spp_nokogiri_ext"
|
8
|
+
s.version = SppNokogiriExt::VERSION
|
9
|
+
s.authors = ["Daniel Heath"]
|
10
|
+
s.email = ["daniel.heath@lonelyplanet.com.au"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{Extensions to Nokogiri used across SPP programs}
|
13
|
+
s.description = %q{Extensions to Nokogiri used across SPP programs}
|
14
|
+
|
15
|
+
# Manifest
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rake"
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
s.add_runtime_dependency "activesupport"
|
24
|
+
s.add_runtime_dependency "nokogiri"
|
25
|
+
# s.add_runtime_dependency "nokogiri-diff" # This currently requires that an up-to-date version of rubygems be installed (our app servers need updating - see INC478208)
|
26
|
+
s.add_development_dependency "nokogiri-diff" # TODO Remove this once INC478208 is resolved.
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: spp_nokogiri_ext
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Daniel Heath
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-07-03 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rake
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
requirement: *id001
|
33
|
+
prerelease: false
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
type: :development
|
46
|
+
requirement: *id002
|
47
|
+
prerelease: false
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: activesupport
|
50
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
type: :runtime
|
60
|
+
requirement: *id003
|
61
|
+
prerelease: false
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: nokogiri
|
64
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
hash: 3
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
type: :runtime
|
74
|
+
requirement: *id004
|
75
|
+
prerelease: false
|
76
|
+
- !ruby/object:Gem::Dependency
|
77
|
+
name: nokogiri-diff
|
78
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
hash: 3
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
type: :development
|
88
|
+
requirement: *id005
|
89
|
+
prerelease: false
|
90
|
+
description: Extensions to Nokogiri used across SPP programs
|
91
|
+
email:
|
92
|
+
- daniel.heath@lonelyplanet.com.au
|
93
|
+
executables: []
|
94
|
+
|
95
|
+
extensions: []
|
96
|
+
|
97
|
+
extra_rdoc_files: []
|
98
|
+
|
99
|
+
files:
|
100
|
+
- .gitignore
|
101
|
+
- .rvmrc
|
102
|
+
- Gemfile
|
103
|
+
- Gemfile.lock
|
104
|
+
- README
|
105
|
+
- Rakefile
|
106
|
+
- build
|
107
|
+
- build.rb
|
108
|
+
- lib/spp_nokogiri_ext.rb
|
109
|
+
- lib/spp_nokogiri_ext/dom_matcher.rb
|
110
|
+
- lib/spp_nokogiri_ext/spec_helpers.rb
|
111
|
+
- lib/spp_nokogiri_ext/xml_matcher.rb
|
112
|
+
- lib/tasks/gem.rake
|
113
|
+
- lib/version.rb
|
114
|
+
- rubyversion
|
115
|
+
- spec/matchers/dom_matcher_spec.rb
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spp_nokogiri_ext.gemspec
|
118
|
+
homepage: ""
|
119
|
+
licenses: []
|
120
|
+
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
none: false
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
hash: 3
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
136
|
+
none: false
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
hash: 3
|
141
|
+
segments:
|
142
|
+
- 0
|
143
|
+
version: "0"
|
144
|
+
requirements: []
|
145
|
+
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 1.8.24
|
148
|
+
signing_key:
|
149
|
+
specification_version: 3
|
150
|
+
summary: Extensions to Nokogiri used across SPP programs
|
151
|
+
test_files:
|
152
|
+
- spec/matchers/dom_matcher_spec.rb
|
153
|
+
- spec/spec_helper.rb
|