rubysl-set 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/.travis.yml +8 -0
  4. data/README.md +2 -2
  5. data/Rakefile +0 -1
  6. data/lib/rubysl/set.rb +2 -0
  7. data/lib/rubysl/set/set.rb +1237 -0
  8. data/lib/{rubysl-set → rubysl/set}/version.rb +1 -1
  9. data/lib/set.rb +1 -0
  10. data/rubysl-set.gemspec +19 -18
  11. data/spec/add_spec.rb +26 -0
  12. data/spec/append_spec.rb +6 -0
  13. data/spec/classify_spec.rb +34 -0
  14. data/spec/clear_spec.rb +16 -0
  15. data/spec/collect_spec.rb +6 -0
  16. data/spec/constructor_spec.rb +14 -0
  17. data/spec/delete_if_spec.rb +45 -0
  18. data/spec/delete_spec.rb +36 -0
  19. data/spec/difference_spec.rb +6 -0
  20. data/spec/divide_spec.rb +33 -0
  21. data/spec/each_spec.rb +33 -0
  22. data/spec/empty_spec.rb +9 -0
  23. data/spec/enumerable/to_set_spec.rb +18 -0
  24. data/spec/eql_spec.rb +16 -0
  25. data/spec/equal_value_spec.rb +37 -0
  26. data/spec/exclusion_spec.rb +17 -0
  27. data/spec/flatten_merge_spec.rb +22 -0
  28. data/spec/flatten_spec.rb +39 -0
  29. data/spec/hash_spec.rb +14 -0
  30. data/spec/include_spec.rb +6 -0
  31. data/spec/initialize_copy_spec.rb +17 -0
  32. data/spec/initialize_spec.rb +23 -0
  33. data/spec/inspect_spec.rb +17 -0
  34. data/spec/intersection_spec.rb +10 -0
  35. data/spec/keep_if_spec.rb +39 -0
  36. data/spec/length_spec.rb +6 -0
  37. data/spec/map_spec.rb +6 -0
  38. data/spec/member_spec.rb +6 -0
  39. data/spec/merge_spec.rb +18 -0
  40. data/spec/minus_spec.rb +6 -0
  41. data/spec/plus_spec.rb +6 -0
  42. data/spec/pretty_print_cycle_spec.rb +9 -0
  43. data/spec/pretty_print_spec.rb +16 -0
  44. data/spec/proper_subset_spec.rb +33 -0
  45. data/spec/proper_superset_spec.rb +33 -0
  46. data/spec/reject_spec.rb +49 -0
  47. data/spec/replace_spec.rb +16 -0
  48. data/spec/select_spec.rb +43 -0
  49. data/spec/shared/add.rb +14 -0
  50. data/spec/shared/collect.rb +20 -0
  51. data/spec/shared/difference.rb +15 -0
  52. data/spec/shared/include.rb +7 -0
  53. data/spec/shared/intersection.rb +15 -0
  54. data/spec/shared/length.rb +6 -0
  55. data/spec/shared/union.rb +15 -0
  56. data/spec/size_spec.rb +6 -0
  57. data/spec/sortedset/add_spec.rb +34 -0
  58. data/spec/sortedset/append_spec.rb +6 -0
  59. data/spec/sortedset/classify_spec.rb +34 -0
  60. data/spec/sortedset/clear_spec.rb +16 -0
  61. data/spec/sortedset/collect_spec.rb +6 -0
  62. data/spec/sortedset/constructor_spec.rb +14 -0
  63. data/spec/sortedset/delete_if_spec.rb +47 -0
  64. data/spec/sortedset/delete_spec.rb +36 -0
  65. data/spec/sortedset/difference_spec.rb +6 -0
  66. data/spec/sortedset/divide_spec.rb +33 -0
  67. data/spec/sortedset/each_spec.rb +35 -0
  68. data/spec/sortedset/empty_spec.rb +9 -0
  69. data/spec/sortedset/eql_spec.rb +17 -0
  70. data/spec/sortedset/equal_value_spec.rb +12 -0
  71. data/spec/sortedset/exclusion_spec.rb +17 -0
  72. data/spec/sortedset/flatten_merge_spec.rb +7 -0
  73. data/spec/sortedset/flatten_spec.rb +47 -0
  74. data/spec/sortedset/hash_spec.rb +14 -0
  75. data/spec/sortedset/include_spec.rb +6 -0
  76. data/spec/sortedset/initialize_copy_spec.rb +17 -0
  77. data/spec/sortedset/initialize_spec.rb +31 -0
  78. data/spec/sortedset/inspect_spec.rb +9 -0
  79. data/spec/sortedset/intersection_spec.rb +10 -0
  80. data/spec/sortedset/keep_if_spec.rb +32 -0
  81. data/spec/sortedset/length_spec.rb +6 -0
  82. data/spec/sortedset/map_spec.rb +6 -0
  83. data/spec/sortedset/member_spec.rb +6 -0
  84. data/spec/sortedset/merge_spec.rb +18 -0
  85. data/spec/sortedset/minus_spec.rb +6 -0
  86. data/spec/sortedset/plus_spec.rb +6 -0
  87. data/spec/sortedset/pretty_print_cycle_spec.rb +9 -0
  88. data/spec/sortedset/pretty_print_spec.rb +16 -0
  89. data/spec/sortedset/proper_subset_spec.rb +32 -0
  90. data/spec/sortedset/proper_superset_spec.rb +32 -0
  91. data/spec/sortedset/reject_spec.rb +51 -0
  92. data/spec/sortedset/replace_spec.rb +16 -0
  93. data/spec/sortedset/select_spec.rb +36 -0
  94. data/spec/sortedset/shared/add.rb +14 -0
  95. data/spec/sortedset/shared/collect.rb +20 -0
  96. data/spec/sortedset/shared/difference.rb +15 -0
  97. data/spec/sortedset/shared/include.rb +7 -0
  98. data/spec/sortedset/shared/intersection.rb +15 -0
  99. data/spec/sortedset/shared/length.rb +6 -0
  100. data/spec/sortedset/shared/union.rb +15 -0
  101. data/spec/sortedset/size_spec.rb +6 -0
  102. data/spec/sortedset/subset_spec.rb +32 -0
  103. data/spec/sortedset/subtract_spec.rb +16 -0
  104. data/spec/sortedset/superset_spec.rb +32 -0
  105. data/spec/sortedset/to_a_spec.rb +7 -0
  106. data/spec/sortedset/union_spec.rb +10 -0
  107. data/spec/subset_spec.rb +33 -0
  108. data/spec/subtract_spec.rb +16 -0
  109. data/spec/superset_spec.rb +33 -0
  110. data/spec/to_a_spec.rb +7 -0
  111. data/spec/union_spec.rb +10 -0
  112. metadata +282 -86
  113. 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,6 @@
1
+ require File.expand_path('../shared/include', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#include?" do
5
+ it_behaves_like :set_include, :include?
6
+ 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,10 @@
1
+ require File.expand_path('../shared/intersection', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#intersection" do
5
+ it_behaves_like :set_intersection, :intersection
6
+ end
7
+
8
+ describe "Set#&" do
9
+ it_behaves_like :set_intersection, :&
10
+ 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
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/length', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#length" do
5
+ it_behaves_like :set_length, :length
6
+ end
data/spec/map_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/collect', __FILE__)
3
+
4
+ describe "Set#map!" do
5
+ it_behaves_like :set_collect_bang, :map!
6
+ end
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/include', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#member?" do
5
+ it_behaves_like :set_include, :member?
6
+ end
@@ -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
@@ -0,0 +1,6 @@
1
+ require 'set'
2
+ require File.expand_path('../shared/difference', __FILE__)
3
+
4
+ describe "Set#-" do
5
+ it_behaves_like :set_difference, :-
6
+ end
data/spec/plus_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require File.expand_path('../shared/union', __FILE__)
2
+ require 'set'
3
+
4
+ describe "Set#+" do
5
+ it_behaves_like :set_union, :+
6
+ end
@@ -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
@@ -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
@@ -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