-
-
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.
Merge pull request #168 from pd/test-helpers
Add some test helpers for common patterns
- Loading branch information
Showing
35 changed files
with
1,017 additions
and
3,239 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ rvm: | |
- "2.1" | ||
- "jruby" | ||
- "rbx" | ||
install: | ||
- bundle install --retry=3 | ||
matrix: | ||
include: | ||
- rvm: "2.1" | ||
|
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,108 @@ | ||
module ArrayValidation | ||
module ItemsTests | ||
def test_items_single_schema | ||
schema = { 'items' => { 'type' => 'string' } } | ||
|
||
assert_valid schema, [] | ||
assert_valid schema, ['a'] | ||
assert_valid schema, ['a', 'b'] | ||
|
||
refute_valid schema, [1] | ||
refute_valid schema, ['a', 1] | ||
|
||
# other types are disregarded | ||
assert_valid schema, {'a' => 'foo'} | ||
end | ||
|
||
def test_items_multiple_schemas | ||
schema = { | ||
'items' => [ | ||
{ 'type' => 'string' }, | ||
{ 'type' => 'integer' } | ||
] | ||
} | ||
|
||
assert_valid schema, ['b', 1] | ||
assert_valid schema, ['b', 1, nil] | ||
refute_valid schema, [1, 'b'] | ||
refute_valid schema, [] | ||
end | ||
|
||
def test_minitems | ||
schema = { 'minItems' => 1 } | ||
|
||
assert_valid schema, [1] | ||
assert_valid schema, [1, 2] | ||
refute_valid schema, [] | ||
|
||
# other types are disregarded | ||
assert_valid schema, 5 | ||
end | ||
|
||
def test_maxitems | ||
schema = { 'maxItems' => 1 } | ||
|
||
assert_valid schema, [] | ||
assert_valid schema, [1] | ||
refute_valid schema, [1, 2] | ||
|
||
# other types are disregarded | ||
assert_valid schema, 5 | ||
end | ||
end | ||
|
||
module AdditionalItemsTests | ||
def test_additional_items_false | ||
schema = { | ||
'items' => [ | ||
{ 'type' => 'integer' }, | ||
{ 'type' => 'string' } | ||
], | ||
'additionalItems' => false | ||
} | ||
|
||
assert_valid schema, [1, 'string'] | ||
refute_valid schema, [1, 'string', 2] | ||
refute_valid schema, ['string', 1] | ||
end | ||
|
||
def test_additional_items_schema | ||
schema = { | ||
'items' => [ | ||
{ 'type' => 'integer' }, | ||
{ 'type' => 'string' } | ||
], | ||
'additionalItems' => { 'type' => 'integer' } | ||
} | ||
|
||
assert_valid schema, [1, 'string'] | ||
assert_valid schema, [1, 'string', 2] | ||
refute_valid schema, [1, 'string', 'string'] | ||
end | ||
end | ||
|
||
module UniqueItemsTests | ||
def test_unique_items | ||
schema = { 'uniqueItems' => true } | ||
|
||
assert_valid schema, [nil, 5] | ||
refute_valid schema, [nil, nil] | ||
|
||
assert_valid schema, [true, false] | ||
refute_valid schema, [true, true] | ||
|
||
assert_valid schema, [4, 4.1] | ||
refute_valid schema, [4, 4] | ||
|
||
assert_valid schema, ['a', 'ab'] | ||
refute_valid schema, ['a', 'a'] | ||
|
||
assert_valid schema, [[1], [2]] | ||
refute_valid schema, [[1], [1]] | ||
|
||
assert_valid schema, [{'b' => 1}, {'c' => 2}] | ||
assert_valid schema, [{'b' => 1}, {'c' => 1}] | ||
refute_valid schema, [{'b' => 1}, {'b' => 1}] | ||
end | ||
end | ||
end |
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,93 @@ | ||
module NumberValidation | ||
module MinMaxTests | ||
def test_minimum | ||
schema = { | ||
'properties' => { | ||
'a' => { 'minimum' => 5 } | ||
} | ||
} | ||
|
||
assert_valid schema, {'a' => 5} | ||
assert_valid schema, {'a' => 6} | ||
|
||
refute_valid schema, {'a' => 4} | ||
refute_valid schema, {'a' => 4.99999} | ||
|
||
# other types are disregarded | ||
assert_valid schema, {'a' => 'str'} | ||
end | ||
|
||
def test_exclusive_minimum | ||
schema = { | ||
'properties' => { | ||
'a' => { 'minimum' => 5 }.merge(exclusive_minimum) | ||
} | ||
} | ||
|
||
assert_valid schema, {'a' => 6} | ||
assert_valid schema, {'a' => 5.0001} | ||
refute_valid schema, {'a' => 5} | ||
end | ||
|
||
def test_maximum | ||
schema = { | ||
'properties' => { | ||
'a' => { 'maximum' => 5 } | ||
} | ||
} | ||
|
||
assert_valid schema, {'a' => 4} | ||
assert_valid schema, {'a' => 5} | ||
|
||
refute_valid schema, {'a' => 6} | ||
refute_valid schema, {'a' => 5.0001} | ||
end | ||
|
||
def test_exclusive_maximum | ||
schema = { | ||
'properties' => { | ||
'a' => { 'maximum' => 5 }.merge(exclusive_maximum) | ||
} | ||
} | ||
|
||
assert_valid schema, {'a' => 4} | ||
assert_valid schema, {'a' => 4.99999} | ||
refute_valid schema, {'a' => 5} | ||
end | ||
end | ||
|
||
# draft3 introduced `divisibleBy`, renamed to `multipleOf` in draft4. | ||
# Favor the newer name, but the behavior should be identical. | ||
module MultipleOfTests | ||
def multiple_of | ||
'multipleOf' | ||
end | ||
|
||
def test_multiple_of | ||
schema = { | ||
'properties' => { | ||
'a' => { multiple_of => 1.1 } | ||
} | ||
} | ||
|
||
assert_valid schema, {'a' => 0} | ||
|
||
assert_valid schema, {'a' => 2.2} | ||
refute_valid schema, {'a' => 3.4} | ||
|
||
# other types are disregarded | ||
assert_valid schema, {'a' => 'hi'} | ||
end | ||
|
||
def test_multiple_of_zero | ||
schema = { | ||
'properties' => { | ||
'a' => { multiple_of => 0 } | ||
} | ||
} | ||
|
||
refute_valid schema, {'a' => 5} | ||
refute_valid schema, {'a' => 0} | ||
end | ||
end | ||
end |
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,68 @@ | ||
module ObjectValidation | ||
module AdditionalPropertiesTests | ||
def test_additional_properties_false | ||
schema = { | ||
'properties' => { | ||
'a' => { 'type' => 'integer' } | ||
}, | ||
'additionalProperties' => false | ||
} | ||
|
||
assert_valid schema, {'a' => 1} | ||
refute_valid schema, {'a' => 1, 'b' => 2} | ||
end | ||
|
||
def test_additional_properties_schema | ||
schema = { | ||
'properties' => { | ||
'a' => { 'type' => 'integer' } | ||
}, | ||
'additionalProperties' => { 'type' => 'string' } | ||
} | ||
|
||
assert_valid schema, {'a' => 1} | ||
assert_valid schema, {'a' => 1, 'b' => 'hi'} | ||
refute_valid schema, {'a' => 1, 'b' => 2} | ||
end | ||
end | ||
|
||
module PatternPropertiesTests | ||
def test_pattern_properties | ||
schema = { | ||
'patternProperties' => { | ||
"\\d+ taco" => { 'type' => 'integer' } | ||
} | ||
} | ||
|
||
assert_valid schema, {'1 taco' => 1, '20 taco' => 20} | ||
assert_valid schema, {'foo' => true, '1 taco' => 1} | ||
refute_valid schema, {'1 taco' => 'yum'} | ||
end | ||
|
||
def test_pattern_properties_additional_properties_false | ||
schema = { | ||
'patternProperties' => { | ||
"\\d+ taco" => { 'type' => 'integer' } | ||
}, | ||
'additionalProperties' => false | ||
} | ||
|
||
assert_valid schema, {'1 taco' => 1} | ||
refute_valid schema, {'1 taco' => 'yum'} | ||
refute_valid schema, {'1 taco' => 1, 'foo' => true} | ||
end | ||
|
||
def test_pattern_properties_additional_properties_schema | ||
schema = { | ||
'patternProperties' => { | ||
"\\d+ taco" => { 'type' => 'integer' } | ||
}, | ||
'additionalProperties' => { 'type' => 'string' } | ||
} | ||
|
||
assert_valid schema, {'1 taco' => 1} | ||
assert_valid schema, {'1 taco' => 1, 'foo' => 'bar'} | ||
refute_valid schema, {'1 taco' => 1, 'foo' => 2} | ||
end | ||
end | ||
end |
Oops, something went wrong.