forked from rubocop/rubocop-ast
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fixes rubocop#22] Introduce Tuple, a frozen Array/Set
- Loading branch information
Showing
9 changed files
with
152 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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module AST | ||
# A frozen Array/Set | ||
# | ||
class Tuple < ::Array | ||
extend Forwardable | ||
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 | ||
|
||
def to_a | ||
self | ||
end | ||
|
||
def_delegators :@to_set, :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,51 @@ | ||
# 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 | ||
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 |