assert_valid_markup 0.6.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/assert_valid_markup.rb +22 -8
  3. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.6.1
@@ -4,7 +4,7 @@ require 'digest/md5'
4
4
  require 'open-uri'
5
5
  require 'fileutils'
6
6
  require 'tempfile'
7
- require 'xmlsimple'
7
+ require 'json'
8
8
  require 'cgi'
9
9
 
10
10
  class Test::Unit::TestCase
@@ -30,6 +30,10 @@ class Test::Unit::TestCase
30
30
  #
31
31
  def assert_valid_markup(fragment=@response.body, options={})
32
32
  opts = @@default_avm_options.merge(options)
33
+
34
+ # html5 validation is a special case
35
+ opts[:validation_service] = :w3c if fragment =~ /<!DOCTYPE html>/
36
+
33
37
  result = ''
34
38
  if opts[:validation_service] == :local
35
39
  result = local_validate(fragment, opts[:dtd_validate], opts[:catalog_path])
@@ -59,12 +63,13 @@ class Test::Unit::TestCase
59
63
  end
60
64
 
61
65
  # Class-level method to to turn on validation for the response from any successful html request via "get"
62
- def self.assert_all_valid_markup
66
+ def self.assert_all_valid_markup(options={})
67
+ opts = @@default_avm_options.merge(options)
63
68
  self.class_eval do
64
69
  # automatically check markup for all successfull GETs
65
- def get_with_assert_valid_markup(*args)
70
+ define_method(:get_with_assert_valid_markup) do |*args|
66
71
  get_without_assert_valid_markup(*args)
67
- assert_valid_markup if ! @@skip_validation && @request.format.html? && @response.success?
72
+ assert_valid_markup(@response.body, opts) if ! @@skip_validation && @request.format.html? && @response.success?
68
73
  end
69
74
  alias_method_chain :get, :assert_valid_markup
70
75
  end
@@ -142,7 +147,7 @@ class Test::Unit::TestCase
142
147
  msg << l.gsub(/^[^:]*:/, "Invalid markup: line ")
143
148
  if l =~ /^[^:]*:(\d+)/
144
149
  line = $1.to_i
145
- ((line - 1)..(line + 1)).each do |ln|
150
+ ((line - 3)..(line + 3)).each do |ln|
146
151
  msg << "\t#{ln}: #{xmldata.lines.to_a[ln-1]}"
147
152
  end
148
153
  end
@@ -159,13 +164,22 @@ class Test::Unit::TestCase
159
164
  response = File.open filename {|f| Marshal.load(f) } unless ENV['NO_CACHE_VALIDATION'] rescue nil
160
165
  end
161
166
  if ! response
162
- response = Net::HTTP.start('validator.w3.org').post2('/check', "fragment=#{CGI.escape(fragment)}&output=xml")
167
+ response = Net::HTTP.start('validator.w3.org').post2('/check', "fragment=#{CGI.escape(fragment)}&output=json")
163
168
  File.open filename, 'w+' do |f| Marshal.dump response, f end
164
169
  end
165
170
  markup_is_valid = response['x-w3c-validator-status']=='Valid'
166
171
  if ! markup_is_valid
167
- doc = XmlSimple.xml_in(response.body)
168
- validation_result = doc['messages'][0]['msg'].collect{ |m| "Invalid markup: line #{m['line']}: #{CGI.unescapeHTML(m['content'])}" }.join("\n")
172
+ doc = JSON.parse(response.body)
173
+ msgs = []
174
+ doc['messages'].each do |m|
175
+ line = m['lastLine']
176
+ msg = "Invalid markup: line #{line}: #{CGI.unescapeHTML(m['message'])}\n"
177
+ ((line - 3)..(line + 3)).each do |ln|
178
+ msg << "\t#{ln}: #{fragment.lines.to_a[ln-1]}"
179
+ end
180
+ msgs << msg
181
+ end
182
+ validation_result = msgs.join("\n")
169
183
  end
170
184
  rescue SocketError
171
185
  # if we can't reach the validator service, just let the test pass
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
- - 0
9
- version: 0.6.0
8
+ - 1
9
+ version: 0.6.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Conway
@@ -54,7 +54,7 @@ dependencies:
54
54
  type: :development
55
55
  version_requirements: *id003
56
56
  - !ruby/object:Gem::Dependency
57
- name: xml-simple
57
+ name: json
58
58
  prerelease: false
59
59
  requirement: &id004 !ruby/object:Gem::Requirement
60
60
  requirements: