georgebrock-assert-microformats 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.markdown +23 -0
- data/Rakefile +45 -0
- data/lib/assert_microformats.rb +45 -0
- data/rails/init.rb +3 -0
- data/test/assert_microformats_test.rb +149 -0
- data/test/test_helper.rb +56 -0
- metadata +69 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 George Brocklehurst
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# AssertMicroformats #
|
2
|
+
|
3
|
+
A Rails plugin to test pages that contain microformats. Assert that certain microformats are present in the page, and that they have certain properties.
|
4
|
+
|
5
|
+
|
6
|
+
## Setup ##
|
7
|
+
|
8
|
+
# In config/environments/test.rb
|
9
|
+
config.gem "georgebrock-assert-microformats", :source => "http://gems.github.com/", :lib => false
|
10
|
+
|
11
|
+
## Example ##
|
12
|
+
|
13
|
+
# Ensure @response contains an hCard
|
14
|
+
assert_mf_hcard
|
15
|
+
|
16
|
+
# Ensure @response contains an hCard with fn set to 'George Brocklehurst'
|
17
|
+
assert_mf_hcard :fn => 'George Brocklehurst'
|
18
|
+
|
19
|
+
# Ensure the HTML in my_html contains an hCalendar event with summary set to 'My party'
|
20
|
+
assert_mf_hcalendar my_html, :summary => 'My party'
|
21
|
+
|
22
|
+
|
23
|
+
Copyright © 2009 <span class="vcard"><a href="http://georgebrock.com" class="url">George Brocklehurst</a></span>, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require "rake/gempackagetask"
|
4
|
+
require 'rubygems'
|
5
|
+
|
6
|
+
spec = Gem::Specification.new do |s|
|
7
|
+
s.name = "assert-microformats"
|
8
|
+
s.version = "0.1.2"
|
9
|
+
s.summary = "A Rails plugin to help with testing the presence and correctness of microformats markup"
|
10
|
+
s.author = "George Brocklehurst"
|
11
|
+
s.email = "george.brocklehurst@gmail.com"
|
12
|
+
s.homepage = "http://github.com/georgebrock"
|
13
|
+
s.has_rdoc = true
|
14
|
+
s.extra_rdoc_files = %w(README.markdown)
|
15
|
+
s.rdoc_options = %w(--main README.markdown)
|
16
|
+
s.files = %w(MIT-LICENSE README.markdown Rakefile) + Dir.glob("{test,lib,rails}/**/*")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.add_dependency("mofo", ">= 0.2.0")
|
19
|
+
end
|
20
|
+
|
21
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
22
|
+
pkg.gem_spec = spec
|
23
|
+
file = File.dirname(__FILE__) + "/#{spec.name}.gemspec"
|
24
|
+
File.open(file, "w") {|f| f << spec.to_ruby }
|
25
|
+
end
|
26
|
+
|
27
|
+
desc 'Default: run unit tests.'
|
28
|
+
task :default => :test
|
29
|
+
|
30
|
+
desc 'Test the assert-microformats plugin.'
|
31
|
+
Rake::TestTask.new(:test) do |t|
|
32
|
+
t.libs << 'lib'
|
33
|
+
t.libs << 'test'
|
34
|
+
t.pattern = 'test/**/*_test.rb'
|
35
|
+
t.verbose = true
|
36
|
+
end
|
37
|
+
|
38
|
+
desc 'Generate documentation for the assert-microformats plugin.'
|
39
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
40
|
+
rdoc.rdoc_dir = 'rdoc'
|
41
|
+
rdoc.title = 'AssertMicroformats'
|
42
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
43
|
+
rdoc.rdoc_files.include('README')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
gem 'mofo'
|
2
|
+
require 'mofo'
|
3
|
+
|
4
|
+
module AssertMicroformats
|
5
|
+
|
6
|
+
MICROFORMATS = {}
|
7
|
+
ObjectSpace.each_object(Class){|klass|
|
8
|
+
next unless klass < Microformat
|
9
|
+
type = klass.name.downcase.to_sym
|
10
|
+
class_eval %%
|
11
|
+
def assert_mf_#{type} *args, &block
|
12
|
+
assert_microformat #{type.to_sym.inspect}, *args
|
13
|
+
end
|
14
|
+
%
|
15
|
+
MICROFORMATS[type] = klass
|
16
|
+
}
|
17
|
+
|
18
|
+
def assert_microformat(*args)
|
19
|
+
html = @response.nil? ? '' : @response.body
|
20
|
+
type = nil
|
21
|
+
properties = {}
|
22
|
+
|
23
|
+
args.each_with_index do |arg, index|
|
24
|
+
html = arg if arg.is_a?(String)
|
25
|
+
type = arg if arg.is_a?(Symbol)
|
26
|
+
properties = arg if arg.is_a?(Hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
mf = MICROFORMATS[type] or raise ArgumentError.new('Unsupported microformat type')
|
30
|
+
|
31
|
+
instances = mf.find :all => {:text => html}
|
32
|
+
raise Test::Unit::AssertionFailedError.new("No instances of #{type} were found") unless instances.any?
|
33
|
+
|
34
|
+
instances.each do |instance|
|
35
|
+
return if properties.inject(true) do |found, pair|
|
36
|
+
prop, value = pair
|
37
|
+
found && instance.properties.include?(prop.to_s) && (instance.instance_eval(prop.to_s) == value)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
raise Test::Unit::AssertionFailedError.new("#{instances.size} instances of #{type} were found, but none had the required properties")
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
data/rails/init.rb
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class AssertMicroformatsTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
include AssertMicroformatsTestData
|
6
|
+
|
7
|
+
class MockTestResponse
|
8
|
+
attr_reader :body
|
9
|
+
def initialize(body)
|
10
|
+
@body = body || ''
|
11
|
+
end
|
12
|
+
end
|
13
|
+
def test_mock_test_response
|
14
|
+
string = "A unique string"
|
15
|
+
@response = MockTestResponse.new(string)
|
16
|
+
assert_equal string, @response.body
|
17
|
+
@response = nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_should_fail_to_find_any_microformats_in_an_empty_string
|
21
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :hcard end
|
22
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :hcalendar end
|
23
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :hreview end
|
24
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :hentry end
|
25
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :hresume end
|
26
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :xoxo end
|
27
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :geo end
|
28
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_microformat '', :xfn end
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_fail_to_find_any_microformats_in_an_empty_string_using_dynamic_method_names
|
32
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_hcard '' end
|
33
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_hcalendar '' end
|
34
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_hreview '' end
|
35
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_hentry '' end
|
36
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_hresume '' end
|
37
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_xoxo '' end
|
38
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_geo '' end
|
39
|
+
assert_raise(Test::Unit::AssertionFailedError) do assert_mf_xfn '' end
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_find_a_simple_hcard
|
43
|
+
assert_microformat hcards[:simple], :hcard
|
44
|
+
assert_mf_hcard hcards[:simple]
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_find_a_simple_hcalendar_event
|
48
|
+
assert_microformat hcalendars[:simple], :hcalendar
|
49
|
+
assert_mf_hcalendar hcalendars[:simple]
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_should_find_a_simple_hreview
|
53
|
+
assert_microformat hreviews[:simple], :hreview
|
54
|
+
assert_mf_hreview hreviews[:simple]
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_should_find_a_simple_hentry
|
58
|
+
assert_microformat hentries[:simple], :hentry
|
59
|
+
assert_mf_hentry hentries[:simple]
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_should_find_a_simple_hresume
|
63
|
+
assert_microformat hresumes[:simple], :hresume
|
64
|
+
assert_mf_hresume hresumes[:simple]
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_should_find_a_simple_xoxo_outline
|
68
|
+
assert_microformat xoxo[:simple], :xoxo
|
69
|
+
assert_mf_xoxo xoxo[:simple]
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_should_find_a_simple_geo_location
|
73
|
+
assert_microformat geo[:simple], :geo
|
74
|
+
assert_mf_geo geo[:simple]
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_should_find_a_simple_xfn_relationship
|
78
|
+
assert_microformat xfn[:simple], :xfn
|
79
|
+
assert_mf_xfn xfn[:simple]
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_not_find_hcalendar_when_only_hcard_is_present
|
83
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
84
|
+
assert_microformat hcards[:simple], :hcalendar
|
85
|
+
end
|
86
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
87
|
+
assert_mf_hcalendar hcards[:simple]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_should_raise_when_microformat_type_is_invalid
|
92
|
+
assert_raise(ArgumentError) do
|
93
|
+
assert_microformat '', :invalid_microformat_type
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_should_only_match_microformats_which_match_the_given_properties
|
98
|
+
assert_microformat hcards[:simple], :hcard, :fn => 'George Brocklehurst'
|
99
|
+
assert_microformat hcards[:complex], :hcard, :fn => 'George Brocklehurst', :url => 'http://georgebrock.com', :tel => '(020)12312312'
|
100
|
+
assert_mf_hcard hcards[:simple], :fn => 'George Brocklehurst'
|
101
|
+
assert_mf_hcard hcards[:complex], :fn => 'George Brocklehurst', :url => 'http://georgebrock.com', :tel => '(020)12312312'
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_should_not_match_microformats_which_do_not_match_the_given_properties
|
105
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
106
|
+
assert_microformat hcards[:simple], :hcard, :fn => 'Someone Else'
|
107
|
+
end
|
108
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
109
|
+
assert_mf_hcard hcards[:simple], :fn => 'Someone Else'
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_should_not_match_microformats_which_only_match_some_of_the_given_properties
|
114
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
115
|
+
assert_microformat hcards[:simple], :hcard, :fn => 'George Brocklehurst', :url => 'http://www.google.com'
|
116
|
+
end
|
117
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
118
|
+
assert_mf_hcard hcards[:simple], :fn => 'George Brocklehurst', :url => 'http://www.google.com'
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_should_not_match_microformats_that_do_not_have_some_of_the_given_properties
|
123
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
124
|
+
assert_microformat hcards[:simple], :hcard, :fn => 'George Brocklehurst', :fakeprop => 'http://www.google.com'
|
125
|
+
end
|
126
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
127
|
+
assert_mf_hcard hcards[:simple], :fn => 'George Brocklehurst', :fakeprop => 'http://www.google.com'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_should_not_match_microformats_that_do_not_have_any_of_the_given_properties
|
132
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
133
|
+
assert_microformat hcards[:simple], :hcard, :fakeprop => 'http://www.google.com'
|
134
|
+
end
|
135
|
+
assert_raise(Test::Unit::AssertionFailedError) do
|
136
|
+
assert_mf_hcard hcards[:simple], :fakeprop => 'http://www.google.com'
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_should_use_test_response_if_no_html_is_provided
|
141
|
+
@response = MockTestResponse.new(hcards[:simple])
|
142
|
+
assert_microformat @response.body, :hcard, :fn => 'George Brocklehurst'
|
143
|
+
assert_microformat :hcard, :fn => 'George Brocklehurst'
|
144
|
+
assert_mf_hcard @response.body, :fn => 'George Brocklehurst'
|
145
|
+
assert_mf_hcard
|
146
|
+
assert_mf_hcard :fn => 'George Brocklehurst'
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
RAILS_ENV = 'test'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'test/unit'
|
4
|
+
require File.join(File.dirname(__FILE__), "..", "rails", "init")
|
5
|
+
module AssertMicroformatsTestData
|
6
|
+
|
7
|
+
def hcards
|
8
|
+
{
|
9
|
+
:simple => '<span class="vcard"><span class="fn">George Brocklehurst</span></span>',
|
10
|
+
:complex => '<div class="vcard"><a class="fn url" href="http://georgebrock.com">George Brocklehurst</a> <span class="tel">(020)12312312</span></div>'
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
def hcalendars
|
15
|
+
{
|
16
|
+
:simple => '<span class="vevent"><span class="summary">An event</span>, <span class="dtstart">2009-05-30</span></span>'
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def hreviews
|
21
|
+
{
|
22
|
+
:simple => '<span class="hreview">A review of <span class="item vcard"><span class="fn org">Apple</span></span></span>'
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def hentries
|
27
|
+
{
|
28
|
+
:simple => '<div class="hentry"><h1 class="entry-title">Test Entry</h1><span class="published">2009-05-30</span><p class="entry-title">An awesome test case for hEntry</p></div>'
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def hresumes
|
33
|
+
{
|
34
|
+
:simple => '<div class="hresume"><address class="vcard"><span class="fn">George Brocklehurst</span></address></div>'
|
35
|
+
}
|
36
|
+
end
|
37
|
+
|
38
|
+
def xoxo
|
39
|
+
{
|
40
|
+
:simple => '<ol class="xoxo"><li>Does anyone know what XOXO is for?</li><li>And so on</li></ol>'
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def geo
|
45
|
+
{
|
46
|
+
:simple => '<div class="geo"><span class="latitude">37.386013</span>, <span class="longitude">-122.082932</span></div>'
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
def xfn
|
51
|
+
{
|
52
|
+
:simple => '<a href="http://georgebrock.com" rel="me">My other website</a>'
|
53
|
+
}
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: georgebrock-assert-microformats
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- George Brocklehurst
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-06-01 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mofo
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.2.0
|
24
|
+
version:
|
25
|
+
description:
|
26
|
+
email: george.brocklehurst@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.markdown
|
33
|
+
files:
|
34
|
+
- MIT-LICENSE
|
35
|
+
- README.markdown
|
36
|
+
- Rakefile
|
37
|
+
- test/assert_microformats_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- lib/assert_microformats.rb
|
40
|
+
- rails/init.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/georgebrock
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --main
|
46
|
+
- README.markdown
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: "0"
|
60
|
+
version:
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.2.0
|
65
|
+
signing_key:
|
66
|
+
specification_version: 2
|
67
|
+
summary: A Rails plugin to help with testing the presence and correctness of microformats markup
|
68
|
+
test_files: []
|
69
|
+
|