forked from rubocop/rubocop-ast
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
174 additions
and
62 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
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# Tuple represents a frozen and indexed Array | ||
# It's meant to be used to store arrays of constants in an array | ||
# but with faster lookup via `include?` | ||
# Like `Set`, the case equality `===` is an alias for `include?` | ||
# | ||
# FOO = Tuple[:hello, :world] | ||
# FOO.include?(:hello) # => true, quickly | ||
# | ||
# case bar | ||
# when FOO # Note: no splat | ||
# # decided quickly | ||
# # ... | ||
class Tuple < ::Array | ||
attr_reader :to_set | ||
|
||
def initialize(ary) | ||
raise ArgumentError, 'Must be initialized with an array' unless ary.is_a?(Array) | ||
|
||
super | ||
freeze | ||
end | ||
|
||
def self.[](*values) | ||
new(values) | ||
end | ||
|
||
def freeze | ||
@to_set ||= Set.new(self).freeze | ||
super | ||
end | ||
|
||
# Return self, not a newly allocated Tuple | ||
def to_a | ||
self | ||
end | ||
|
||
def include?(value) | ||
@to_set.include?(value) | ||
end | ||
|
||
alias === include? | ||
end | ||
end | ||
end | ||
|
||
def Tuple(list) # rubocop:disable Naming/MethodName | ||
RuboCop::AST::Tuple.new(list) | ||
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,59 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::AST::Tuple do | ||
shared_examples 'a tuple' do | ||
it { is_expected.to be_frozen } | ||
it { expect(tuple.include?(:included)).to be true } | ||
it { expect(tuple.include?(:not_included)).to be false } | ||
it { is_expected.to eq tuple.dup } | ||
|
||
describe '#to_a' do | ||
subject { tuple.to_a } | ||
|
||
it { is_expected.to equal tuple.to_a } | ||
it { is_expected.to be_frozen } | ||
it { is_expected.to include :included } | ||
end | ||
|
||
describe '#to_set' do | ||
subject { tuple.to_set } | ||
|
||
it { is_expected.to equal tuple.to_set } | ||
it { is_expected.to be_frozen } | ||
it { is_expected.to be >= Set[:included] } | ||
end | ||
end | ||
|
||
let(:values) { %i[included also_included] } | ||
|
||
describe '.new' do | ||
subject(:tuple) { described_class.new(values) } | ||
|
||
it_behaves_like 'a tuple' | ||
|
||
it 'enforces a single array argument' do | ||
expect { described_class.new }.to raise_error ArgumentError | ||
expect { described_class.new(5) }.to raise_error ArgumentError | ||
end | ||
|
||
it 'has freeze return self' do | ||
expect(tuple.freeze).to equal tuple | ||
end | ||
|
||
it 'has the right case equality' do | ||
expect(tuple).to be === :included # rubocop:disable Style/CaseEquality | ||
end | ||
end | ||
|
||
describe '.[]' do | ||
subject(:tuple) { described_class[*values] } | ||
|
||
it_behaves_like 'a tuple' | ||
end | ||
|
||
describe '()' do | ||
subject(:tuple) { Tuple values } | ||
|
||
it_behaves_like 'a tuple' | ||
end | ||
end |