rubysl-set 0.0.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +0 -1
- data/.travis.yml +8 -0
- data/README.md +2 -2
- data/Rakefile +0 -1
- data/lib/rubysl/set.rb +2 -0
- data/lib/rubysl/set/set.rb +1237 -0
- data/lib/{rubysl-set → rubysl/set}/version.rb +1 -1
- data/lib/set.rb +1 -0
- data/rubysl-set.gemspec +19 -18
- data/spec/add_spec.rb +26 -0
- data/spec/append_spec.rb +6 -0
- data/spec/classify_spec.rb +34 -0
- data/spec/clear_spec.rb +16 -0
- data/spec/collect_spec.rb +6 -0
- data/spec/constructor_spec.rb +14 -0
- data/spec/delete_if_spec.rb +45 -0
- data/spec/delete_spec.rb +36 -0
- data/spec/difference_spec.rb +6 -0
- data/spec/divide_spec.rb +33 -0
- data/spec/each_spec.rb +33 -0
- data/spec/empty_spec.rb +9 -0
- data/spec/enumerable/to_set_spec.rb +18 -0
- data/spec/eql_spec.rb +16 -0
- data/spec/equal_value_spec.rb +37 -0
- data/spec/exclusion_spec.rb +17 -0
- data/spec/flatten_merge_spec.rb +22 -0
- data/spec/flatten_spec.rb +39 -0
- data/spec/hash_spec.rb +14 -0
- data/spec/include_spec.rb +6 -0
- data/spec/initialize_copy_spec.rb +17 -0
- data/spec/initialize_spec.rb +23 -0
- data/spec/inspect_spec.rb +17 -0
- data/spec/intersection_spec.rb +10 -0
- data/spec/keep_if_spec.rb +39 -0
- data/spec/length_spec.rb +6 -0
- data/spec/map_spec.rb +6 -0
- data/spec/member_spec.rb +6 -0
- data/spec/merge_spec.rb +18 -0
- data/spec/minus_spec.rb +6 -0
- data/spec/plus_spec.rb +6 -0
- data/spec/pretty_print_cycle_spec.rb +9 -0
- data/spec/pretty_print_spec.rb +16 -0
- data/spec/proper_subset_spec.rb +33 -0
- data/spec/proper_superset_spec.rb +33 -0
- data/spec/reject_spec.rb +49 -0
- data/spec/replace_spec.rb +16 -0
- data/spec/select_spec.rb +43 -0
- data/spec/shared/add.rb +14 -0
- data/spec/shared/collect.rb +20 -0
- data/spec/shared/difference.rb +15 -0
- data/spec/shared/include.rb +7 -0
- data/spec/shared/intersection.rb +15 -0
- data/spec/shared/length.rb +6 -0
- data/spec/shared/union.rb +15 -0
- data/spec/size_spec.rb +6 -0
- data/spec/sortedset/add_spec.rb +34 -0
- data/spec/sortedset/append_spec.rb +6 -0
- data/spec/sortedset/classify_spec.rb +34 -0
- data/spec/sortedset/clear_spec.rb +16 -0
- data/spec/sortedset/collect_spec.rb +6 -0
- data/spec/sortedset/constructor_spec.rb +14 -0
- data/spec/sortedset/delete_if_spec.rb +47 -0
- data/spec/sortedset/delete_spec.rb +36 -0
- data/spec/sortedset/difference_spec.rb +6 -0
- data/spec/sortedset/divide_spec.rb +33 -0
- data/spec/sortedset/each_spec.rb +35 -0
- data/spec/sortedset/empty_spec.rb +9 -0
- data/spec/sortedset/eql_spec.rb +17 -0
- data/spec/sortedset/equal_value_spec.rb +12 -0
- data/spec/sortedset/exclusion_spec.rb +17 -0
- data/spec/sortedset/flatten_merge_spec.rb +7 -0
- data/spec/sortedset/flatten_spec.rb +47 -0
- data/spec/sortedset/hash_spec.rb +14 -0
- data/spec/sortedset/include_spec.rb +6 -0
- data/spec/sortedset/initialize_copy_spec.rb +17 -0
- data/spec/sortedset/initialize_spec.rb +31 -0
- data/spec/sortedset/inspect_spec.rb +9 -0
- data/spec/sortedset/intersection_spec.rb +10 -0
- data/spec/sortedset/keep_if_spec.rb +32 -0
- data/spec/sortedset/length_spec.rb +6 -0
- data/spec/sortedset/map_spec.rb +6 -0
- data/spec/sortedset/member_spec.rb +6 -0
- data/spec/sortedset/merge_spec.rb +18 -0
- data/spec/sortedset/minus_spec.rb +6 -0
- data/spec/sortedset/plus_spec.rb +6 -0
- data/spec/sortedset/pretty_print_cycle_spec.rb +9 -0
- data/spec/sortedset/pretty_print_spec.rb +16 -0
- data/spec/sortedset/proper_subset_spec.rb +32 -0
- data/spec/sortedset/proper_superset_spec.rb +32 -0
- data/spec/sortedset/reject_spec.rb +51 -0
- data/spec/sortedset/replace_spec.rb +16 -0
- data/spec/sortedset/select_spec.rb +36 -0
- data/spec/sortedset/shared/add.rb +14 -0
- data/spec/sortedset/shared/collect.rb +20 -0
- data/spec/sortedset/shared/difference.rb +15 -0
- data/spec/sortedset/shared/include.rb +7 -0
- data/spec/sortedset/shared/intersection.rb +15 -0
- data/spec/sortedset/shared/length.rb +6 -0
- data/spec/sortedset/shared/union.rb +15 -0
- data/spec/sortedset/size_spec.rb +6 -0
- data/spec/sortedset/subset_spec.rb +32 -0
- data/spec/sortedset/subtract_spec.rb +16 -0
- data/spec/sortedset/superset_spec.rb +32 -0
- data/spec/sortedset/to_a_spec.rb +7 -0
- data/spec/sortedset/union_spec.rb +10 -0
- data/spec/subset_spec.rb +33 -0
- data/spec/subtract_spec.rb +16 -0
- data/spec/superset_spec.rb +33 -0
- data/spec/to_a_spec.rb +7 -0
- data/spec/union_spec.rb +10 -0
- metadata +282 -86
- data/lib/rubysl-set.rb +0 -7
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#flatten" do
|
4
|
+
it "returns a copy of self with each included Set flattened" do
|
5
|
+
set = Set[1, 2, Set[3, 4, Set[5, 6, Set[7, 8]]], 9, 10]
|
6
|
+
flattened_set = set.flatten
|
7
|
+
|
8
|
+
flattened_set.should_not equal(set)
|
9
|
+
flattened_set.should == Set[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises an ArgumentError when self is recursive" do
|
13
|
+
(set = Set[]) << set
|
14
|
+
lambda { set.flatten }.should raise_error(ArgumentError)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "Set#flatten!" do
|
19
|
+
it "flattens self" do
|
20
|
+
set = Set[1, 2, Set[3, 4, Set[5, 6, Set[7, 8]]], 9, 10]
|
21
|
+
set.flatten!
|
22
|
+
set.should == Set[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns self when self was modified" do
|
26
|
+
set = Set[1, 2, Set[3, 4]]
|
27
|
+
set.flatten!.should equal(set)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "returns nil when self was not modified" do
|
31
|
+
set = Set[1, 2, 3, 4]
|
32
|
+
set.flatten!.should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises an ArgumentError when self is recursive" do
|
36
|
+
(set = Set[]) << set
|
37
|
+
lambda { set.flatten! }.should raise_error(ArgumentError)
|
38
|
+
end
|
39
|
+
end
|
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
ruby_version_is "1.8.7" do
|
4
|
+
describe "Set#hash" do
|
5
|
+
it "is static" do
|
6
|
+
Set[].hash.should == Set[].hash
|
7
|
+
Set[1, 2, 3].hash.should == Set[1, 2, 3].hash
|
8
|
+
Set[:a, "b", ?c].hash.should == Set[?c, "b", :a].hash
|
9
|
+
|
10
|
+
Set[].hash.should_not == Set[1, 2, 3].hash
|
11
|
+
Set[1, 2, 3].hash.should_not == Set[:a, "b", ?c].hash
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#initialize_copy" do
|
4
|
+
before(:each) do
|
5
|
+
@set = Set[1, 2, 3]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "is private" do
|
9
|
+
Set.should have_private_instance_method(:initialize_copy)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "replaces all elements of self with the elements of the passed Set" do
|
13
|
+
other = Set["1", "2", "3"]
|
14
|
+
@set.send(:initialize_copy, other)
|
15
|
+
@set.should == Set["1", "2", "3"]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#initialize" do
|
4
|
+
it "is private" do
|
5
|
+
Set.should have_private_instance_method(:initialize)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "adds all elements of the passed Enumerable to self" do
|
9
|
+
s = Set.new([1, 2, 3])
|
10
|
+
s.size.should eql(3)
|
11
|
+
s.should include(1)
|
12
|
+
s.should include(2)
|
13
|
+
s.should include(3)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "preprocesses all elements by a passed block before adding to self" do
|
17
|
+
s = Set.new([1, 2, 3]) { |x| x * x }
|
18
|
+
s.size.should eql(3)
|
19
|
+
s.should include(1)
|
20
|
+
s.should include(4)
|
21
|
+
s.should include(9)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#inspect" do
|
4
|
+
it "returns a String representation of self" do
|
5
|
+
Set[].inspect.should be_kind_of(String)
|
6
|
+
Set[nil, false, true].inspect.should be_kind_of(String)
|
7
|
+
Set[1, 2, 3].inspect.should be_kind_of(String)
|
8
|
+
Set["1", "2", "3"].inspect.should be_kind_of(String)
|
9
|
+
Set[:a, "b", Set[?c]].inspect.should be_kind_of(String)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "correctly handles self-references" do
|
13
|
+
(set = Set[]) << set
|
14
|
+
set.inspect.should be_kind_of(String)
|
15
|
+
set.inspect.should include("#<Set: {...}>")
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
ruby_version_is "1.9" do
|
4
|
+
describe "Set#keep_if" do
|
5
|
+
before(:each) do
|
6
|
+
@set = Set["one", "two", "three"]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "yields every element of self" do
|
10
|
+
ret = []
|
11
|
+
@set.keep_if { |x| ret << x }
|
12
|
+
ret.sort.should == ["one", "two", "three"].sort
|
13
|
+
end
|
14
|
+
|
15
|
+
it "keeps every element from self for which the passed block returns true" do
|
16
|
+
@set.keep_if { |x| x.size != 3 }
|
17
|
+
@set.size.should eql(1)
|
18
|
+
|
19
|
+
@set.should_not include("one")
|
20
|
+
@set.should_not include("two")
|
21
|
+
@set.should include("three")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns self" do
|
25
|
+
@set.keep_if {}.should equal(@set)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns an Enumerator when passed no block" do
|
29
|
+
enum = @set.keep_if
|
30
|
+
enum.should be_an_instance_of(enumerator_class)
|
31
|
+
|
32
|
+
enum.each { |x| x.size != 3 }
|
33
|
+
|
34
|
+
@set.should_not include("one")
|
35
|
+
@set.should_not include("two")
|
36
|
+
@set.should include("three")
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/spec/length_spec.rb
ADDED
data/spec/map_spec.rb
ADDED
data/spec/member_spec.rb
ADDED
data/spec/merge_spec.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#merge" do
|
4
|
+
it "adds the elements of the passed Enumerable to self" do
|
5
|
+
Set[:a, :b].merge(Set[:b, :c, :d]).should == Set[:a, :b, :c, :d]
|
6
|
+
Set[1, 2].merge([3, 4]).should == Set[1, 2, 3, 4]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns self" do
|
10
|
+
set = Set[1, 2]
|
11
|
+
set.merge([3, 4]).should equal(set)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "raises an ArgumentError when passed a non-Enumerable" do
|
15
|
+
lambda { Set[1, 2].merge(1) }.should raise_error(ArgumentError)
|
16
|
+
lambda { Set[1, 2].merge(Object.new) }.should raise_error(ArgumentError)
|
17
|
+
end
|
18
|
+
end
|
data/spec/minus_spec.rb
ADDED
data/spec/plus_spec.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#pretty_print_cycle" do
|
4
|
+
it "passes the 'pretty print' representation of a self-referencing Set to the pretty print writer" do
|
5
|
+
pp = mock("PrettyPrint")
|
6
|
+
pp.should_receive(:text).with("#<Set: {...}>")
|
7
|
+
Set[1, 2, 3].pretty_print_cycle(pp)
|
8
|
+
end
|
9
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#pretty_print" do
|
4
|
+
it "passes the 'pretty print' representation of self to the pretty print writer" do
|
5
|
+
pp = mock("PrettyPrint")
|
6
|
+
set = Set[1, 2, 3]
|
7
|
+
|
8
|
+
pp.should_receive(:text).with("#<Set: {")
|
9
|
+
pp.should_receive(:text).with("}>")
|
10
|
+
|
11
|
+
pp.should_receive(:nest).with(1).and_yield
|
12
|
+
pp.should_receive(:seplist).with(set)
|
13
|
+
|
14
|
+
set.pretty_print(pp)
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#proper_subset?" do
|
4
|
+
before(:each) do
|
5
|
+
@set = Set[1, 2, 3, 4]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns true if passed a Set that self is a proper subset of" do
|
9
|
+
Set[].proper_subset?(@set).should be_true
|
10
|
+
Set[].proper_subset?(Set[1, 2, 3]).should be_true
|
11
|
+
Set[].proper_subset?(Set["a", :b, ?c]).should be_true
|
12
|
+
|
13
|
+
Set[1, 2, 3].proper_subset?(@set).should be_true
|
14
|
+
Set[1, 3].proper_subset?(@set).should be_true
|
15
|
+
Set[1, 2].proper_subset?(@set).should be_true
|
16
|
+
Set[1].proper_subset?(@set).should be_true
|
17
|
+
|
18
|
+
Set[5].proper_subset?(@set).should be_false
|
19
|
+
Set[1, 5].proper_subset?(@set).should be_false
|
20
|
+
Set[nil].proper_subset?(@set).should be_false
|
21
|
+
Set["test"].proper_subset?(@set).should be_false
|
22
|
+
|
23
|
+
@set.proper_subset?(@set).should be_false
|
24
|
+
Set[].proper_subset?(Set[]).should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "raises an ArgumentError when passed a non-Set" do
|
28
|
+
lambda { Set[].proper_subset?([]) }.should raise_error(ArgumentError)
|
29
|
+
lambda { Set[].proper_subset?(1) }.should raise_error(ArgumentError)
|
30
|
+
lambda { Set[].proper_subset?("test") }.should raise_error(ArgumentError)
|
31
|
+
lambda { Set[].proper_subset?(Object.new) }.should raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#proper_superset?" do
|
4
|
+
before(:each) do
|
5
|
+
@set = Set[1, 2, 3, 4]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns true if passed a Set that self is a proper superset of" do
|
9
|
+
@set.proper_superset?(Set[]).should be_true
|
10
|
+
Set[1, 2, 3].proper_superset?(Set[]).should be_true
|
11
|
+
Set["a", :b, ?c].proper_superset?(Set[]).should be_true
|
12
|
+
|
13
|
+
@set.proper_superset?(Set[1, 2, 3]).should be_true
|
14
|
+
@set.proper_superset?(Set[1, 3]).should be_true
|
15
|
+
@set.proper_superset?(Set[1, 2]).should be_true
|
16
|
+
@set.proper_superset?(Set[1]).should be_true
|
17
|
+
|
18
|
+
@set.proper_superset?(Set[5]).should be_false
|
19
|
+
@set.proper_superset?(Set[1, 5]).should be_false
|
20
|
+
@set.proper_superset?(Set[nil]).should be_false
|
21
|
+
@set.proper_superset?(Set["test"]).should be_false
|
22
|
+
|
23
|
+
@set.proper_superset?(@set).should be_false
|
24
|
+
Set[].proper_superset?(Set[]).should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "raises an ArgumentError when passed a non-Set" do
|
28
|
+
lambda { Set[].proper_superset?([]) }.should raise_error(ArgumentError)
|
29
|
+
lambda { Set[].proper_superset?(1) }.should raise_error(ArgumentError)
|
30
|
+
lambda { Set[].proper_superset?("test") }.should raise_error(ArgumentError)
|
31
|
+
lambda { Set[].proper_superset?(Object.new) }.should raise_error(ArgumentError)
|
32
|
+
end
|
33
|
+
end
|
data/spec/reject_spec.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#reject!" do
|
4
|
+
before(:each) do
|
5
|
+
@set = Set["one", "two", "three"]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "yields every element of self" do
|
9
|
+
ret = []
|
10
|
+
@set.reject! { |x| ret << x }
|
11
|
+
ret.sort.should == ["one", "two", "three"].sort
|
12
|
+
end
|
13
|
+
|
14
|
+
it "deletes every element from self for which the passed block returns true" do
|
15
|
+
@set.reject! { |x| x.size == 3 }
|
16
|
+
@set.size.should eql(1)
|
17
|
+
|
18
|
+
@set.should_not include("one")
|
19
|
+
@set.should_not include("two")
|
20
|
+
@set.should include("three")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "returns self when self was modified" do
|
24
|
+
@set.reject! { |x| true }.should equal(@set)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns nil when self was not modified" do
|
28
|
+
@set.reject! { |x| false }.should be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
ruby_version_is "" ... "1.8.8" do
|
32
|
+
it "raises a LocalJumpError when passed no block" do
|
33
|
+
lambda { @set.reject! }.should raise_error(LocalJumpError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ruby_version_is "1.8.8" do
|
38
|
+
it "returns an Enumerator when passed no block" do
|
39
|
+
enum = @set.reject!
|
40
|
+
enum.should be_an_instance_of(enumerator_class)
|
41
|
+
|
42
|
+
enum.each { |x| x.size == 3 }
|
43
|
+
|
44
|
+
@set.should_not include("one")
|
45
|
+
@set.should_not include("two")
|
46
|
+
@set.should include("three")
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
describe "Set#replace" do
|
4
|
+
before :each do
|
5
|
+
@set = Set[:a, :b, :c]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "replaces the contents with other and returns self" do
|
9
|
+
@set.replace(Set[1, 2, 3]).should == @set
|
10
|
+
@set.should == Set[1, 2, 3]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "accepts any enumerable as other" do
|
14
|
+
@set.replace([1, 2, 3]).should == Set[1, 2, 3]
|
15
|
+
end
|
16
|
+
end
|
data/spec/select_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'set'
|
2
|
+
|
3
|
+
ruby_version_is "1.9" do
|
4
|
+
describe "Set#select!" do
|
5
|
+
before(:each) do
|
6
|
+
@set = Set["one", "two", "three"]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "yields every element of self" do
|
10
|
+
ret = []
|
11
|
+
@set.select! { |x| ret << x }
|
12
|
+
ret.sort.should == ["one", "two", "three"].sort
|
13
|
+
end
|
14
|
+
|
15
|
+
it "keeps every element from self for which the passed block returns true" do
|
16
|
+
@set.select! { |x| x.size != 3 }
|
17
|
+
@set.size.should eql(1)
|
18
|
+
|
19
|
+
@set.should_not include("one")
|
20
|
+
@set.should_not include("two")
|
21
|
+
@set.should include("three")
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns self when self was modified" do
|
25
|
+
@set.select! { false }.should equal(@set)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns nil when self was not modified" do
|
29
|
+
@set.select! { true }.should be_nil
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns an Enumerator when passed no block" do
|
33
|
+
enum = @set.select!
|
34
|
+
enum.should be_an_instance_of(enumerator_class)
|
35
|
+
|
36
|
+
enum.each { |x| x.size != 3 }
|
37
|
+
|
38
|
+
@set.should_not include("one")
|
39
|
+
@set.should_not include("two")
|
40
|
+
@set.should include("three")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|