CodeMonkeySteve-assert_valid_content 0.1 → 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +0 -1
- data/lib/assert_valid_content.rb +13 -13
- data/lib/assert_valid_content/libxml.rb +19 -13
- data/lib/assert_valid_content/validator.rb +34 -34
- data/lib/assert_valid_content/w3c-net.rb +14 -10
- metadata +12 -3
- data/lib/assert_valid_content/assertions.rb +0 -64
data/README.rdoc
CHANGED
data/lib/assert_valid_content.rb
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
require '
|
1
|
+
require 'assert_valid_content/validator'
|
2
2
|
|
3
|
+
require 'tmpdir'
|
3
4
|
module AssertValidContent
|
4
5
|
TempDir = if defined? RAILS_ROOT
|
5
6
|
File.join RAILS_ROOT, 'tmp/validate'
|
6
7
|
else
|
7
8
|
File.join Dir.tmpdir, 'assert_valid_content'
|
8
9
|
end
|
9
|
-
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
AssertValidContent::LibXML::XML. validates Mime::XML
|
15
|
-
end
|
11
|
+
module Assertions
|
12
|
+
require 'assert_valid_content/libxml.rb'
|
13
|
+
include AssertValidContent::LibXML::Assertions if AssertValidContent::LibXML::Installed
|
16
14
|
|
17
|
-
require 'assert_valid_content/w3c-net.rb'
|
18
|
-
if AssertValidContent::W3C::Installed
|
19
|
-
|
15
|
+
require 'assert_valid_content/w3c-net.rb'
|
16
|
+
include AssertValidContent::W3C::Assertions if AssertValidContent::W3C::Installed
|
17
|
+
end
|
20
18
|
end
|
21
19
|
|
22
|
-
require '
|
20
|
+
require 'test/unit'
|
23
21
|
if defined? Test::Unit::TestCase
|
24
|
-
module Test # :nodoc:all
|
25
|
-
class
|
22
|
+
module Test::Unit # :nodoc:all
|
23
|
+
class TestCase
|
24
|
+
include AssertValidContent::Assertions
|
25
|
+
end
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
@@ -1,24 +1,18 @@
|
|
1
|
-
require '
|
1
|
+
require 'libxml'
|
2
2
|
|
3
|
-
# Validates using LibXML via the libxml-ruby[http://libxml.rubyforge.org/] gem.
|
3
|
+
# Validates using LibXML via the libxml-ruby[http://libxml.rubyforge.org/] gem.
|
4
4
|
module AssertValidContent::LibXML
|
5
5
|
|
6
|
-
|
7
|
-
Installed = begin
|
8
|
-
gem( 'libxml-ruby', '~> 1.1' )
|
9
|
-
require 'libxml'
|
10
|
-
true
|
11
|
-
rescue Gem::LoadError
|
12
|
-
false
|
13
|
-
end
|
6
|
+
Installed = true
|
14
7
|
|
15
8
|
# Validates HTML and XHTML
|
16
|
-
class
|
17
|
-
def validate( content ) #:nodoc:
|
9
|
+
class HTMLValidator < AssertValidContent::Validator
|
10
|
+
def validate!( content ) #:nodoc:
|
18
11
|
::LibXML::XML::Error.set_handler { |err| @errors << err }
|
19
12
|
begin
|
20
13
|
doc = self.parser( content ).parse
|
21
14
|
rescue ::LibXML::XML::Error ; end
|
15
|
+
@errors.empty?
|
22
16
|
end
|
23
17
|
|
24
18
|
protected
|
@@ -27,12 +21,24 @@ protected
|
|
27
21
|
end
|
28
22
|
end
|
29
23
|
|
24
|
+
module Assertions
|
25
|
+
def assert_valid_html( *srcs, &blk )
|
26
|
+
assert_valid_content ::AssertValidContent::LibXML::HTMLValidator, *srcs, &blk
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
30
|
# Validates XML
|
31
|
-
class
|
31
|
+
class XMLValidator < HTMLValidator
|
32
32
|
protected
|
33
33
|
def parser( content ) #:nodoc:
|
34
34
|
::LibXML::XML::Parser.string content
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
module Assertions
|
39
|
+
def assert_valid_xml( *srcs, &blk )
|
40
|
+
assert_valid_content ::AssertValidContent::LibXML::XMLValidator, *srcs, &blk
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
38
44
|
end
|
@@ -3,53 +3,53 @@ require 'pathname'
|
|
3
3
|
|
4
4
|
module AssertValidContent # :nodoc
|
5
5
|
|
6
|
-
# Hash of +type+ to sets of Validator objects for that type.
|
7
|
-
def self.validators ; @@validators ; end
|
8
|
-
@@validators = Hash.new { |h, k| h[k] = Set.new }
|
9
|
-
|
10
|
-
# Returns +true+ if the content from +src+ is valid for type +type+. Yields on failure.
|
11
|
-
# +src+ is one of +String+, +Pathname+, or +IO+ object
|
12
|
-
def self.valid_content?( type, src ) # :yields: validator
|
13
|
-
validators = AssertValidContent.validators[type]
|
14
|
-
raise "No validators for type: #{type}" if validators.empty?
|
15
|
-
|
16
|
-
content = case src
|
17
|
-
when String then src
|
18
|
-
when Pathname then IO.read src
|
19
|
-
when IO then src.read
|
20
|
-
else raise "Unknown content src type: #{src.class}"
|
21
|
-
end
|
22
|
-
|
23
|
-
val = nil
|
24
|
-
valid = validators.all? do |cl|
|
25
|
-
val = cl.new
|
26
|
-
val.validate content
|
27
|
-
val.errors.empty?
|
28
|
-
end
|
29
|
-
|
30
|
-
yield(val) if block_given? and not valid
|
31
|
-
valid
|
32
|
-
end
|
33
|
-
|
34
6
|
# Base class for all content validators
|
35
7
|
class Validator
|
36
8
|
# Validation errors
|
37
9
|
attr_reader :errors
|
38
10
|
|
39
|
-
def initialize #:nodoc:
|
40
|
-
@errors = []
|
11
|
+
def initialize( opts = {} ) #:nodoc:
|
12
|
+
@src, @errors = nil, []
|
41
13
|
end
|
42
14
|
|
43
15
|
# Returns an error message listing validation errors (or +nil+ if there were none).
|
44
16
|
def to_s
|
45
17
|
return nil unless @errors
|
46
|
-
"#{self.class.name} validation failed:\n" +
|
18
|
+
"#{self.class.name} validation failed:\n" +
|
19
|
+
@errors.map { |e| " #{e.to_s}" }.join("\n")
|
20
|
+
end
|
21
|
+
|
22
|
+
def validate( src, &blk )
|
23
|
+
@src = src
|
24
|
+
content = case src
|
25
|
+
when Pathname then IO.read src
|
26
|
+
when IO then src.read
|
27
|
+
else src
|
28
|
+
end
|
29
|
+
validate! content, &blk
|
47
30
|
end
|
48
31
|
|
49
32
|
protected
|
50
|
-
|
51
|
-
|
52
|
-
|
33
|
+
def validate!( content, &blk )
|
34
|
+
raise NotImplementedError
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
module Assertions
|
39
|
+
def assert_valid_content ( vclass, *args, &blk )
|
40
|
+
opts = (Hash === args.last) ? args.pop : {}
|
41
|
+
if args.empty?
|
42
|
+
if defined? RAILS_ROOT
|
43
|
+
args = [ @controller.response.body ]
|
44
|
+
else
|
45
|
+
raise ArgumentError, 'wrong number of arguments (0 for 1 or more)'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
validator = vclass.new opts
|
50
|
+
args.each do |src|
|
51
|
+
assert validator.validate( *args, &blk )
|
52
|
+
end
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -1,8 +1,8 @@
|
|
1
|
-
require 'assert_valid_content/validator'
|
2
1
|
require 'md5'
|
3
2
|
require 'fileutils'
|
4
3
|
require 'cgi'
|
5
4
|
require 'net/http'
|
5
|
+
require 'libxml'
|
6
6
|
|
7
7
|
# Validates using W3C Web Service(s)
|
8
8
|
module AssertValidContent::W3C
|
@@ -12,13 +12,12 @@ Installed = ENV['NONET'].to_i.zero?
|
|
12
12
|
|
13
13
|
# Validates via the W3C CSS validator service[http://jigsaw.w3.org/css-validator/]
|
14
14
|
# (taken from assert-valid-asset[http://github.com/CodeMonkeySteve/assert-valid-asset]).
|
15
|
-
class
|
16
|
-
|
15
|
+
class CSSValidator < AssertValidContent::Validator
|
17
16
|
SrvHost = ENV['CSS_VALIDATOR_HOST'] || 'jigsaw.w3.org'
|
18
17
|
SrvPath = ENV['CSS_VALIDATOR_PATH'] || '/css-validator/validator'
|
19
18
|
TempDir = File.join AssertValidContent::TempDir, 'w3c-net'
|
20
19
|
|
21
|
-
def validate( content ) #:nodoc:
|
20
|
+
def validate!( content ) #:nodoc:
|
22
21
|
FileUtils.mkdir_p TempDir
|
23
22
|
|
24
23
|
md5 = MD5.md5( content ).to_s
|
@@ -42,13 +41,13 @@ class CSS < AssertValidContent::Validator
|
|
42
41
|
File.open( resp_path, 'w+' ) { |f| f.write resp }
|
43
42
|
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
rescue REXML::ParseException
|
44
|
+
doc = LibXML::XML::HTMLParser.string(resp).parse
|
45
|
+
doc.find("//tr[@class='error']").each do |err|
|
46
|
+
lineno = err.find_first("td[@class='linenumber']/text()").content.to_i
|
47
|
+
msg = err.find_first("td[@class='parse-error']/text()").content.strip
|
48
|
+
@errors << "Invalid CSS (#{@src.inspect} line #{lineno}: #{msg}"
|
51
49
|
end
|
50
|
+
@errors.empty?
|
52
51
|
end
|
53
52
|
|
54
53
|
def text_to_multipart(key,value) #:nodoc:
|
@@ -67,7 +66,12 @@ class CSS < AssertValidContent::Validator
|
|
67
66
|
Net::HTTP
|
68
67
|
end
|
69
68
|
end
|
69
|
+
end
|
70
70
|
|
71
|
+
module Assertions
|
72
|
+
def assert_valid_css( *srcs, &blk )
|
73
|
+
assert_valid_content ::AssertValidContent::W3C::CSSValidator, *srcs, &blk
|
74
|
+
end
|
71
75
|
end
|
72
76
|
|
73
77
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: CodeMonkeySteve-assert_valid_content
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: "0.
|
4
|
+
version: "0.2"
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Steve Sloan
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-06-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -22,6 +22,16 @@ dependencies:
|
|
22
22
|
- !ruby/object:Gem::Version
|
23
23
|
version: "0"
|
24
24
|
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: ruby-libxml
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
25
35
|
description: Provides test assertions for the validity of various content types (i.e. HTML, XML CSS, etc).
|
26
36
|
email: steve@finagle.org
|
27
37
|
executables: []
|
@@ -35,7 +45,6 @@ files:
|
|
35
45
|
- MIT-LICENSE
|
36
46
|
- lib/assert_valid_content.rb
|
37
47
|
- lib/assert_valid_content/validator.rb
|
38
|
-
- lib/assert_valid_content/assertions.rb
|
39
48
|
- lib/assert_valid_content/libxml.rb
|
40
49
|
- lib/assert_valid_content/w3c-net.rb
|
41
50
|
has_rdoc: true
|
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'assert_valid_content/validator'
|
2
|
-
|
3
|
-
module AssertValidContent
|
4
|
-
|
5
|
-
# Test case assertions
|
6
|
-
module Assertions
|
7
|
-
|
8
|
-
# Asserts that the content (from one ore more +srcs+) is valid for type +type+.
|
9
|
-
# Each of +srcs+ is one of +String+, +Pathname+, or +IO+ object.
|
10
|
-
def assert_valid_content( type, *srcs )
|
11
|
-
msg = nil
|
12
|
-
for src in srcs
|
13
|
-
res = AssertValidContent.valid_content?(type, src) { |val| msg = val.to_s }
|
14
|
-
assert_block( msg ) { res }
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
alias_method :orig_method_missing, :method_missing #:nodoc:
|
19
|
-
|
20
|
-
# Implements the +assert_valid_TYPE+(*+srcs+) dynamic assertions, where +TYPE+ is a MIME type name
|
21
|
-
# (e.g. +html+, +css+, etc.) and each of +srcs+ one of +String+, +Pathname+, or +IO+ object.
|
22
|
-
def method_missing( name, *args )
|
23
|
-
n = name.to_s
|
24
|
-
return orig_method_missing( name, *args ) unless
|
25
|
-
(n[0..12] == 'assert_valid_') and (type = Mime::Type.lookup_by_extension n[13..-1])
|
26
|
-
|
27
|
-
if args.empty? and self.respond_to?(:response) and self.response.respond_to(:body)
|
28
|
-
args = [ self.response.body ]
|
29
|
-
end
|
30
|
-
|
31
|
-
assert_valid_content type, *args
|
32
|
-
end
|
33
|
-
|
34
|
-
module ClassMethods
|
35
|
-
alias_method :orig_method_missing, :method_missing #:nodoc:
|
36
|
-
|
37
|
-
# Implements the class-level +assert_valid_TYPE+(*+srcs+) dynamic assertions. See Assertions.method_missing.
|
38
|
-
def method_missing( name, *args )
|
39
|
-
n = name.to_s
|
40
|
-
return orig_method_missing( name, *args ) unless
|
41
|
-
(n[0..12] == 'assert_valid_') and (type = Mime::Type.lookup_by_extension n[13..-1])
|
42
|
-
|
43
|
-
self.assert_valid_static_content << [ type, *args ]
|
44
|
-
end
|
45
|
-
|
46
|
-
def assert_valid_static_content #:nodoc:
|
47
|
-
@assert_valid_static_content ||= []
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.included( base ) #:nodoc:
|
52
|
-
base.extend Assertions::ClassMethods
|
53
|
-
end
|
54
|
-
|
55
|
-
# Executes class-level assertions (i.e. static content).
|
56
|
-
def test__assert_valid_static_content #:nodoc:
|
57
|
-
for args in self.class.assert_valid_static_content
|
58
|
-
assert_valid_content *args
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
end
|