-
-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix test runs for optional dependencies #196
Changes from all commits
db5e8d5
337e23d
c475251
152742d
084e4fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,3 @@ source "https://rubygems.org" | |
gemspec :path => "../" | ||
|
||
gem "multi_json" | ||
|
||
group :development do | ||
gem "rake" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,3 @@ source "https://rubygems.org" | |
gemspec :path => "../" | ||
|
||
gem "uuidtools" | ||
|
||
group :development do | ||
gem "rake" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,3 @@ source "https://rubygems.org" | |
gemspec :path => "../" | ||
|
||
gem "yajl-ruby" | ||
|
||
group :development do | ||
gem "rake" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,7 +24,8 @@ class Validator | |
:errors_as_objects => false, | ||
:insert_defaults => false, | ||
:clear_cache => true, | ||
:strict => false | ||
:strict => false, | ||
:parse_data => true | ||
} | ||
@@validators = {} | ||
@@default_validator = nil | ||
|
@@ -400,7 +401,7 @@ def parse(s) | |
else | ||
case @@json_backend.to_s | ||
when 'json' | ||
JSON.parse(s) | ||
JSON.parse(s, :quirks_mode => true) | ||
when 'yajl' | ||
json = StringIO.new(s) | ||
parser = Yajl::Parser.new | ||
|
@@ -550,20 +551,22 @@ def initialize_schema(schema) | |
|
||
|
||
def initialize_data(data) | ||
if @options[:json] | ||
data = JSON::Validator.parse(data) | ||
elsif @options[:uri] | ||
json_uri = normalized_uri(data) | ||
data = JSON::Validator.parse(custom_open(json_uri)) | ||
elsif data.is_a?(String) | ||
begin | ||
if @options[:parse_data] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you make this an early return to simplify the rightward drift? eg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The diff is a bit unfortunate - this method is actually returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah I just totally overlooked that, alright. |
||
if @options[:json] | ||
data = JSON::Validator.parse(data) | ||
rescue | ||
elsif @options[:uri] | ||
json_uri = normalized_uri(data) | ||
data = JSON::Validator.parse(custom_open(json_uri)) | ||
elsif data.is_a?(String) | ||
begin | ||
json_uri = normalized_uri(data) | ||
data = JSON::Validator.parse(custom_open(json_uri)) | ||
data = JSON::Validator.parse(data) | ||
rescue | ||
# Silently discard the error - the data will not change | ||
begin | ||
json_uri = normalized_uri(data) | ||
data = JSON::Validator.parse(custom_open(json_uri)) | ||
rescue | ||
# Silently discard the error - the data will not change | ||
end | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
require 'webmock' | ||
require 'socket' | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
|
||
class InitializeDataTest < Minitest::Test | ||
|
||
def refute_block(&blk) | ||
result = blk.call | ||
refute(result) | ||
end | ||
|
||
def test_parse_character_string | ||
schema = {'type' => 'string'} | ||
data = 'hello world' | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
refute_block do | ||
begin | ||
JSON::Validator.validate(schema, data, :json => true) | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_integer_string | ||
schema = {'type' => 'integer'} | ||
data = '42' | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
refute(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :json => true)) | ||
|
||
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_hash_string | ||
schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } } | ||
data = '{"a": "b"}' | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
refute(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :json => true)) | ||
|
||
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_json_string | ||
schema = {'type' => 'string'} | ||
data = '"hello world"' | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :json => true)) | ||
|
||
assert_raises(Errno::ENOENT) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_valid_uri_string | ||
schema = {'type' => 'string'} | ||
data = 'http://foo.bar/' | ||
|
||
stub_request(:get, "foo.bar").to_return(:body => '"hello world"', :status => 200) | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
refute_block do | ||
begin | ||
JSON::Validator.validate(schema, data, :json => true) | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
assert(JSON::Validator.validate(schema, data, :uri => true)) | ||
end | ||
|
||
def test_parse_invalid_uri_string | ||
schema = {'type' => 'string'} | ||
data = 'http://foo.bar/' | ||
|
||
stub_request(:get, "foo.bar").to_timeout | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
refute_block do | ||
begin | ||
JSON::Validator.validate(schema, data, :json => true) | ||
rescue | ||
false | ||
end | ||
end | ||
|
||
assert_raises(Timeout::Error) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_integer | ||
schema = {'type' => 'integer'} | ||
data = 42 | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) } | ||
|
||
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
|
||
def test_parse_hash | ||
schema = { 'type' => 'object', 'properties' => { 'a' => { 'type' => 'string' } } } | ||
data = { 'a' => 'b' } | ||
|
||
assert(JSON::Validator.validate(schema, data)) | ||
|
||
assert(JSON::Validator.validate(schema, data, :parse_data => false)) | ||
|
||
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :json => true) } | ||
|
||
assert_raises(TypeError) { JSON::Validator.validate(schema, data, :uri => true) } | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should generally start leaving these off in the code examples, it's just noisy. It's safe to assume that people will know to require the library they're using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that's a good point.
Let me raise an issue so we don't forget