-
Notifications
You must be signed in to change notification settings - Fork 234
Replace virtus with regular accessors + testing type-check shim #574
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
Open
pnomolos
wants to merge
6
commits into
whitesmith:main
Choose a base branch
from
pnomolos:ps.replace-virtus
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a642b76
chore(deps): replace virtus with internal attributes module
pnomolos 34848c1
Merge branch 'main' into ps.replace-virtus
pnomolos 20e6967
Updated type test to check typing of numeric attributes for value
pnomolos 4d82735
Merge remote-tracking branch 'refs/remotes/origin/ps.replace-virtus' …
pnomolos 9cf463d
Add ostruct as dependency for Ruby 4.0
pnomolos e32a65e
Fix JRuby compatibility for bundle install and type-check test
pnomolos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
| require 'rubycritic/core/smell' | ||
| require 'rubycritic/core/analysed_module' | ||
| require 'bigdecimal' | ||
|
|
||
| describe 'TypeCheck' do | ||
| describe RubyCritic::Smell do | ||
| # Numeric.subclasses includes `Date::Infinity` and `Complex` which don't make | ||
| # sense to be assigned to `cost`. We are accepting this as a possibility to | ||
| # avoid bikeshedding the micro type system to support more complex typing | ||
| %i[Integer BigDecimal Float].each do |numeric_type| | ||
| it "allows a numeric subclass of the declared type (#{numeric_type})" do | ||
| smell = RubyCritic::Smell.new | ||
| smell.score = Kernel.send(numeric_type, 42) | ||
|
|
||
| _(smell.score).must_equal Kernel.send(numeric_type, 42) | ||
| _(smell.score).must_be_kind_of Numeric | ||
| _(smell.score).must_be_kind_of Kernel.send(numeric_type, 0).class | ||
| end | ||
| end | ||
|
|
||
| it 'allows nil for any attribute' do | ||
| smell = RubyCritic::Smell.new | ||
| smell.message = nil | ||
|
|
||
| _(smell.message).must_be_nil | ||
| end | ||
|
|
||
| it 'accepts either member of a union type (String or Symbol for type)' do | ||
| smell = RubyCritic::Smell.new | ||
| smell.type = 'DuplicateCode' | ||
|
|
||
| _(smell.type).must_equal 'DuplicateCode' | ||
| smell.type = :complexity | ||
|
|
||
| _(smell.type).must_equal :complexity | ||
| end | ||
|
|
||
| it 'raises a TypeError when assigning the wrong type' do | ||
| smell = RubyCritic::Smell.new | ||
| error = _ { smell.cost = 'expensive' }.must_raise TypeError | ||
| _(error.message).must_match(/Smell#cost= expected Numeric or nil, got String/) | ||
| end | ||
|
|
||
| it 'raises when assigning the wrong type to a union attribute' do | ||
| smell = RubyCritic::Smell.new | ||
| error = _ { smell.type = 123 }.must_raise TypeError | ||
| _(error.message).must_match(/Smell#type= expected String \| Symbol or nil, got Integer/) | ||
| end | ||
|
|
||
| it 'enforces the type through the initializer' do | ||
| _ { RubyCritic::Smell.new(locations: 'not-an-array') }.must_raise TypeError | ||
| end | ||
|
|
||
| it 'constructs with realistic arguments without raising' do | ||
| smell = RubyCritic::Smell.new( | ||
| context: '#bar', | ||
| cost: 16, | ||
| locations: [RubyCritic::Location.new('./foo', '42')], | ||
| message: 'This smells', | ||
| score: 0, | ||
| type: 'DuplicateMethodCall', | ||
| analyser: 'flog' | ||
| ) | ||
|
|
||
| _(smell.analyser).must_equal 'flog' | ||
| end | ||
| end | ||
|
|
||
| describe RubyCritic::AnalysedModule do | ||
| # Ref similar test above for RubyCritic::Smell | ||
| %i[Integer BigDecimal Float].each do |numeric_type| | ||
| it "allows a numeric subclass of the declared type (#{numeric_type})" do | ||
| # BigDecimal() requires an explicit precision when given a Float; JRuby | ||
| # enforces this (CRuby is lenient), so build the value portably. | ||
| value = numeric_type == :BigDecimal ? BigDecimal(42.5, Float::DIG) : Kernel.send(numeric_type, 42.5) | ||
| mod = RubyCritic::AnalysedModule.new | ||
| mod.coverage = value | ||
|
|
||
| _(mod.coverage).must_equal value | ||
| _(mod.coverage).must_be_kind_of Numeric | ||
| _(mod.coverage).must_be_kind_of Kernel.send(numeric_type, 0).class | ||
| end | ||
| end | ||
|
|
||
| it 'preserves the Float::INFINITY default' do | ||
| _(RubyCritic::AnalysedModule.new.complexity).must_equal Float::INFINITY | ||
| end | ||
|
|
||
| it 'accumulates duplication through the writer (+=)' do | ||
| mod = RubyCritic::AnalysedModule.new | ||
| mod.duplication += 18 | ||
|
|
||
| _(mod.duplication).must_equal 18 | ||
| end | ||
|
|
||
| it 'raises a TypeError when assigning the wrong type to pathname' do | ||
| mod = RubyCritic::AnalysedModule.new | ||
| error = _ { mod.pathname = 'foo.rb' }.must_raise TypeError | ||
| _(error.message).must_match(/AnalysedModule#pathname= expected Pathname \| FakeFS::Pathname or nil, got String/) | ||
| end | ||
|
|
||
| it 'raises when assigning a non-Array to smells' do | ||
| _ { RubyCritic::AnalysedModule.new.smells = 5 }.must_raise TypeError | ||
| end | ||
|
|
||
| it 'constructs with realistic arguments without raising' do | ||
| mod = RubyCritic::AnalysedModule.new( | ||
| name: 'Foo', | ||
| pathname: Pathname.new('foo.rb'), | ||
| smells: [], | ||
| churn: 3, | ||
| complexity: 12.5, | ||
| methods_count: 4 | ||
| ) | ||
|
|
||
| _(mod.name).must_equal 'Foo' | ||
| end | ||
| end | ||
|
|
||
| describe 'the TypeCheck error type' do | ||
| it 'is a kind of the standard TypeError' do | ||
| _(TypeCheckHelper::TypeError.ancestors).must_include TypeError | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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,97 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'pathname' | ||
| require 'rubycritic/core/smell' | ||
| require 'rubycritic/core/analysed_module' | ||
|
|
||
| # Test-only "micro-Sorbet". Wraps attribute writers so that assigning a value | ||
| # of an unexpected type raises immediately, catching type regressions in the | ||
| # plain-Ruby accessors that replaced virtus. It is loaded only through | ||
| # test_helper, is never required by production code, and is never shipped | ||
| # (the gemspec packages `lib` only), so it has zero runtime impact. | ||
| module TypeCheckHelper | ||
| # Raised when a wrapped writer receives a value of an unexpected type. | ||
| class TypeError < ::TypeError; end | ||
|
|
||
| # Does `value` satisfy any of the `allowed` type entries? An entry may be a | ||
| # Module (matched with is_a?) or a String naming a class that might not be | ||
| # loaded when the spec is defined (e.g. the FakeFS::Pathname test double), | ||
| # matched by name against the value's ancestors. | ||
| def self.match?(value, allowed) | ||
| allowed.any? do |type| | ||
| if type.is_a?(Module) | ||
| value.is_a?(type) | ||
| else | ||
| value.class.ancestors.any? { |ancestor| ancestor.name == type } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Builds an anonymous module that, when included, prepends type-checking | ||
| # wrappers around the named writers of the including class. `prepend` is | ||
| # required (not plain include) so the wrapper's `name=` wins over the | ||
| # `attr_accessor` writer defined directly on the class, while `super` | ||
| # still reaches that original writer. | ||
| def self.build(specs) | ||
| Module.new do | ||
| define_singleton_method(:included) do |base| | ||
| wrapper = Module.new do | ||
| specs.each do |name, expected| | ||
| allowed = Array(expected) | ||
| define_method("#{name}=") do |value| | ||
| unless value.nil? || TypeCheckHelper.match?(value, allowed) | ||
| raise TypeCheckHelper::TypeError, | ||
| "#{base}##{name}= expected #{allowed.join(' | ')} or nil, " \ | ||
| "got #{value.class} (#{value.inspect})" | ||
| end | ||
|
|
||
| super(value) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| base.prepend(wrapper) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Test-only DSL so a class can declare `include TypeCheck(attr: Type, ...)`. | ||
| # Defined privately on Kernel so the bare `TypeCheck(...)` call resolves inside | ||
| # a class body (where `self` is the class). A nil value is always permitted; a | ||
| # spec value may be a single class or an array of classes (a union). | ||
| module Kernel | ||
| private | ||
|
|
||
| def TypeCheck(specs) | ||
| TypeCheckHelper.build(specs) | ||
| end | ||
| end | ||
|
|
||
| module RubyCritic | ||
| class Smell | ||
| include TypeCheck( | ||
| cost: Numeric, | ||
| score: Numeric, | ||
| locations: Array, | ||
| status: Symbol, | ||
| context: String, | ||
| message: String, | ||
| type: [String, Symbol], | ||
| analyser: String | ||
| ) | ||
| end | ||
|
|
||
| class AnalysedModule | ||
| include TypeCheck( | ||
| coverage: Numeric, | ||
| complexity: Numeric, | ||
| duplication: Numeric, | ||
| churn: Integer, | ||
| methods_count: Integer, | ||
| name: String, | ||
| pathname: [Pathname, 'FakeFS::Pathname'], | ||
| smells: Array | ||
| ) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Should we check smell.score's type here?