-
Notifications
You must be signed in to change notification settings - Fork 477
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 #815 from sleekybadger/hotfix/file-list-return-array
Fix FileList returning arrays
- Loading branch information
Showing
7 changed files
with
126 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
module Danger | ||
module Helpers | ||
module ArraySublcass | ||
include Comparable | ||
|
||
def initialize(array) | ||
@__array__ = array | ||
end | ||
|
||
def kind_of?(compare_class) | ||
return true if compare_class == self.class | ||
|
||
dummy.kind_of?(compare_class) | ||
end | ||
|
||
def method_missing(name, *args, &block) | ||
super unless __array__.respond_to?(name) | ||
|
||
respond_to_method(name, *args, &block) | ||
end | ||
|
||
def respond_to_missing?(name) | ||
__array__.respond_to?(name) || super | ||
end | ||
|
||
def to_a | ||
__array__ | ||
end | ||
|
||
def to_ary | ||
__array__ | ||
end | ||
|
||
def <=>(other) | ||
return unless other.kind_of?(self.class) | ||
|
||
__array__ <=> other.instance_variable_get(:@__array__) | ||
end | ||
|
||
private | ||
|
||
attr_accessor :__array__ | ||
|
||
def dummy | ||
Class.new(Array).new | ||
end | ||
|
||
def respond_to_method(name, *args, &block) | ||
result = __array__.send(name, *args, &block) | ||
return result unless result.kind_of?(Array) | ||
|
||
if name =~ /!/ | ||
__array__ = result | ||
self | ||
else | ||
self.class.new(result) | ||
end | ||
end | ||
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
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,49 @@ | ||
RSpec.describe Danger::Helpers::ArraySublcass do | ||
class List; include Danger::Helpers::ArraySublcass; end | ||
class OtherList; include Danger::Helpers::ArraySublcass; end | ||
|
||
it "acts as array" do | ||
first_list = List.new([1, 2, 3]) | ||
second_list = List.new([4, 5, 6]) | ||
third_list = List.new([1, 2, 3]) | ||
fourth_list = List.new([7, 7]) | ||
|
||
mapped_list = first_list.map { |item| item + 1 } | ||
concated_list = first_list + second_list | ||
mapped_mutated_list = third_list.map! { |item| item + 10 } | ||
deleted_from_list = fourth_list.delete_at(0) | ||
reduced_list = first_list.each_with_index.reduce({}) do |accum, (i, el)| | ||
accum.store(i, el) | ||
accum | ||
end | ||
|
||
expect(first_list.length).to eq(3) | ||
expect(mapped_list).to eq(List.new([2, 3, 4])) | ||
expect(concated_list).to eq(List.new([1, 2, 3, 4, 5, 6])) | ||
expect(third_list).to eq(List.new([11, 12, 13])) | ||
expect(mapped_mutated_list).to eq(List.new([11, 12, 13])) | ||
expect(deleted_from_list).to eq(7) | ||
expect(fourth_list).to eq(List.new([7])) | ||
expect(reduced_list).to eq({ 1 => 0, 2 => 1, 3 => 2 }) | ||
end | ||
|
||
describe "equality" do | ||
it "equals with same class same size and same values" do | ||
first_list = List.new([1, 2, 3]) | ||
second_list = List.new([1, 2, 3]) | ||
third_list = List.new([4, 5, 6]) | ||
|
||
expect(first_list).to eq(second_list) | ||
expect(first_list).not_to eq(third_list) | ||
end | ||
|
||
it "not equals with other classes" do | ||
first_list = List.new([1, 2, 3]) | ||
second_list = OtherList.new([1, 2, 3]) | ||
third_list = [4, 5, 6] | ||
|
||
expect(first_list).not_to eq(second_list) | ||
expect(first_list).not_to eq(third_list) | ||
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