Skip to content

Commit

Permalink
rubocop: cleanup whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
bastelfreak committed Jul 12, 2024
1 parent 3859a29 commit 640a4c6
Show file tree
Hide file tree
Showing 35 changed files with 428 additions and 69 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require:
- rubocop-rake
- rubocop-rspec
- rubocop-performance
- rubocop-minitest

AllCops:
TargetRubyVersion: 2.7
NewCops: enable
Expand Down
26 changes: 16 additions & 10 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2023-02-18 18:00:43 UTC using RuboCop version 1.45.1.
# on 2023-02-18 18:13:01 UTC using RuboCop version 1.45.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 2
# Offense count: 3
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: TreatCommentsAsGroupSeparators, ConsiderPunctuation, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Expand Down Expand Up @@ -38,14 +38,6 @@ Layout/MultilineMethodCallBraceLayout:
Exclude:
- 'test/common_test_suite_test.rb'

# Offense count: 8
# This cop supports safe autocorrection (--autocorrect).
# Configuration parameters: EnforcedStyleForEmptyBraces.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBraces: space, no_space
Layout/SpaceInsideHashLiteralBraces:
EnforcedStyle: space

# Offense count: 1
# Configuration parameters: AllowedMethods, AllowedPatterns.
Lint/AmbiguousBlockAssociation:
Expand Down Expand Up @@ -184,6 +176,20 @@ Metrics/ParameterLists:
Metrics/PerceivedComplexity:
Max: 20

# Offense count: 63
Minitest/MultipleAssertions:
Max: 25

# Offense count: 53
# This cop supports unsafe autocorrection (--autocorrect-all).
Minitest/RefuteFalse:
Exclude:
- 'test/custom_format_test.rb'
- 'test/draft3_test.rb'
- 'test/draft4_test.rb'
- 'test/schema_validation_test.rb'
- 'test/support/strict_validation.rb'

# Offense count: 1
Naming/AccessorMethodName:
Exclude:
Expand Down
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ group :tests do
gem 'rubocop-rspec', '~> 2.18.1'
gem 'rubocop-rake', '~> 0.6.0'
gem 'rubocop-performance', '~> 1.16.0'
gem 'rubocop-minitest', '~> 0.27.0'
end
2 changes: 2 additions & 0 deletions test/all_of_ref_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def test_all_of_ref_schema_succeeds
def test_all_of_ref_subschema_errors
errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true)
nested_errors = errors[0][:errors]

assert_equal([:allof_0], nested_errors.keys, 'should have nested errors for each allOf subschema')
assert_match(%r{the property '#/name' of type string did not match the following type: integer}i, nested_errors[:allof_0][0][:message])
end
Expand All @@ -30,6 +31,7 @@ def test_all_of_ref_message
- allOf #0:
- The property '#/name' of type string did not match the following type: integer"''

assert_equal(expected_message, errors[0])
end

Expand Down
2 changes: 2 additions & 0 deletions test/any_of_ref_schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def test_any_of_ref_subschema_errors
data = %({"names": ["jack"]})
errors = JSON::Validator.fully_validate(schema, data, errors_as_objects: true)
nested_errors = errors[0][:errors]

assert_equal(%i[anyof_0 anyof_1 anyof_2], nested_errors.keys, 'should have nested errors for each anyOf subschema')
assert_match(%r{the property '#/names/0' value "jack" did not match the regex 'john'}i, nested_errors[:anyof_0][0][:message])
assert_match(%r{the property '#/names/0' value "jack" did not match the regex 'jane'}i, nested_errors[:anyof_1][0][:message])
Expand All @@ -30,6 +31,7 @@ def test_any_of_ref_message
- The property '#/names/0' value \"jack\" did not match the regex 'jane'
- anyOf #2:
- The property '#/names/0' value \"jack\" did not match the regex 'jimmy'"''

assert_equal(expected_message, errors[0])
end
end
6 changes: 6 additions & 0 deletions test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ def teardown

def test_caching
set_schema('type' => 'string')

assert_valid(schema_path, 'foo', clear_cache: false)

set_schema('type' => 'number')

refute_valid(schema_path, 123)
end

def test_clear_cache
set_schema('type' => 'string')

assert_valid(schema_path, 'foo', clear_cache: true)

set_schema('type' => 'number')

assert_valid(schema_path, 123)
end

Expand All @@ -34,9 +38,11 @@ def test_cache_schemas
end

set_schema('type' => 'string')

assert_valid(schema_path, 'foo', clear_cache: false)

set_schema('type' => 'number')

assert_valid(schema_path, 123)
ensure
suppress_warnings do
Expand Down
1 change: 1 addition & 0 deletions test/common_test_suite_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def self.skip?(current_test, file_path)
validate_schema: true,
version: version,
)

assert_equal t['valid'], errors.empty?, "Common test suite case failed: #{err_id}"
end
end
Expand Down
25 changes: 18 additions & 7 deletions test/custom_format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,40 @@ def setup

def test_single_registration
@all_versions.each do |version|
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' for #{version || 'default'} should be nil")
assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' for #{version || 'default'} should be nil")
JSON::Validator.register_format_validator('custom', @format_proc, [version])

assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}")
(@all_versions - [version]).each do |other_version|
assert(JSON::Validator.validator_for_name(other_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{other_version || 'default'}")
assert_nil(JSON::Validator.validator_for_name(other_version).formats['custom'], "Format 'custom' should still be nil for #{other_version || 'default'}")
end
JSON::Validator.deregister_format_validator('custom', [version])
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should be deregistered for #{version || 'default'}")

assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' should be deregistered for #{version || 'default'}")
end
end

def test_register_for_all_by_default
JSON::Validator.register_format_validator('custom', @format_proc)

@all_versions.each do |version|
assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}")
end
JSON::Validator.restore_default_formats

@all_versions.each do |version|
assert(JSON::Validator.validator_for_name(version).formats['custom'].nil?, "Format 'custom' should still be nil for #{version || 'default'}")
assert_nil(JSON::Validator.validator_for_name(version).formats['custom'], "Format 'custom' should still be nil for #{version || 'default'}")
end
end

def test_multi_registration
unregistered_version = @all_versions.delete('draft1')
JSON::Validator.register_format_validator('custom', @format_proc, @all_versions)

@all_versions.each do |version|
assert(JSON::Validator.validator_for_name(version).formats['custom'].is_a?(JSON::Schema::CustomFormat), "Format 'custom' should be registered for #{version || 'default'}")
end
assert(JSON::Validator.validator_for_name(unregistered_version).formats['custom'].nil?, "Format 'custom' should still be nil for #{unregistered_version}")
assert_nil(JSON::Validator.validator_for_name(unregistered_version).formats['custom'], "Format 'custom' should still be nil for #{unregistered_version}")
end

def test_format_validation
Expand All @@ -81,18 +86,22 @@ def test_format_validation

JSON::Validator.register_format_validator('custom', @format_proc, [version])
data['a'] = '42'

assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds with 'custom' format validator and correct data")

data['a'] = '23'

assert(!JSON::Validator.validate(schema, data), "#{prefix} fails with 'custom' format validator and wrong data")

errors = JSON::Validator.fully_validate(schema, data)
assert_equal(errors.count, 1)

assert_equal(1, errors.count)
assert_match(%r{The property '#/a' must be 42 in schema}, errors.first, "#{prefix} records format error")

data['a'] = 23
errors = JSON::Validator.fully_validate(schema, data)
assert_equal(errors.count, 1)

assert_equal(1, errors.count)
assert_match(%r{The property '#/a' of type integer did not match the following type: string}i, errors.first, "#{prefix} records no format error on type mismatch")
end
end
Expand All @@ -109,6 +118,7 @@ def test_override_default_format
assert(JSON::Validator.validate(schema, data), "#{prefix} succeeds for default format with correct data")

data['a'] = 'no_ip6_address'

assert(!JSON::Validator.validate(schema, data), "#{prefix} fails for default format and wrong data")

data['a'] = '42'
Expand All @@ -117,6 +127,7 @@ def test_override_default_format

JSON::Validator.deregister_format_validator('ipv6', [version])
data['a'] = '2001:db8:85a3:0:0:8a2e:370:7334'

assert(JSON::Validator.validate(schema, data), "#{prefix} restores the default format on deregistration")
end
end
Expand Down
12 changes: 12 additions & 0 deletions test/draft1_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def test_optional

refute_valid schema, data
data['a'] = 'Hello'

assert_valid schema, data

schema = {
Expand All @@ -52,6 +53,7 @@ def test_optional
}

data = {}

assert_valid schema, data
end

Expand All @@ -68,20 +70,25 @@ def test_max_decimal
}

data['a'] = 3.35

assert_valid schema, data

data['a'] = 3.455

refute_valid schema, data

schema['properties']['a']['maxDecimal'] = 0

data['a'] = 4.0

refute_valid schema, data

data['a'] = 'boo'

assert_valid schema, data

data['a'] = 5

assert_valid schema, data
end

Expand All @@ -98,19 +105,24 @@ def test_disallow
}

data['a'] = 'string'

assert_valid schema, data

data['a'] = 5

refute_valid schema, data

schema['properties']['a']['disallow'] = %w[integer string]
data['a'] = 'string'

refute_valid schema, data

data['a'] = 5

refute_valid schema, data

data['a'] = false

assert_valid schema, data
end

Expand Down
7 changes: 7 additions & 0 deletions test/draft2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def test_optional

refute_valid schema, data
data['a'] = 'Hello'

assert_valid schema, data

schema = {
Expand All @@ -58,6 +59,7 @@ def test_optional
}

data = {}

assert_valid schema, data
end

Expand All @@ -74,19 +76,24 @@ def test_disallow
}

data['a'] = 'string'

assert_valid schema, data

data['a'] = 5

refute_valid schema, data

schema['properties']['a']['disallow'] = %w[integer string]
data['a'] = 'string'

refute_valid schema, data

data['a'] = 5

refute_valid schema, data

data['a'] = false

assert_valid schema, data
end

Expand Down
Loading

0 comments on commit 640a4c6

Please sign in to comment.