jactive_support 1.0.0-universal-java-1.6

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.
Files changed (133) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +63 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +20 -0
  5. data/Rakefile +45 -0
  6. data/jactive_support.gemspec +25 -0
  7. data/lib/jactive_support/constantize.rb +19 -0
  8. data/lib/jactive_support/core_ext/enum.rb +43 -0
  9. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  10. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/locale.rb +28 -0
  13. data/lib/jactive_support/core_ext/module.rb +9 -0
  14. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  15. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  16. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  17. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  18. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  19. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  20. data/lib/jactive_support/core_ext.rb +4 -0
  21. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  22. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/enum.rb +19 -0
  25. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  26. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  27. data/lib/jactive_support/java_ext/list.rb +65 -0
  28. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/locale.rb +32 -0
  30. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  31. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/number.rb +91 -0
  34. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  35. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  36. data/lib/jactive_support/java_ext.rb +4 -0
  37. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  38. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  39. data/lib/jactive_support/json/encoders/map.rb +55 -0
  40. data/lib/jactive_support/json/encoding.rb +5 -0
  41. data/lib/jactive_support/json.rb +1 -0
  42. data/lib/jactive_support/rescuable.rb +62 -0
  43. data/lib/jactive_support/version.rb +3 -0
  44. data/lib/jactive_support.rb +5 -0
  45. data/spec/constantize_spec.rb +57 -0
  46. data/spec/core_ext/hash_spec.rb +34 -0
  47. data/spec/core_ext/module_spec.rb +19 -0
  48. data/spec/core_ext/ordering_spec.rb +12 -0
  49. data/spec/enum_spec.rb +67 -0
  50. data/spec/java_ext/iterable/clear_spec.rb +56 -0
  51. data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
  52. data/spec/java_ext/iterable/empty_spec.rb +10 -0
  53. data/spec/java_ext/iterable/reject_spec.rb +76 -0
  54. data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
  55. data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
  56. data/spec/java_ext/iterable/shift_spec.rb +160 -0
  57. data/spec/java_ext/list/clear_spec.rb +56 -0
  58. data/spec/java_ext/list/collect_spec.rb +11 -0
  59. data/spec/java_ext/list/last_spec.rb +87 -0
  60. data/spec/java_ext/list/map_spec.rb +11 -0
  61. data/spec/java_ext/list/push_spec.rb +44 -0
  62. data/spec/java_ext/list/shared/collect.rb +133 -0
  63. data/spec/java_ext/list/shared/fixtures.rb +34 -0
  64. data/spec/java_ext/map/assoc_spec.rb +43 -0
  65. data/spec/java_ext/map/clear_spec.rb +49 -0
  66. data/spec/java_ext/map/clone_spec.rb +13 -0
  67. data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
  68. data/spec/java_ext/map/constructor_spec.rb +54 -0
  69. data/spec/java_ext/map/delete_if_spec.rb +54 -0
  70. data/spec/java_ext/map/delete_spec.rb +38 -0
  71. data/spec/java_ext/map/each_key_spec.rb +29 -0
  72. data/spec/java_ext/map/each_pair_spec.rb +38 -0
  73. data/spec/java_ext/map/each_spec.rb +9 -0
  74. data/spec/java_ext/map/each_value_spec.rb +28 -0
  75. data/spec/java_ext/map/element_reference_spec.rb +119 -0
  76. data/spec/java_ext/map/element_set_spec.rb +7 -0
  77. data/spec/java_ext/map/empty_spec.rb +15 -0
  78. data/spec/java_ext/map/eql_spec.rb +19 -0
  79. data/spec/java_ext/map/equal_value_spec.rb +18 -0
  80. data/spec/java_ext/map/fetch_spec.rb +35 -0
  81. data/spec/java_ext/map/fixtures/classes.rb +36 -0
  82. data/spec/java_ext/map/flatten_spec.rb +64 -0
  83. data/spec/java_ext/map/has_key_spec.rb +8 -0
  84. data/spec/java_ext/map/has_value_spec.rb +8 -0
  85. data/spec/java_ext/map/hash_spec.rb +53 -0
  86. data/spec/java_ext/map/include_spec.rb +7 -0
  87. data/spec/java_ext/map/index_spec.rb +7 -0
  88. data/spec/java_ext/map/indexes_spec.rb +9 -0
  89. data/spec/java_ext/map/indices_spec.rb +9 -0
  90. data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
  91. data/spec/java_ext/map/initialize_spec.rb +64 -0
  92. data/spec/java_ext/map/inspect_spec.rb +9 -0
  93. data/spec/java_ext/map/invert_spec.rb +27 -0
  94. data/spec/java_ext/map/keep_if_spec.rb +33 -0
  95. data/spec/java_ext/map/key_spec.rb +14 -0
  96. data/spec/java_ext/map/keys_spec.rb +39 -0
  97. data/spec/java_ext/map/length_spec.rb +7 -0
  98. data/spec/java_ext/map/member_spec.rb +7 -0
  99. data/spec/java_ext/map/merge_spec.rb +69 -0
  100. data/spec/java_ext/map/rassoc_spec.rb +39 -0
  101. data/spec/java_ext/map/reject_spec.rb +123 -0
  102. data/spec/java_ext/map/replace_spec.rb +7 -0
  103. data/spec/java_ext/map/select_spec.rb +98 -0
  104. data/spec/java_ext/map/shared/each.rb +77 -0
  105. data/spec/java_ext/map/shared/eql.rb +224 -0
  106. data/spec/java_ext/map/shared/equal.rb +94 -0
  107. data/spec/java_ext/map/shared/index.rb +29 -0
  108. data/spec/java_ext/map/shared/iteration.rb +28 -0
  109. data/spec/java_ext/map/shared/key.rb +43 -0
  110. data/spec/java_ext/map/shared/length.rb +12 -0
  111. data/spec/java_ext/map/shared/replace.rb +82 -0
  112. data/spec/java_ext/map/shared/store.rb +68 -0
  113. data/spec/java_ext/map/shared/to_s.rb +68 -0
  114. data/spec/java_ext/map/shared/update.rb +95 -0
  115. data/spec/java_ext/map/shared/value.rb +16 -0
  116. data/spec/java_ext/map/shared/values_at.rb +12 -0
  117. data/spec/java_ext/map/shift_spec.rb +44 -0
  118. data/spec/java_ext/map/size_spec.rb +7 -0
  119. data/spec/java_ext/map/sort_spec.rb +17 -0
  120. data/spec/java_ext/map/store_spec.rb +7 -0
  121. data/spec/java_ext/map/to_a_spec.rb +29 -0
  122. data/spec/java_ext/map/to_hash_spec.rb +11 -0
  123. data/spec/java_ext/map/to_s_spec.rb +20 -0
  124. data/spec/java_ext/map/try_convert_spec.rb +32 -0
  125. data/spec/java_ext/map/update_spec.rb +7 -0
  126. data/spec/java_ext/map/value_spec.rb +8 -0
  127. data/spec/java_ext/map/values_at_spec.rb +7 -0
  128. data/spec/java_ext/map/values_spec.rb +10 -0
  129. data/spec/locale_spec.rb +77 -0
  130. data/spec/shared/fixtures.rb +7 -0
  131. data/spec/shared/version.rb +123 -0
  132. data/spec/spec_helper.rb +50 -0
  133. metadata +332 -0
@@ -0,0 +1,5 @@
1
+ require 'jactive_support/java_ext'
2
+ require 'jactive_support/core_ext'
3
+ require 'jactive_support/json'
4
+ require 'jactive_support/rescuable'
5
+ require 'jactive_support/constantize'
@@ -0,0 +1,57 @@
1
+ require 'java'
2
+ require 'active_support'
3
+ require 'jactive_support/constantize'
4
+
5
+ class InflectorTest
6
+ def call_constantize(name)
7
+ ActiveSupport::Inflector.constantize(name)
8
+ end
9
+ end
10
+ module Ace
11
+ module Base
12
+ class Case
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "constantize with jruby" do
18
+
19
+ it "should find named ruby constants" do
20
+ ActiveSupport::Inflector.constantize("Ace::Base::Case").should be(Ace::Base::Case)
21
+ ActiveSupport::Inflector.constantize("::Ace::Base::Case").should be(Ace::Base::Case)
22
+ ActiveSupport::Inflector.constantize("::InflectorTest").should be(InflectorTest)
23
+ end
24
+
25
+ it "raises a NameError on invalid ruby constants" do
26
+ expect { ActiveSupport::Inflector.constantize("UnknownClass") }.to raise_error(NameError, 'uninitialized constant UnknownClass')
27
+ expect { ActiveSupport::Inflector.constantize("An invalid string") }.to raise_error(NameError, 'wrong constant name An invalid string')
28
+ expect { ActiveSupport::Inflector.constantize("InvalidClass\n") }.to raise_error(NameError, "wrong constant name InvalidClass\n")
29
+ end
30
+
31
+ it "should do lexical lookup" do
32
+ expect{ puts InflectorTest.new.call_constantize("::Ace::Base::InflectorTest").inspect }.to raise_error(NameError, 'uninitialized constant Ace::Base::InflectorTest')
33
+ end
34
+
35
+ it "should find a java class" do
36
+ ActiveSupport::Inflector.constantize("Java::JavaLang::Thread").should be(java.lang.Thread);
37
+ end
38
+
39
+ it "should be able to lookup the canonical name of a java class" do
40
+ ActiveSupport::Inflector.constantize(java.lang.Thread.name).should be(java.lang.Thread);
41
+ end
42
+
43
+ it "should raise a NameError when trying to lookup an unknown java class" do
44
+ expect{ ActiveSupport::Inflector.constantize("Java::ComMuh::Miav") }.to raise_error(NameError, 'cannot load Java class com.muh.Miav')
45
+ end
46
+
47
+ #=begin comment
48
+ #Java packages currently don't have a should method with the normal setup
49
+ it "should find a java package" do
50
+ ActiveSupport::Inflector.constantize("Java::JavaLang").should be(java.lang);
51
+ end
52
+
53
+ it "should find an unknown java package" do
54
+ ActiveSupport::Inflector.constantize("Java::ComMuh").should be(nil)
55
+ end
56
+ #=end
57
+ end
@@ -0,0 +1,34 @@
1
+ require 'jactive_support/core_ext/hash'
2
+
3
+ describe Hash do
4
+ describe "assert_valid_params" do
5
+ it "should not raise an error when assert_valid_params is called with correct parameters" do
6
+ { :failure => "stuff", :funny => "business" }.assert_valid_params([ :failure, :funny ])
7
+ { :failure => "stuff", :funny => "business" }.assert_valid_params(:failure, :funny)
8
+ end
9
+
10
+ it "should raise an error when assert_valid_params is called with incorrect parameters" do
11
+ expect{
12
+ { :failore => "stuff", :funny => "business" }.assert_valid_params([ :failure, :funny ])
13
+ }.to raise_error(JactiveSupport::InvalidParameter, "Unknown key(s): failore")
14
+
15
+ expect{
16
+ { :failore => "stuff", :funny => "business" }.assert_valid_params(:failure, :funny)
17
+ }.to raise_error(JactiveSupport::InvalidParameter, "Unknown key(s): failore")
18
+ end
19
+ end
20
+
21
+ describe "supports with_keys_values" do
22
+ it "should make a new Hash with arguments as keys and values" do
23
+ Hash.with_keys_values([:one, :two], [1, 2]).should eq(:one=>1, :two=>2)
24
+ end
25
+
26
+ it "should support empty arrays" do
27
+ Hash.with_keys_values([], []).should eq({})
28
+ end
29
+
30
+ it "raises ArgumentError when keys and values don't have same size" do
31
+ expect { Hash.with_keys_values([:one, :two], [1]) }.to raise_error(ArgumentError, "Unmatching sizes 2 != 1")
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,19 @@
1
+ require 'jactive_support/core_ext/module'
2
+
3
+ class Base
4
+ def self.inherited(other)
5
+ other.instance_variable_set('@thename', other.name || '')
6
+ end
7
+ end
8
+
9
+ Test2 = Class.new(Base)
10
+
11
+ describe Module, 'define_class' do
12
+ it "normal dynamic class does not have access to name in inherited" do
13
+ Test2.instance_variable_get('@thename').should eq('')
14
+ end
15
+
16
+ it "should have access to name even in inherited" do
17
+ Object.define_class('Test', Base).instance_variable_get('@thename').should eq('Test')
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'jactive_support/core_ext/ordering'
2
+
3
+ describe "ordering" do
4
+ it "has an order" do
5
+ (nil <=> nil).should be(0)
6
+ (nil <=> 1).should be(-1)
7
+ (true <=> true).should be(0)
8
+ (true <=> 1).should be(-1)
9
+ (false <=> false).should be(0)
10
+ (false <=> 1).should be(1)
11
+ end
12
+ end
data/spec/enum_spec.rb ADDED
@@ -0,0 +1,67 @@
1
+ require 'java'
2
+ require 'active_support'
3
+ require 'jactive_support/core_ext/enum'
4
+
5
+ describe 'enum' do
6
+
7
+ it 'returns matching enum object when calling find_value_of with exact named symbol or string' do
8
+ java::lang::Thread::State.find_value_of(:NEW).should be(java::lang::Thread::State::NEW)
9
+ java::lang::Thread::State.find_value_of('NEW').should be(java::lang::Thread::State::NEW)
10
+ end
11
+
12
+ it 'returns matching enum object when calling find_value_of with underscored symbol or string' do
13
+ java::lang::Thread::State.find_value_of(:n_e_w).should be(java::lang::Thread::State::NEW)
14
+ java::lang::Thread::State.find_value_of('n_e_w').should be(java::lang::Thread::State::NEW)
15
+ end
16
+
17
+ it 'fails when calling find_value_of with value not in enum' do
18
+ expect {
19
+ java::lang::Thread::State.find_value_of(:MISSING)
20
+ }.to raise_error(NativeException, "java.lang.IllegalArgumentException: No enum const class java.lang.Thread$State.MISSING")
21
+
22
+ expect {
23
+ java::lang::Thread::State.find_value_of(:missing_link)
24
+ }.to raise_error(NativeException, "java.lang.IllegalArgumentException: No enum const class java.lang.Thread$State.missing_link")
25
+ end
26
+
27
+ it 'should be comparable with a symbol when using ===' do
28
+ t = java::lang::Thread::State::NEW
29
+ t.should === :NEW
30
+ :NEW.should === t
31
+ end
32
+
33
+ it 'should be comparable with an underscored symbol when using ===' do
34
+ t = java::lang::Thread::State::NEW
35
+ t.should === :n_e_w
36
+ :n_e_w.should === t
37
+ end
38
+
39
+ it 'should be comparable with a string when using ===' do
40
+ t = java::lang::Thread::State::NEW
41
+ t.should === 'NEW'
42
+ 'NEW'.should === t
43
+ end
44
+
45
+
46
+ it 'should be comparable with an underscored string when using ===' do
47
+ t = java::lang::Thread::State::NEW
48
+ t.should === 'n_e_w'
49
+ 'n_e_w'.should === t
50
+ end
51
+
52
+ it 'should return the enum object when calling to_java on a symbol or string with enum class as argument' do
53
+ :NEW.to_java(java::lang::Thread::State).should be(java::lang::Thread::State::NEW)
54
+ 'NEW'.to_java(java::lang::Thread::State).should be(java::lang::Thread::State::NEW)
55
+ :n_e_w.to_java(java::lang::Thread::State).should be(java::lang::Thread::State::NEW)
56
+ 'n_e_w'.to_java(java::lang::Thread::State).should be(java::lang::Thread::State::NEW)
57
+ end
58
+
59
+ it 'fails when calling to_java on a symbol or string whose value can not be found in the provided enum class' do
60
+ expect {
61
+ :MISSING.to_java(java::lang::Thread::State)
62
+ }.to raise_error(TypeError, "No enum const class java.lang.Thread$State.MISSING")
63
+ expect {
64
+ 'MISSING'.to_java(java::lang::Thread::State)
65
+ }.to raise_error(TypeError, "No enum const class java.lang.Thread$State.MISSING")
66
+ end
67
+ end
@@ -0,0 +1,56 @@
1
+ require 'jactive_support/java_ext/iterable'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::lang::Iterable#clear" do
5
+ it "removes all elements" do
6
+ a = Itr[1, 2, 3, 4]
7
+ a.clear.should equal(a)
8
+ a.should == Itr[]
9
+ end
10
+
11
+ it "returns self" do
12
+ a = Itr[1]
13
+ oid = a.object_id
14
+ a.clear.object_id.should == oid
15
+ end
16
+
17
+ it "leaves the Iterable empty" do
18
+ a = Itr[1]
19
+ a.clear
20
+ a.empty?.should == true
21
+ end
22
+
23
+ it "keeps tainted status" do
24
+ a = Itr[1]
25
+ a.taint
26
+ a.tainted?.should be_true
27
+ a.clear
28
+ a.tainted?.should be_true
29
+ end
30
+
31
+ it "does not accept any arguments" do
32
+ lambda { Itr[1].clear(true) }.should raise_error(ArgumentError)
33
+ end
34
+
35
+ ruby_version_is '1.9' do
36
+ it "keeps untrusted status" do
37
+ a = Itr[1]
38
+ a.untrust
39
+ a.untrusted?.should be_true
40
+ a.clear
41
+ a.untrusted?.should be_true
42
+ end
43
+ end
44
+
45
+ ruby_version_is '' ... '1.9' do
46
+ it "raises a TypeError on a frozen iterable" do
47
+ lambda { IterableSpecs.frozen_iterable.clear }.should raise_error(TypeError)
48
+ end
49
+ end
50
+
51
+ ruby_version_is '1.9' do
52
+ it "raises a RuntimeError on a frozen iterable" do
53
+ lambda { IterableSpecs.frozen_iterable.clear }.should raise_error(RuntimeError)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,71 @@
1
+ require 'jactive_support/java_ext/iterable'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+ require File.expand_path('../shared/enumeratorize', __FILE__)
4
+
5
+ describe "java::lang::Iterable#delete_if" do
6
+ before do
7
+ @a = Itr[ "a", "b", "c" ]
8
+ end
9
+
10
+ it "removes each element for which block returns true" do
11
+ @a = Itr[ "a", "b", "c" ]
12
+ @a.delete_if { |x| x >= "b" }
13
+ @a.should == Itr["a"]
14
+ end
15
+
16
+ it "returns self" do
17
+ @a.delete_if{ true }.equal?(@a).should be_true
18
+ end
19
+
20
+ it_behaves_like :enumeratorize, :delete_if
21
+
22
+ it "returns self when called on an Iterable emptied with #shift" do
23
+ iterable = Itr[1]
24
+ iterable.shift
25
+ iterable.delete_if { |x| true }.should equal(iterable)
26
+ end
27
+
28
+ ruby_version_is '1.8.7' do
29
+ it "returns an Enumerator if no block given, and the enumerator can modify the original iterable" do
30
+ enum = @a.delete_if
31
+ enum.should be_an_instance_of(enumerator_class)
32
+ @a.should_not be_empty
33
+ enum.each { true }
34
+ @a.should be_empty
35
+ end
36
+ end
37
+
38
+ ruby_version_is '' ... '1.9' do
39
+ it "raises a TypeError on a frozen iterable" do
40
+ lambda { IterableSpecs.frozen_iterable.delete_if {} }.should raise_error(TypeError)
41
+ end
42
+ it "raises a TypeError on an empty frozen iterable" do
43
+ lambda { IterableSpecs.empty_frozen_iterable.delete_if {} }.should raise_error(TypeError)
44
+ end
45
+ end
46
+
47
+ ruby_version_is '1.9' do
48
+ it "raises a RuntimeError on a frozen iterable" do
49
+ lambda { IterableSpecs.frozen_iterable.delete_if {} }.should raise_error(RuntimeError)
50
+ end
51
+ it "raises a RuntimeError on an empty frozen iterable" do
52
+ lambda { IterableSpecs.empty_frozen_iterable.delete_if {} }.should raise_error(RuntimeError)
53
+ end
54
+ end
55
+
56
+ it "keeps tainted status" do
57
+ @a.taint
58
+ @a.tainted?.should be_true
59
+ @a.delete_if{ true }
60
+ @a.tainted?.should be_true
61
+ end
62
+
63
+ ruby_version_is '1.9' do
64
+ it "keeps untrusted status" do
65
+ @a.untrust
66
+ @a.untrusted?.should be_true
67
+ @a.delete_if{ true }
68
+ @a.untrusted?.should be_true
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,10 @@
1
+ require 'jactive_support/java_ext/iterable'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::lang::Iterable#empty?" do
5
+ it "returns true if the iterable has no elements" do
6
+ Itr[].empty?.should == true
7
+ Itr[1].empty?.should == false
8
+ Itr[1, 2].empty?.should == false
9
+ end
10
+ end
@@ -0,0 +1,76 @@
1
+ require 'jactive_support/java_ext/iterable'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+ require File.expand_path('../shared/enumeratorize', __FILE__)
4
+
5
+ describe "java::lang::Iterable#reject!" do
6
+ it "removes elements for which block is true" do
7
+ a = Itr[3, 4, 5, 6, 7, 8, 9, 10, 11]
8
+ a.reject! { |i| i % 2 == 0 }.should equal(a)
9
+ a.should == Itr[3, 5, 7, 9, 11]
10
+ a.reject! { |i| i > 8 }
11
+ a.should == Itr[3, 5, 7]
12
+ a.reject! { |i| i < 4 }
13
+ a.should == Itr[5, 7]
14
+ a.reject! { |i| i == 5 }
15
+ a.should == Itr[7]
16
+ a.reject! { true }
17
+ a.should == Itr[]
18
+ a.reject! { true }
19
+ a.should == Itr[]
20
+ end
21
+
22
+ it "properly handles recursive iterables" do
23
+ empty = IterableSpecs.empty_recursive_iterable
24
+ empty_dup = empty.dup
25
+ empty.reject! { false }.should == nil
26
+ empty.should == empty_dup
27
+
28
+ empty = IterableSpecs.empty_recursive_iterable
29
+ empty.reject! { true }.should == Itr[]
30
+ empty.should == Itr[]
31
+
32
+ iterable = IterableSpecs.recursive_iterable
33
+ iterable_dup = iterable.dup
34
+ iterable.reject! { false }.should == nil
35
+ iterable.should == iterable_dup
36
+
37
+ iterable = IterableSpecs.recursive_iterable
38
+ iterable.reject! { true }.should == Itr[]
39
+ iterable.should == Itr[]
40
+ end
41
+
42
+ it "returns nil when called on an Iterable emptied with #shift" do
43
+ iterable = Itr[1]
44
+ iterable.shift
45
+ iterable.reject! { |x| true }.should == nil
46
+ end
47
+
48
+ it "returns nil if no changes are made" do
49
+ a = Itr[1, 2, 3]
50
+
51
+ a.reject! { |i| i < 0 }.should == nil
52
+
53
+ a.reject! { true }
54
+ a.reject! { true }.should == nil
55
+ end
56
+
57
+ ruby_version_is "" ... "1.9" do
58
+ it "raises a TypeError on a frozen iterable" do
59
+ lambda { IterableSpecs.frozen_iterable.reject! {} }.should raise_error(TypeError)
60
+ end
61
+ it "raises a TypeError on an empty frozen iterable" do
62
+ lambda { IterableSpecs.empty_frozen_iterable.reject! {} }.should raise_error(TypeError)
63
+ end
64
+ end
65
+
66
+ ruby_version_is "1.9" do
67
+ it "raises a RuntimeError on a frozen iterable" do
68
+ lambda { IterableSpecs.frozen_iterable.reject! {} }.should raise_error(RuntimeError)
69
+ end
70
+ it "raises a RuntimeError on an empty frozen iterable" do
71
+ lambda { IterableSpecs.empty_frozen_iterable.reject! {} }.should raise_error(RuntimeError)
72
+ end
73
+ end
74
+
75
+ it_behaves_like :enumeratorize, :reject!
76
+ end
@@ -0,0 +1,12 @@
1
+ shared_examples_for :enumeratorize do |method|
2
+ ruby_version_is '' ... '1.8.7' do
3
+ it 'raises a LocalJumpError if no block given' do
4
+ lambda{ Itr[1,2].send(method) }.should raise_error(LocalJumpError)
5
+ end
6
+ end
7
+ ruby_version_is '1.8.7' do
8
+ it 'returns an Enumerator if no block given' do
9
+ Itr[1,2].send(method).should be_an_instance_of(enumerator_class)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,90 @@
1
+ require File.expand_path('../../../../shared/version', __FILE__)
2
+ require File.expand_path('../../../../shared/fixtures', __FILE__)
3
+ require 'jactive_support/core_ext/to_java'
4
+ require 'jactive_support/core_ext/to_java_list'
5
+
6
+ class Itr
7
+ class Iterator
8
+ include java::util::Iterator
9
+
10
+ def initialize(array)
11
+ @array = array
12
+ @next = 0
13
+ end
14
+
15
+ def hasNext
16
+ @next < @array.size
17
+ end
18
+
19
+ def next
20
+ raise java::util::NoSuchElementException.new('No next element available') unless hasNext
21
+ @next += 1
22
+ @array[@next-1]
23
+ end
24
+
25
+ def remove
26
+ raise java::lang::IllegalStateException.new('Can not remove until after next is called') if @next == 0
27
+ @next -= 1
28
+ @array.delete_at(@next)
29
+ end
30
+ end
31
+
32
+ def self.[](*args)
33
+ new(args)
34
+ end
35
+
36
+ attr_reader :value
37
+
38
+ def initialize(arr)
39
+ @value = arr
40
+ end
41
+
42
+ include java::lang::Iterable
43
+
44
+ def iterator
45
+ Iterator.new(@value)
46
+ end
47
+
48
+ def ==(o)
49
+ o = o.to_a if o.respond_to?(:to_a)
50
+ to_a == o
51
+ end
52
+
53
+ def to_a
54
+ value
55
+ end
56
+
57
+ def <<(o)
58
+ value << o
59
+ end
60
+
61
+ def [](*args)
62
+ Itr.new(value[*args])
63
+ end
64
+ end
65
+
66
+ module IterableSpecs
67
+ def self.frozen_iterable
68
+ frozen_array = Itr[1,2,3]
69
+ frozen_array.freeze
70
+ frozen_array
71
+ end
72
+
73
+ def self.empty_frozen_iterable
74
+ frozen_array = Itr[]
75
+ frozen_array.freeze
76
+ frozen_array
77
+ end
78
+
79
+ def self.recursive_iterable
80
+ a = Itr[1, 'two', 3.0]
81
+ 5.times { a << a }
82
+ a
83
+ end
84
+
85
+ def self.empty_recursive_iterable
86
+ a = Itr[]
87
+ a << a
88
+ a
89
+ end
90
+ end
@@ -0,0 +1,160 @@
1
+ require 'jactive_support/java_ext/iterable'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::lang::Iterable#shift" do
5
+ it "removes and returns the first element" do
6
+ a = Itr[5, 1, 1, 5, 4]
7
+ a.shift.should == 5
8
+ a.should == Itr[1, 1, 5, 4]
9
+ a.shift.should == 1
10
+ a.should == Itr[1, 5, 4]
11
+ a.shift.should == 1
12
+ a.should == Itr[5, 4]
13
+ a.shift.should == 5
14
+ a.should == Itr[4]
15
+ a.shift.should == 4
16
+ a.should == Itr[]
17
+ end
18
+
19
+ it "returns nil when the iterable is empty" do
20
+ Itr[].shift.should == nil
21
+ end
22
+
23
+ it "properly handles recursive iterables" do
24
+ empty = IterableSpecs.empty_recursive_iterable
25
+ empty.shift.should == Itr[]
26
+ empty.should == Itr[]
27
+
28
+ iterable = IterableSpecs.recursive_iterable
29
+ iterable.shift.should == 1
30
+ iterable[0..2].should == Itr['two', 3.0, iterable]
31
+ end
32
+
33
+ ruby_version_is "" ... "1.9" do
34
+ it "raises a TypeError on a frozen iterable" do
35
+ lambda { IterableSpecs.frozen_iterable.shift }.should raise_error(TypeError)
36
+ end
37
+ it "raises a TypeError on an empty frozen iterable" do
38
+ lambda { IterableSpecs.empty_frozen_iterable.shift }.should raise_error(TypeError)
39
+ end
40
+ end
41
+
42
+ ruby_version_is "1.9" do
43
+ it "raises a RuntimeError on a frozen iterable" do
44
+ lambda { IterableSpecs.frozen_iterable.shift }.should raise_error(RuntimeError)
45
+ end
46
+ it "raises a RuntimeError on an empty frozen iterable" do
47
+ lambda { IterableSpecs.empty_frozen_iterable.shift }.should raise_error(RuntimeError)
48
+ end
49
+ end
50
+
51
+ ruby_version_is '' ... '1.8.7' do
52
+ it "raises an ArgumentError if passed an argument" do
53
+ lambda{ Itr[1, 2].shift(1) }.should raise_error(ArgumentError)
54
+ end
55
+ end
56
+
57
+ describe "passed a number n as an argument" do
58
+ ruby_version_is '1.8.7' do
59
+ it "removes and returns an iterable with the first n element of the iterable" do
60
+ a = Itr[1, 2, 3, 4, 5, 6]
61
+
62
+ a.shift(0).should == []
63
+ a.should == Itr[1, 2, 3, 4, 5, 6]
64
+
65
+ a.shift(1).should == [1]
66
+ a.should == Itr[2, 3, 4, 5, 6]
67
+
68
+ a.shift(2).should == [2, 3]
69
+ a.should == Itr[4, 5, 6]
70
+
71
+ a.shift(3).should == [4, 5, 6]
72
+ a.should == Itr[]
73
+ end
74
+
75
+ it "does not corrupt the iterable when shift without arguments is followed by shift with an argument" do
76
+ a = Itr[1, 2, 3, 4, 5]
77
+
78
+ a.shift.should == 1
79
+ a.shift(3).should == [2, 3, 4]
80
+ a.should == Itr[5]
81
+ end
82
+
83
+ it "returns a new empty iterable if there are no more elements" do
84
+ a = Itr[]
85
+ popped1 = a.shift(1)
86
+ popped1.should == []
87
+ a.should == Itr[]
88
+
89
+ popped2 = a.shift(2)
90
+ popped2.should == []
91
+ a.should == Itr[]
92
+
93
+ popped1.should_not equal(popped2)
94
+ end
95
+
96
+ it "returns whole elements if n exceeds size of the iterable" do
97
+ a = Itr[1, 2, 3, 4, 5]
98
+ a.shift(6).should == [1, 2, 3, 4, 5]
99
+ a.should == Itr[]
100
+ end
101
+
102
+ it "does not return self even when it returns whole elements" do
103
+ a = Itr[1, 2, 3, 4, 5]
104
+ a.shift(5).should_not equal(a)
105
+
106
+ a = Itr[1, 2, 3, 4, 5]
107
+ a.shift(6).should_not equal(a)
108
+ end
109
+
110
+ it "raises an ArgumentError if n is negative" do
111
+ lambda{ Itr[1, 2, 3].shift(-1) }.should raise_error(ArgumentError)
112
+ end
113
+
114
+ it "tries to convert n to an Integer using #to_int" do
115
+ a = Itr[1, 2, 3, 4]
116
+ a.shift(2.3).should == [1, 2]
117
+
118
+ obj = mock('to_int')
119
+ obj.should_receive(:to_int).and_return(2)
120
+ a.should == Itr[3, 4]
121
+ a.shift(obj).should == [3, 4]
122
+ a.should == Itr[]
123
+ end
124
+
125
+ it "raises a TypeError when the passed n can be coerced to Integer" do
126
+ lambda{ Itr[1, 2].shift("cat") }.should raise_error(TypeError)
127
+ lambda{ Itr[1, 2].shift(nil) }.should raise_error(TypeError)
128
+ end
129
+
130
+ it "raises an ArgumentError if more arguments are passed" do
131
+ lambda{ Itr[1, 2].shift(1, 2) }.should raise_error(ArgumentError)
132
+ end
133
+
134
+ it "returns ruby arrays" do
135
+ Itr[1, 2, 3].shift(2).should be_kind_of(Array)
136
+ end
137
+
138
+ it "returns an untainted iterable even if the iterable is tainted" do
139
+ ary = Itr[1, 2].taint
140
+ ary.shift(2).tainted?.should be_false
141
+ ary.shift(0).tainted?.should be_false
142
+ end
143
+
144
+ it "keeps taint status" do
145
+ a = Itr[1, 2].taint
146
+ a.shift(2)
147
+ a.tainted?.should be_true
148
+ a.shift(2)
149
+ a.tainted?.should be_true
150
+ end
151
+
152
+ ruby_version_is '' ... '1.9' do
153
+ it "raises a TypeError on a frozen iterable" do
154
+ lambda { IterableSpecs.frozen_iterable.shift(2) }.should raise_error(TypeError)
155
+ lambda { IterableSpecs.frozen_iterable.shift(0) }.should raise_error(TypeError)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end