w3c_rspec_validators 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,6 +2,10 @@ source "http://rubygems.org"
2
2
 
3
3
  gem "w3c_validators"
4
4
  gem "rspec"
5
+ gem "linecache19", git: "git://github.com/mark-moseley/linecache", require: "ruby-debug"
6
+ gem "ruby-debug-base19x", "~> 0.11.30.pre4"
7
+ gem "ruby-debug19"
8
+
5
9
 
6
10
  # Specify your gem's dependencies in w3c_rspec_validators.gemspec
7
11
  gemspec
data/README.md CHANGED
@@ -10,7 +10,9 @@ You need rspec for this gem to work. In addition add this to your gemfile:
10
10
 
11
11
  ## Usage
12
12
 
13
- Here is an example for using it in an acceptance test:
13
+ Here are examples for using it in acceptance tests:
14
+
15
+ ### HTML Validation
14
16
 
15
17
  ```ruby
16
18
  scenario "visit landing page" do
@@ -33,6 +35,20 @@ end
33
35
  If you want to check html code, that has been modified by javascript, use "page.body.should be_valid_html".
34
36
  Note that a browser may correct wrongly nested tags on its own.
35
37
 
38
+ ### CSS Validation
39
+
40
+ W3cRspecValidators::CssFiles.compiled will run rake assets:precompile and returns
41
+ the applications css as string.
42
+
43
+ WARNING: This also deletes the folder public/assets so that the compiled assets are removed after the tests.
44
+
45
+ ```ruby
46
+ it "should be valid css" do
47
+ W3cRspecValidators::CssFiles.compiled.should be_valid_css
48
+ end
49
+ ```
50
+
51
+
36
52
  ## Running faster offline validation
37
53
 
38
54
  If you do not want to use the online W3C Validation service, which is default, you can set the validator url to a locally runnig version
@@ -41,7 +57,9 @@ of the W3C Validator service by creating a config file:
41
57
  rails g w3c_rspec_validators:config
42
58
 
43
59
  Check this url to see how to install your local version of the W3C validation service: http://validator.w3.org/docs/install.html
44
- For local HTML5 validation to work you'll need validator.nu running locally too: http://about.validator.nu/
60
+ For local HTML5 validation to work you'll need validator.nu running locally too: http://about.validator.nu
61
+
62
+ For local CSS validation to work you'll need http://jigsaw.w3.org/css-validator/DOWNLOAD.html
45
63
 
46
64
  ## Licence
47
65
  MIT License
@@ -1,2 +1,6 @@
1
+ # you cann comment out the service uris that you don't need. The default online
2
+ # validator will be used then.
3
+
1
4
  config:
2
5
  w3c_service_uri: "http://localhost.de/check"
6
+ w3c_css_service_uri: "http://css.localhost.de/validator"
@@ -8,6 +8,8 @@ module W3cRspecValidators
8
8
  if config_file.file? &&
9
9
  Config.load!(config_file)
10
10
  end
11
+
12
+ # puts ActionDispatch::Routing::UrlFor::url_for("/")
11
13
  end
12
14
  end
13
15
 
@@ -0,0 +1,15 @@
1
+ module W3cRspecValidators
2
+ class CssFiles
3
+ def self.compiled paths=["#{Rails.root}/public/assets/*.css"]
4
+ system "rake -q assets:precompile"
5
+ files = Dir.glob(paths.join(","))
6
+ css = ""
7
+ files.each do |f|
8
+ css += File.read f
9
+ end
10
+ FileUtils.rm_rf "#{Rails.root}/public/assets/"
11
+
12
+ css
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ module W3cRspecValidators
2
+ class ErrorParser
3
+ def self.parse_html_error err, actual
4
+ seperator = "######\n"
5
+ error = /line \d.*/.match err.to_s
6
+ line_number = /line (\d*)/.match(err.to_s)[1].to_i
7
+
8
+ sbody = actual.split("\n")
9
+ context = sbody[[line_number-3,0].max...line_number-1].join("\n")
10
+ context += "\n>>" + sbody[line_number-1] + "\n"
11
+ context += sbody[line_number..line_number+2].join("\n")
12
+
13
+ seperator + error.to_s + "\n\n" + context + "\n"
14
+ end
15
+
16
+ def self.parse_css_error err, actual
17
+ seperator = "######\n"
18
+ error = /line \d*:(.*)/m.match(err.to_s)[1]
19
+ line_number = /line (\d*)/.match(err.to_s)[1].to_i
20
+
21
+ stext = actual.split("\n")
22
+
23
+ i = line_number
24
+ file = ""
25
+ while true do
26
+ if i == 0 || match = /\/\* line \d*,(.*)\*\//.match(stext[i])
27
+ unless match.nil?
28
+ file = match[1].strip
29
+ end
30
+ break
31
+ end
32
+ i -= 1
33
+ end
34
+ real_line_number = line_number - i - 1
35
+
36
+ context = stext[[line_number-3,0].max...line_number-1].join("\n")
37
+ context += "\n>>" + stext[line_number-1] + "\n"
38
+ context += stext[line_number..line_number+2].join("\n")
39
+
40
+ seperator + error.to_s.gsub(/\n/,"").gsub(" ", "") + "\nReal code location(guessed) in File " + file + " on line #{real_line_number}\n\nCode context:\n" + context + "\n"
41
+ end
42
+ end
43
+ end
@@ -3,23 +3,27 @@ module W3cRspecValidators
3
3
  RSpec::Matchers.define :be_valid_html do
4
4
  validator = Validator.new
5
5
  match do |body|
6
- validator.validate_text(body)
6
+ validator.validate_html(body)
7
7
  validator.response.errors.length == 0
8
8
  end
9
9
  failure_message_for_should do |actual|
10
10
  validator.response.errors.map do |err|
11
- seperator = "######\n"
12
- error = /line \d.*/.match err.to_s
13
- line_number = /line (\d*)/.match(err.to_s)[1].to_i
14
-
15
- sbody = actual.split("\n")
16
- context = sbody[[line_number-3,0].max...line_number-1].join("\n")
17
- context += "\n>>" + sbody[line_number-1] + "\n"
18
- context += sbody[line_number..line_number+2].join("\n")
19
-
20
- seperator + error.to_s + "\n\n" + context + "\n"
11
+ ErrorParser.parse_html_error(err, actual)
21
12
  end.join("\n")
22
13
  end
23
14
  end
15
+
16
+ RSpec::Matchers.define :be_valid_css do
17
+ validator = Validator.new
18
+ match do |css|
19
+ validator.validate_css css
20
+ validator.response.errors.length == 0
21
+ end
24
22
 
23
+ failure_message_for_should do |actual|
24
+ validator.response.errors.map do |err|
25
+ ErrorParser.parse_css_error(err, actual)
26
+ end.join("\n")
27
+ end
28
+ end
25
29
  end
@@ -4,12 +4,21 @@ module W3cRspecValidators
4
4
  include W3CValidators
5
5
 
6
6
  def initialize
7
- @validator = MarkupValidator.new validator_uri: Config.get["w3c_service_uri"]
7
+ @html_validator = MarkupValidator.new validator_uri: Config.get["w3c_service_uri"]
8
+ @css_validator = CSSValidator.new validator_uri: Config.get["w3c_css_service_uri"]
8
9
  end
9
10
 
10
- def validate_text text
11
- @response = @validator.validate_text(text)
11
+ def validate_html text
12
+ @response = @html_validator.validate_text(text)
12
13
  raise "Error: Invalid validation response! Tip: check if validator.nu engine is configured correctly" if @response.checked_by.blank?
14
+ @response
15
+ end
16
+
17
+ def validate_css text
18
+ @response = @css_validator.validate_text(text)
19
+ raise "Error: Invalid validation response! Tip: check if validator.nu engine is configured correctly" if @response.checked_by.blank?
20
+ @response
13
21
  end
14
22
  end
23
+
15
24
  end
@@ -1,3 +1,3 @@
1
1
  module W3cRspecValidators
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -3,4 +3,6 @@ require "w3c_validators"
3
3
  require "w3c_rspec_validators/version"
4
4
  require "w3c_rspec_validators/config"
5
5
  require "w3c_rspec_validators/validator"
6
+ require "w3c_rspec_validators/css_files"
7
+ require "w3c_rspec_validators/error_parser"
6
8
  require "w3c_rspec_validators/rspec"
@@ -0,0 +1,18 @@
1
+ require "w3c_rspec_validators"
2
+
3
+ describe "css_files" do
4
+ describe "self.compiled" do
5
+
6
+ before(:each) do
7
+ Object.should_receive(:system).with "rake -q assets:precompile"
8
+ Rails.stub(:root).and_return "railsroot"
9
+ FileUtils.should_receive(:rm_rf).with "railsroot/public/assets/"
10
+ end
11
+
12
+ it "should read css files from a directory" do
13
+ directory = File.dirname(__FILE__)
14
+ W3cRspecValidators::CssFiles.compiled(["#{directory}/fixtures/*.css"])
15
+ .should == "#home {}\nbody {\n}\n"
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,60 @@
1
+ require "w3c_rspec_validators"
2
+
3
+ describe "error_parser" do
4
+
5
+ describe "self.parse_css" do
6
+ before(:each) do
7
+ @css_error_message = "ERROR; URI: file://localhost/TextArea; line 7:
8
+
9
+ Property foo doesn't exist :"
10
+ @css = "
11
+ body {
12
+ blubb: 1 px;
13
+ }
14
+ /* line 5, ../../app/assets/stylesheets/home.css.scss */
15
+ #home {
16
+ foo: 2px;
17
+ }"
18
+ end
19
+
20
+ it "should properly format css errors" do
21
+ W3cRspecValidators::ErrorParser.parse_css_error(@css_error_message, @css)
22
+ .should == "######
23
+ Property foo doesn't exist :
24
+ Real code location(guessed) in File ../../app/assets/stylesheets/home.css.scss on line 2
25
+
26
+ Code context:
27
+ /* line 5, ../../app/assets/stylesheets/home.css.scss */
28
+ #home {
29
+ >> foo: 2px;
30
+ }\n"
31
+ end
32
+ end
33
+
34
+ describe "self.parse_html" do
35
+ before(:each) do
36
+ @html_error_message = "ERROR; URI: upload://Form Submission; line 7: An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images."
37
+ @html = "<!DOCTYPE HTML SYSTEM>
38
+ <html>
39
+ <head>
40
+ <title>dd</title>
41
+ </head>
42
+ <body>
43
+ <img />
44
+ </body>
45
+ </html>"
46
+ end
47
+
48
+ it "should properly format a html error message" do
49
+ W3cRspecValidators::ErrorParser.parse_html_error(@html_error_message, @html)
50
+ .should == "######
51
+ line 7: An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images.
52
+
53
+ </head>
54
+ <body>
55
+ >> <img />
56
+ </body>
57
+ </html>\n"
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,2 @@
1
+ body {
2
+ }
@@ -0,0 +1 @@
1
+ #home {}
@@ -17,50 +17,69 @@ module RSpec
17
17
  end
18
18
  end
19
19
 
20
+ describe "rspec matchers" do
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
21
33
 
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
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
33
43
 
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
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
43
57
 
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)
58
+ it "should use a custom uri if provided" do
59
+ W3cRspecValidators::Config.stub(:get).and_return("w3c_service_uri" => "http://blubb.de")
60
+
61
+ validator = MarkupValidator.new
62
+ MarkupValidator.should_receive(:new).with(:validator_uri => "http://blubb.de").and_return validator
63
+
64
+ "dummy".should_not be_valid_html
65
+ end
56
66
  end
57
67
 
58
- it "should use a custom uri if provided" do
59
- W3cRspecValidators::Config.stub(:get).and_return("w3c_service_uri" => "http://blubb.de")
60
-
61
- validator = MarkupValidator.new
62
- MarkupValidator.should_receive(:new).with(:validator_uri => "http://blubb.de").and_return validator
68
+ describe "be_valid_css" do
69
+ it "should be valid" do
70
+ "body {}".should be_valid_css
71
+ end
72
+
73
+ it "should not be valid" do
74
+ "body { foo: 12px; }".should_not be_valid_css
75
+ end
76
+
77
+ it "should not be valid" do
78
+ lambda {
79
+ "body { foo: 12px; }".should be_valid_css
80
+ }.should fail_matching(/Property foo doesn't exist/)
81
+ end
63
82
 
64
- "dummy".should_not be_valid_html
65
83
  end
84
+
66
85
  end
@@ -5,8 +5,9 @@ describe W3cRspecValidators::Validator do
5
5
 
6
6
  describe "initialize" do
7
7
  it "should initialize MarkupValidator with config value" do
8
- W3cRspecValidators::Config.stub(:get).and_return("w3c_service_uri" => "http://blubb.de")
8
+ W3cRspecValidators::Config.stub(:get).and_return("w3c_service_uri" => "http://blubb.de", "w3c_css_service_uri" => "http://test.de")
9
9
  MarkupValidator.should_receive(:new).with(validator_uri: "http://blubb.de")
10
+ CSSValidator.should_receive(:new).with(validator_uri: "http://test.de")
10
11
 
11
12
  W3cRspecValidators::Validator.new
12
13
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: w3c_rspec_validators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-18 00:00:00.000000000 Z
12
+ date: 2012-03-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: w3c_validators
16
- requirement: &13643700 !ruby/object:Gem::Requirement
16
+ requirement: &19268480 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *13643700
24
+ version_requirements: *19268480
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &13641700 !ruby/object:Gem::Requirement
27
+ requirement: &19267980 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *13641700
35
+ version_requirements: *19267980
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rails
38
- requirement: &13639620 !ruby/object:Gem::Requirement
38
+ requirement: &19267560 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *13639620
46
+ version_requirements: *19267560
47
47
  description: At this time it allows for validation of html (including html5) produced
48
48
  by rspec acceptance tests. In addition you can configure the gem to use a locally
49
49
  installed validation service instead of the online w3c servers
@@ -61,9 +61,15 @@ files:
61
61
  - lib/rails/generators/w3c_rspec_validators/config/templates/w3c_rspec_validators.yml
62
62
  - lib/w3c_rspec_validators.rb
63
63
  - lib/w3c_rspec_validators/config.rb
64
+ - lib/w3c_rspec_validators/css_files.rb
65
+ - lib/w3c_rspec_validators/error_parser.rb
64
66
  - lib/w3c_rspec_validators/rspec.rb
65
67
  - lib/w3c_rspec_validators/validator.rb
66
68
  - lib/w3c_rspec_validators/version.rb
69
+ - spec/css_files_spec.rb
70
+ - spec/error_parser_spec.rb
71
+ - spec/fixtures/file.css
72
+ - spec/fixtures/file2.css
67
73
  - spec/matchers_spec.rb
68
74
  - spec/validator_spec.rb
69
75
  - w3c_rspec_validators.gemspec
@@ -92,3 +98,4 @@ signing_key:
92
98
  specification_version: 3
93
99
  summary: This gem adds w3c validation in form of rspec matchers
94
100
  test_files: []
101
+ has_rdoc: