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,95 @@
1
+ shared_examples_for :hash_update do |method|
2
+ before { @method = method }
3
+
4
+ =begin self
5
+ it "returns self" do
6
+ h = new_hash(:_1 => 'a', :_2 => '3')
7
+ h.send(@method, :_1 => '9', :_9 => 2).should equal(h)
8
+ end
9
+ =end
10
+
11
+ it "adds the entries from other, overwriting duplicate keys" do
12
+ h = new_hash(:_1 => 'a', :_2 => '3')
13
+ h.send(@method, :_1 => '9', :_9 => 2)
14
+ h.should == new_hash(:_1 => "9", :_2 => "3", :_9 => 2)
15
+ end
16
+
17
+ =begin self
18
+ it "returns self with block" do
19
+ h1 = new_hash(:a => 2, :b => -1)
20
+ h2 = new_hash(:a => -2, :c => 1)
21
+ h1.send(@method, h2) { |k,x,y| 3.14 }.should equal(h1)
22
+ end
23
+ =end
24
+
25
+ =begin nil
26
+ it "sets any duplicate key to the value of block if passed a block" do
27
+ h1 = new_hash(:a => 2, :b => -1)
28
+ h2 = new_hash(:a => -2, :c => 1)
29
+ h1.send(@method, h2) { |k,x,y| 3.14 }
30
+ h1.should == new_hash(:c => 1, :b => -1, :a => 3.14)
31
+
32
+ h1.send(@method, h1) { nil }
33
+ h1.should == new_hash(:a => nil, :b => nil, :c => nil)
34
+ end
35
+ =end
36
+
37
+ it "tries to convert the passed argument to a hash using #to_hash" do
38
+ obj = mock('{1=>2}')
39
+ obj.should_receive(:to_hash).and_return(new_hash(1 => 2).to_hash)
40
+ new_hash(3 => 4).send(@method, obj).should == new_hash(1 => 2, 3 => 4)
41
+ end
42
+
43
+ it "does not call to_hash on hash subclasses" do
44
+ new_hash(3 => 4).send(@method, ToHashHash[1 => 2]).should == new_hash(1 => 2, 3 => 4)
45
+ end
46
+
47
+ =begin nil
48
+ it "processes entries with same order as merge()" do
49
+ h = new_hash(1 => 2, 3 => 4, 5 => 6, "x" => nil, nil => 5, [] => [])
50
+ merge_bang_pairs = []
51
+ merge_pairs = []
52
+ h.merge(h) { |*arg| merge_pairs << arg }
53
+ h.send(@method, h) { |*arg| merge_bang_pairs << arg }
54
+ merge_bang_pairs.should == merge_pairs
55
+ end
56
+ =end
57
+
58
+ =begin frozen
59
+ ruby_version_is ""..."1.9" do
60
+ it "raises a TypeError if called on a non-empty, frozen instance" do
61
+ lambda { HashSpecs.frozen_hash.send(@method, 1 => 2) }.should raise_error(TypeError)
62
+ end
63
+
64
+ it "does not raise an exception on a frozen instance that would not be modified" do
65
+ hash = HashSpecs.frozen_hash.send(@method, HashSpecs.empty_frozen_hash)
66
+ hash.should == HashSpecs.frozen_hash
67
+ end
68
+ end
69
+
70
+ ruby_version_is "1.9" do
71
+ it "raises a RuntimeError on a frozen instance that is modified" do
72
+ lambda do
73
+ HashSpecs.frozen_hash.send(@method, 1 => 2)
74
+ end.should raise_error(RuntimeError)
75
+ end
76
+
77
+ it "checks frozen status before coercing an object with #to_hash" do
78
+ obj = mock("to_hash frozen")
79
+ # This is necessary because mock cleanup code cannot run on the frozen
80
+ # object.
81
+ def obj.to_hash() raise Exception, "should not receive #to_hash" end
82
+ obj.freeze
83
+
84
+ lambda { HashSpecs.frozen_hash.send(@method, obj) }.should raise_error(RuntimeError)
85
+ end
86
+
87
+ # see redmine #1571
88
+ it "raises a RuntimeError on a frozen instance that would not be modified" do
89
+ lambda do
90
+ HashSpecs.frozen_hash.send(@method, HashSpecs.empty_frozen_hash)
91
+ end.should raise_error(RuntimeError)
92
+ end
93
+ end
94
+ =end
95
+ end
@@ -0,0 +1,16 @@
1
+ shared_examples_for :hash_value_p do |method|
2
+ before { @method = method }
3
+
4
+ it "returns true if the value exists in the hash" do
5
+ new_hash(:a => :b).send(@method, :a).should == false
6
+ new_hash(1 => 2).send(@method, 2).should == true
7
+ h = new_hash 5
8
+ h.send(@method, 5).should == false
9
+ h = new_hash { 5 }
10
+ h.send(@method, 5).should == false
11
+ end
12
+
13
+ it "uses == semantics for comparing values" do
14
+ new_hash(5 => 2.0).send(@method, 2).should == true
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ shared_examples_for :hash_values_at do |method|
2
+ before { @method = method }
3
+
4
+ it "returns an array of values for the given keys" do
5
+ h = new_hash(:a => 9, :b => 'a', :c => -10, :d => nil)
6
+ h.send(@method).should be_kind_of(Array)
7
+ h.send(@method).should == []
8
+ h.send(@method, :a, :d, :b).should be_kind_of(Array)
9
+ h.send(@method, :a, :d, :b).should == [9, nil, 'a']
10
+ end
11
+ end
12
+
@@ -0,0 +1,44 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#shift" do
5
+ =begin shift
6
+ it "removes a pair from hash and return it" do
7
+ h = new_hash(:a => 1, :b => 2, "c" => 3, nil => 4, [] => 5)
8
+ h2 = h.dup
9
+
10
+ h.size.times do |i|
11
+ r = h.shift
12
+ r.should be_kind_of(Array)
13
+ h2[r.first].should == r.last
14
+ h.size.should == h2.size - i - 1
15
+ end
16
+
17
+ h.should == new_hash
18
+ end
19
+
20
+ it "returns nil from an empty hash " do
21
+ new_hash.shift.should == nil
22
+ end
23
+
24
+ it "returns (computed) default for empty hashes" do
25
+ new_hash(5).shift.should == 5
26
+ h = new_hash { |*args| args }
27
+ h.shift.should == [h, nil]
28
+ end
29
+
30
+ ruby_version_is "" ... "1.9" do
31
+ it "raises a TypeError if called on a frozen instance" do
32
+ lambda { HashSpecs.frozen_hash.shift }.should raise_error(TypeError)
33
+ lambda { HashSpecs.empty_frozen_hash.shift }.should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ ruby_version_is "1.9" do
38
+ it "raises a RuntimeError if called on a frozen instance" do
39
+ lambda { HashSpecs.frozen_hash.shift }.should raise_error(RuntimeError)
40
+ lambda { HashSpecs.empty_frozen_hash.shift }.should raise_error(RuntimeError)
41
+ end
42
+ end
43
+ =end
44
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/length', __FILE__)
4
+
5
+ describe "Hash#size" do
6
+ it_behaves_like(:hash_length, :size)
7
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#sort" do
5
+ it "converts self to a nested array of [key, value] arrays and sort with Array#sort" do
6
+ new_hash('a' => 'b', '1' => '2', 'b' => 'a').sort.should ==
7
+ [["1", "2"], ["a", "b"], ["b", "a"]]
8
+ end
9
+
10
+ it "works when some of the keys are themselves arrays" do
11
+ new_hash([1,2] => 5, [1,1] => 5).sort.should == [[[1,1],5], [[1,2],5]]
12
+ end
13
+
14
+ it "uses block to sort array if passed a block" do
15
+ new_hash(1 => 2, 2 => 9, 3 => 4).sort { |a,b| b <=> a }.should == [[3, 4], [2, 9], [1, 2]]
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/store', __FILE__)
4
+
5
+ describe "Hash#store" do
6
+ it_behaves_like(:hash_store, :store)
7
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#to_a" do
5
+ it "returns a list of [key, value] pairs with same order as each()" do
6
+ h = new_hash(:a => 1, 1 => :a, 3 => :b, :b => 5)
7
+ pairs = []
8
+
9
+ h.each_pair do |key, value|
10
+ pairs << [key, value]
11
+ end
12
+
13
+ h.to_a.should be_kind_of(Array)
14
+ h.to_a.should == pairs
15
+ end
16
+
17
+ it "is called for Enumerable#entries" do
18
+ h = new_hash(:a => 1, 1 => :a, 3 => :b, :b => 5)
19
+ pairs = []
20
+
21
+ h.each_pair do |key, value|
22
+ pairs << [key, value]
23
+ end
24
+
25
+ ent = h.entries
26
+ ent.should be_kind_of(Array)
27
+ ent.should == pairs
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#to_hash" do
5
+ =begin self
6
+ it "returns self" do
7
+ h = new_hash
8
+ h.to_hash.should equal(h)
9
+ end
10
+ =end
11
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/to_s', __FILE__)
4
+
5
+ describe "Hash#to_s" do
6
+
7
+ ruby_version_is "1.9" do
8
+ it_behaves_like :to_s, :to_s
9
+ end
10
+
11
+ ruby_version_is ""..."1.9" do
12
+ it "returns a string by calling Hash#to_a and using Array#join with default separator" do
13
+ h = new_hash(:fun => 'x', 1 => 3, nil => "ok", [] => :y)
14
+ h.to_a.to_s.should == h.to_s
15
+ old, $, = $,, ':'
16
+ h.to_a.to_s.should == h.to_s
17
+ $, = old
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "1.8.8" do
4
+ describe "Hash.try_convert" do
5
+ it "returns the argument if passed a Hash" do
6
+ h = {:foo => :glark}
7
+ Hash.try_convert(h).should == h
8
+ end
9
+
10
+ it "returns nil if the argument can't be coerced into a Hash" do
11
+ Hash.try_convert(Object.new).should be_nil
12
+ end
13
+
14
+ it "does not rescue exceptions raised by #to_hash" do
15
+ obj = mock("to_hash raises")
16
+ obj.should_receive(:to_hash).and_raise(RuntimeError)
17
+ lambda { Hash.try_convert obj }.should raise_error(RuntimeError)
18
+ end
19
+
20
+ it "coerces the argument with #to_hash" do
21
+ obj = mock('to_hash')
22
+ obj.should_receive(:to_hash).and_return({:foo => :bar})
23
+ Hash.try_convert(obj).should == {:foo => :bar}
24
+ end
25
+
26
+ it "raises a TypeError if #to_hash does not return a Hash" do
27
+ obj = mock("to_hash invalid")
28
+ obj.should_receive(:to_hash).and_return(:invalid_to_hash)
29
+ lambda { Hash.try_convert obj }.should raise_error(TypeError)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/update', __FILE__)
4
+
5
+ describe "Hash#update" do
6
+ it_behaves_like(:hash_update, :update)
7
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/value', __FILE__)
4
+
5
+ describe "Hash#value?" do
6
+ it_behaves_like(:hash_value_p, :value?)
7
+ end
8
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/values_at', __FILE__)
4
+
5
+ describe "Hash#values_at" do
6
+ it_behaves_like(:hash_values_at, :values_at)
7
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#values" do
5
+ it "returns an array of values" do
6
+ h = new_hash(1 => :a, 'a' => :a, 'the' => 'lang')
7
+ h.values.to_a.should be_kind_of(Array)
8
+ h.values.sort {|a, b| a.to_s <=> b.to_s}.should == [:a, :a, 'lang']
9
+ end
10
+ end
@@ -0,0 +1,77 @@
1
+ require 'jactive_support/core_ext/locale'
2
+
3
+ describe Locale do
4
+ before {java::util::Locale.setDefault(java::util::Locale.new('en'))}
5
+ subject {Locale.new('en', 'UK')}
6
+
7
+ it { Locale.should be(java.util.Locale) }
8
+
9
+ it "should have a current locale" do
10
+ Locale.current_locale.should be(Locale.getDefault)
11
+ end
12
+
13
+ it "has a human name" do
14
+ subject.human_name.should eq('English (UK)')
15
+ end
16
+
17
+ it "supports inspect" do
18
+ subject.inspect.should eq('Locale[English (UK)]')
19
+ end
20
+
21
+ it "supports to_s" do
22
+ subject.to_s.should eq('en_UK')
23
+ end
24
+
25
+ it "supports to_str" do
26
+ subject.to_str.should eq('en_UK')
27
+ end
28
+
29
+ it "supports to_sym" do
30
+ subject.to_sym.should eq(:en_UK)
31
+ end
32
+
33
+ it "can convert string to locale" do
34
+ "en".to_locale.should eq(Locale.new('en'))
35
+ "en_UK".to_locale.should eq(Locale.new('en', 'UK'))
36
+ "en-UK".to_locale.should eq(Locale.new('en', 'UK'))
37
+ "en_UK_Ireland".to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
38
+ "en-UK-Ireland".to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
39
+ "en_UK-Ireland".to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
40
+ "en-UK_Ireland".to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
41
+ end
42
+
43
+ it "can convert symbol to locale" do
44
+ :en.to_locale.should eq(Locale.new('en'))
45
+ :en_UK.to_locale.should eq(Locale.new('en', 'UK'))
46
+ :'en-UK'.to_locale.should eq(Locale.new('en', 'UK'))
47
+ :en_UK_Ireland.to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
48
+ :'en-UK-Ireland'.to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
49
+ :'en_UK-Ireland'.to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
50
+ :'en-UK_Ireland'.to_locale.should eq(Locale.new('en', 'UK', 'Ireland'))
51
+ end
52
+
53
+ it "converting nil to a locale is the same as the default locale" do
54
+ nil.to_locale.should be(Locale.current_locale)
55
+ end
56
+
57
+ it "can set current locale" do
58
+ Locale.current_locale = Locale.new('en')
59
+ Locale.current_locale.should eq(:en.to_locale)
60
+ end
61
+
62
+ it "can set current locale to a string" do
63
+ Locale.current_locale = 'en'
64
+ Locale.current_locale.should eq(:en.to_locale)
65
+ end
66
+
67
+ it "can set current locale to a symbol" do
68
+ Locale.current_locale = :en
69
+ Locale.current_locale.should eq(:en.to_locale)
70
+ end
71
+
72
+ it "can set current locale to nil and it has no effect" do
73
+ old = Locale.current_locale
74
+ Locale.current_locale = nil
75
+ Locale.current_locale.should eq(old)
76
+ end
77
+ end
@@ -0,0 +1,7 @@
1
+ def enumerator_class
2
+ [].each.class
3
+ end
4
+
5
+ def ruby_bug(*args)
6
+ yield
7
+ end
@@ -0,0 +1,123 @@
1
+ class SpecVersion
2
+ # If beginning implementations have a problem with this include, we can
3
+ # manually implement the relational operators that are needed.
4
+ include Comparable
5
+
6
+ # SpecVersion handles comparison correctly for the context by filling in
7
+ # missing version parts according to the value of +ceil+. If +ceil+ is
8
+ # +false+, 0 digits fill in missing version parts. If +ceil+ is +true+, 9
9
+ # digits fill in missing parts. (See e.g. VersionGuard and BugGuard.)
10
+ def initialize(version, ceil = false)
11
+ @version = version
12
+ @ceil = ceil
13
+ @integer = nil
14
+ end
15
+
16
+ def to_s
17
+ @version
18
+ end
19
+
20
+ def to_str
21
+ to_s
22
+ end
23
+
24
+ # Converts a string representation of a version major.minor.tiny.patchlevel
25
+ # to an integer representation so that comparisons can be made. For example,
26
+ # "1.8.6.77" < "1.8.6.123" would be false if compared as strings.
27
+ def to_i
28
+ unless @integer
29
+ major, minor, tiny, patch = @version.split "."
30
+ if @ceil
31
+ tiny = 99 unless tiny
32
+ patch = 9999 unless patch
33
+ end
34
+ parts = [major, minor, tiny, patch].map { |x| x.to_i }
35
+ @integer = ("1%02d%02d%02d%04d" % parts).to_i
36
+ end
37
+ @integer
38
+ end
39
+
40
+ def to_int
41
+ to_i
42
+ end
43
+
44
+ def <=>(other)
45
+ if other.respond_to? :to_int
46
+ other = Integer other
47
+ else
48
+ other = SpecVersion.new(String(other)).to_i
49
+ end
50
+
51
+ self.to_i <=> other
52
+ end
53
+ end
54
+
55
+ class VersionGuard
56
+ @@ruby_version_override = nil
57
+
58
+ def self.ruby_version_override=(version)
59
+ @@ruby_version_override = version
60
+ end
61
+
62
+ def self.ruby_version_override
63
+ @@ruby_version_override
64
+ end
65
+
66
+ # Returns a partial Ruby version string based on +which+. For example,
67
+ # if RUBY_VERSION = 8.2.3 and RUBY_PATCHLEVEL = 71:
68
+ #
69
+ # :major => "8"
70
+ # :minor => "8.2"
71
+ # :tiny => "8.2.3"
72
+ # :teeny => "8.2.3"
73
+ # :full => "8.2.3.71"
74
+ def self.ruby_version(which = :minor)
75
+ case which
76
+ when :major
77
+ n = 1
78
+ when :minor
79
+ n = 2
80
+ when :tiny, :teeny
81
+ n = 3
82
+ else
83
+ n = 4
84
+ end
85
+
86
+ patch = RUBY_PATCHLEVEL.to_i
87
+ patch = 0 if patch < 0
88
+ version = "#{ruby_version_override || RUBY_VERSION}.#{patch}"
89
+ version.split('.')[0,n].join('.')
90
+ end
91
+
92
+ def initialize(version)
93
+ case version
94
+ when String
95
+ @version = SpecVersion.new version
96
+ when Range
97
+ a = SpecVersion.new version.first
98
+ b = SpecVersion.new version.last
99
+ @version = version.exclude_end? ? a...b : a..b
100
+ end
101
+ end
102
+
103
+ def ruby_version
104
+ @ruby_version ||= SpecVersion.new self.class.ruby_version(:full)
105
+ end
106
+
107
+ def match?
108
+ if Range === @version
109
+ @version.include? ruby_version
110
+ else
111
+ ruby_version >= @version
112
+ end
113
+ end
114
+ end
115
+
116
+ class Object
117
+ def ruby_version_is(*args)
118
+ g = VersionGuard.new(*args)
119
+ yield if g.match?
120
+ end
121
+ end
122
+
123
+ puts "Ruby is " + VersionGuard.ruby_version(:full)
@@ -0,0 +1,50 @@
1
+ require 'java'
2
+ require File.expand_path('../shared/version', __FILE__)
3
+ require File.expand_path('../shared/fixtures', __FILE__)
4
+
5
+ require 'jactive_support/java_ext/map/constructor'
6
+ class java::util::HashMap
7
+ include JactiveSupport::JavaExtensions::Map::Constructor
8
+ end
9
+
10
+ class Object
11
+ # The following helpers provide a level of indirection for running the specs
12
+ # against a Hash implementation that has a different name than Hash.
13
+
14
+ # Returns the Hash class.
15
+ unless method_defined?(:hash_class)
16
+ def hash_class
17
+ java::util::LinkedHashMap
18
+ end
19
+ end
20
+
21
+ unless method_defined?(:ruby_hash_class)
22
+ def ruby_hash_class
23
+ Hash
24
+ end
25
+ end
26
+
27
+ # Returns a new instance of hash_class.
28
+ def new_hash(*args, &block)
29
+ if block
30
+ r = hash_class.new
31
+ r.default = block
32
+ r
33
+ elsif args.size == 1
34
+ value = args.first
35
+ if value.is_a?(Hash) or value.is_a?(hash_class)
36
+ r = hash_class.new
37
+ r.putAll(value)
38
+ r
39
+ else
40
+ r = hash_class.new
41
+ r.default = value
42
+ r
43
+ end
44
+ else
45
+ r = hash_class.new
46
+ args.each_slice(2) {|key,val| r.put(key,val)}
47
+ r
48
+ end
49
+ end
50
+ end