w3c_rspec_validators 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "w3c_validators"
4
+ gem "rspec"
5
+
6
+ # Specify your gem's dependencies in w3c_rspec_validators.gemspec
7
+ gemspec
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # W3C rspec validations
2
+
3
+ This gem adds rspec matchers that allow you to check if the html code produced by your rails app is valid.
4
+
5
+ ## Install
6
+
7
+ You need rspec for this gem to work. In addition add this to your gemfile:
8
+
9
+ gem "w3c_rspec_validators"
10
+
11
+ ## Usage
12
+
13
+ Here is an example for using it in an acceptance test:
14
+
15
+ ```ruby
16
+ scenario "visit landing page" do
17
+ visit "/"
18
+ page.body.should be_valid_html
19
+ end
20
+ ```
21
+
22
+ If you use capybara with a headless driver or something similiar be
23
+ sure to check the original source code to be valid and not the source code that has
24
+ been parsed and eventually corrected by the browser:
25
+
26
+ ```ruby
27
+ scenario "visit landing page" do
28
+ visit "/"
29
+ page.driver.source.should be_valid_html
30
+ end
31
+ ```
32
+
33
+ If you want to check html code, that has been modified by javascript, use "page.body.should be_valid_html".
34
+ Note that a browser may correct wrongly nested tags on its own.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ module W3cRspecValidators
2
+
3
+ RSpec::Matchers.define :be_valid_html do
4
+ response = nil
5
+ match do |body|
6
+ validator = MarkupValidator.new :validator_uri => HTML5Validator.validator_uri
7
+ response = validator.validate_text(body)
8
+ response.errors.length == 0
9
+ end
10
+ failure_message_for_should do |actual|
11
+ response.errors.map do |err|
12
+ seperator = "######\n"
13
+ error = /line \d.*/.match err.to_s
14
+ line_number = /line (\d*)/.match(err.to_s)[1].to_i
15
+
16
+ sbody = actual.split("\n")
17
+ context = sbody[[line_number-3,0].max...line_number-1].join("\n")
18
+ context += "\n>>" + sbody[line_number-1] + "\n"
19
+ context += sbody[line_number..line_number+2].join("\n")
20
+
21
+ seperator + error.to_s + "\n\n" + context + "\n"
22
+ end.join("\n")
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ module W3cRspecValidators
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,20 @@
1
+ require "w3c_rspec_validators/version"
2
+ require "w3c_validators"
3
+
4
+ module W3cRspecValidators
5
+ include W3CValidators
6
+
7
+ class HTML5Validator
8
+ def self.validator_uri
9
+ @@validator_uri
10
+ end
11
+
12
+ def self.validator_uri= value
13
+ @@validator_uri = value
14
+ end
15
+
16
+ self.validator_uri = "http://validator.w3.org/check"
17
+ end
18
+ end
19
+
20
+ require "w3c_rspec_validators/rspec"
@@ -0,0 +1,65 @@
1
+ require "w3c_rspec_validators"
2
+ include W3CValidators
3
+
4
+ module RSpec
5
+ module Matchers
6
+ def fail
7
+ raise_error(RSpec::Expectations::ExpectationNotMetError)
8
+ end
9
+
10
+ def fail_with(message)
11
+ raise_error(RSpec::Expectations::ExpectationNotMetError, message)
12
+ end
13
+
14
+ def fail_matching(regex)
15
+ raise_error(RSpec::Expectations::ExpectationNotMetError, regex)
16
+ end
17
+ end
18
+ end
19
+
20
+
21
+
22
+ describe "be_valid_html" do
23
+ it "should be valid" do
24
+ "<!DOCTYPE HTML SYSTEM>
25
+ <html>
26
+ <head>
27
+ <title>dd</title>
28
+ </head>
29
+ <body>
30
+ </body>
31
+ </html>".should be_valid_html
32
+ end
33
+
34
+ it "should not be valid" do
35
+ "<!DOCTYPE HTML SYSTEM>
36
+ <html>
37
+ <head>
38
+ <titled>dd</title>
39
+ </head>
40
+ <body></body>
41
+ </html>".should_not be_valid_html
42
+ end
43
+
44
+ it "should complain about an missing src attribute" do
45
+ lambda {
46
+ "<!DOCTYPE HTML SYSTEM>
47
+ <html>
48
+ <head>
49
+ <title>dd</title>
50
+ </head>
51
+ <body>
52
+ <img />
53
+ </body>
54
+ </html>".should be_valid_html
55
+ }.should fail_matching(/line 7: required attribute "SRC" not specified.*<img \/>/m)
56
+ end
57
+
58
+ it "should use a custom uri if provided" do
59
+ W3cRspecValidators::HTML5Validator.validator_uri = "http://blubb.de"
60
+ validator = MarkupValidator.new
61
+ MarkupValidator.should_receive(:new).with(:validator_uri => "http://blubb.de").and_return validator
62
+
63
+ "dummy".should_not be_valid_html
64
+ end
65
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "w3c_rspec_validators/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "w3c_rspec_validators"
7
+ s.version = W3cRspecValidators::VERSION
8
+ s.authors = ["Dominik Goltermann"]
9
+ s.email = ["dominik@goltermann.cc"]
10
+ s.homepage = ""
11
+ s.summary = %q{This gem adds w3c validation in form of rspec matchers}
12
+ s.description = %q{At this time it allows for validation of html (including html5) produced by rspec acceptance tests. In addition you can configure the gem to use a locally installed validation service instead of the online w3c servers}
13
+
14
+ s.rubyforge_project = "w3c_rspec_validators"
15
+
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
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "w3c_validators"
24
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: w3c_rspec_validators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Dominik Goltermann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: w3c_validators
16
+ requirement: &13322940 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *13322940
25
+ description: At this time it allows for validation of html (including html5) produced
26
+ by rspec acceptance tests. In addition you can configure the gem to use a locally
27
+ installed validation service instead of the online w3c servers
28
+ email:
29
+ - dominik@goltermann.cc
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - README.md
37
+ - Rakefile
38
+ - lib/w3c_rspec_validators.rb
39
+ - lib/w3c_rspec_validators/rspec.rb
40
+ - lib/w3c_rspec_validators/version.rb
41
+ - spec/matchers_spec.rb
42
+ - w3c_rspec_validators.gemspec
43
+ homepage: ''
44
+ licenses: []
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project: w3c_rspec_validators
63
+ rubygems_version: 1.8.10
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: This gem adds w3c validation in form of rspec matchers
67
+ test_files: []