-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wedge JSON::Schema::Loader into place in Validator
- Loading branch information
Showing
4 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require File.expand_path('../test_helper', __FILE__) | ||
|
||
class TestValidator < Minitest::Test | ||
|
||
class MockLoader | ||
def load(location) | ||
schema = { | ||
'$schema' => 'http://json-schema.org/draft-04/schema#', | ||
'type' => 'string', | ||
'minLength' => 2 | ||
} | ||
|
||
JSON::Schema.new(schema, Addressable::URI.parse(location.to_s)) | ||
end | ||
end | ||
|
||
def teardown | ||
# Hacky. Not sure of a better tactic for resetting just yet. | ||
JSON::Validator.send(:class_variable_set, :@@schema_loader, nil) | ||
end | ||
|
||
def test_default_schema_loader | ||
loader = JSON::Validator.schema_loader | ||
assert loader.accept_uri?(Addressable::URI.parse('http://example.com')) | ||
assert loader.accept_file?(Pathname.new('/etc/passwd')) | ||
end | ||
|
||
def test_set_default_schema_loader | ||
JSON::Validator.schema_loader = MockLoader.new | ||
|
||
schema = { '$ref' => 'http://any.url/at/all' } | ||
assert_valid schema, 'abc' | ||
refute_valid schema, 'a' | ||
end | ||
|
||
def test_validate_with_loader | ||
loader = MockLoader.new | ||
schema = { '$ref' => 'http://any.url/at/all' } | ||
assert_valid schema, 'abc', :schema_loader => loader | ||
refute_valid schema, 'a', :schema_loader => loader | ||
end | ||
|
||
def test_validate_list_with_loader | ||
loader = MockLoader.new | ||
schema = { '$ref' => 'http://what.ever/schema' } | ||
assert_valid schema, ['abc', 'def'], :schema_loader => loader, :list => true | ||
refute_valid schema, ['abc', 'a'], :schema_loader => loader, :list => true | ||
end | ||
|
||
end |