jactive_support 1.0.2
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.
- data/.gitignore +4 -0
- data/.rvmrc +63 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/Rakefile +45 -0
- data/jactive_support.gemspec +24 -0
- data/lib/jactive_support.rb +6 -0
- data/lib/jactive_support/constantize.rb +19 -0
- data/lib/jactive_support/core_ext.rb +4 -0
- data/lib/jactive_support/core_ext/enum.rb +43 -0
- data/lib/jactive_support/core_ext/hash.rb +2 -0
- data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
- data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
- data/lib/jactive_support/core_ext/locale.rb +28 -0
- data/lib/jactive_support/core_ext/module.rb +9 -0
- data/lib/jactive_support/core_ext/numeric.rb +7 -0
- data/lib/jactive_support/core_ext/ordering.rb +17 -0
- data/lib/jactive_support/core_ext/to_java.rb +9 -0
- data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
- data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
- data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
- data/lib/jactive_support/java_ext.rb +4 -0
- data/lib/jactive_support/java_ext/date.rb +37 -0
- data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
- data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
- data/lib/jactive_support/java_ext/enum.rb +19 -0
- data/lib/jactive_support/java_ext/iterable.rb +74 -0
- data/lib/jactive_support/java_ext/iterator.rb +8 -0
- data/lib/jactive_support/java_ext/list.rb +65 -0
- data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
- data/lib/jactive_support/java_ext/locale.rb +32 -0
- data/lib/jactive_support/java_ext/map.rb +6 -0
- data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
- data/lib/jactive_support/java_ext/map/hash.rb +166 -0
- data/lib/jactive_support/java_ext/number.rb +91 -0
- data/lib/jactive_support/java_ext/sql_date.rb +7 -0
- data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
- data/lib/jactive_support/json.rb +1 -0
- data/lib/jactive_support/json/encoders/collection.rb +12 -0
- data/lib/jactive_support/json/encoders/locale.rb +6 -0
- data/lib/jactive_support/json/encoders/map.rb +55 -0
- data/lib/jactive_support/json/encoding.rb +5 -0
- data/lib/jactive_support/rescuable.rb +62 -0
- data/lib/jactive_support/version.rb +3 -0
- data/spec/constantize_spec.rb +57 -0
- data/spec/core_ext/hash_spec.rb +34 -0
- data/spec/core_ext/module_spec.rb +19 -0
- data/spec/core_ext/ordering_spec.rb +12 -0
- data/spec/enum_spec.rb +67 -0
- data/spec/java_ext/iterable/clear_spec.rb +56 -0
- data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
- data/spec/java_ext/iterable/empty_spec.rb +10 -0
- data/spec/java_ext/iterable/reject_spec.rb +76 -0
- data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
- data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
- data/spec/java_ext/iterable/shift_spec.rb +160 -0
- data/spec/java_ext/list/clear_spec.rb +56 -0
- data/spec/java_ext/list/collect_spec.rb +11 -0
- data/spec/java_ext/list/last_spec.rb +87 -0
- data/spec/java_ext/list/map_spec.rb +11 -0
- data/spec/java_ext/list/push_spec.rb +44 -0
- data/spec/java_ext/list/shared/collect.rb +133 -0
- data/spec/java_ext/list/shared/fixtures.rb +34 -0
- data/spec/java_ext/map/assoc_spec.rb +43 -0
- data/spec/java_ext/map/clear_spec.rb +49 -0
- data/spec/java_ext/map/clone_spec.rb +13 -0
- data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
- data/spec/java_ext/map/constructor_spec.rb +54 -0
- data/spec/java_ext/map/delete_if_spec.rb +54 -0
- data/spec/java_ext/map/delete_spec.rb +38 -0
- data/spec/java_ext/map/each_key_spec.rb +29 -0
- data/spec/java_ext/map/each_pair_spec.rb +38 -0
- data/spec/java_ext/map/each_spec.rb +9 -0
- data/spec/java_ext/map/each_value_spec.rb +28 -0
- data/spec/java_ext/map/element_reference_spec.rb +119 -0
- data/spec/java_ext/map/element_set_spec.rb +7 -0
- data/spec/java_ext/map/empty_spec.rb +15 -0
- data/spec/java_ext/map/eql_spec.rb +19 -0
- data/spec/java_ext/map/equal_value_spec.rb +18 -0
- data/spec/java_ext/map/fetch_spec.rb +35 -0
- data/spec/java_ext/map/fixtures/classes.rb +36 -0
- data/spec/java_ext/map/flatten_spec.rb +64 -0
- data/spec/java_ext/map/has_key_spec.rb +8 -0
- data/spec/java_ext/map/has_value_spec.rb +8 -0
- data/spec/java_ext/map/hash_spec.rb +53 -0
- data/spec/java_ext/map/include_spec.rb +7 -0
- data/spec/java_ext/map/index_spec.rb +7 -0
- data/spec/java_ext/map/indexes_spec.rb +9 -0
- data/spec/java_ext/map/indices_spec.rb +9 -0
- data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
- data/spec/java_ext/map/initialize_spec.rb +64 -0
- data/spec/java_ext/map/inspect_spec.rb +9 -0
- data/spec/java_ext/map/invert_spec.rb +27 -0
- data/spec/java_ext/map/keep_if_spec.rb +33 -0
- data/spec/java_ext/map/key_spec.rb +14 -0
- data/spec/java_ext/map/keys_spec.rb +39 -0
- data/spec/java_ext/map/length_spec.rb +7 -0
- data/spec/java_ext/map/member_spec.rb +7 -0
- data/spec/java_ext/map/merge_spec.rb +69 -0
- data/spec/java_ext/map/rassoc_spec.rb +39 -0
- data/spec/java_ext/map/reject_spec.rb +123 -0
- data/spec/java_ext/map/replace_spec.rb +7 -0
- data/spec/java_ext/map/select_spec.rb +98 -0
- data/spec/java_ext/map/shared/each.rb +77 -0
- data/spec/java_ext/map/shared/eql.rb +224 -0
- data/spec/java_ext/map/shared/equal.rb +94 -0
- data/spec/java_ext/map/shared/index.rb +29 -0
- data/spec/java_ext/map/shared/iteration.rb +28 -0
- data/spec/java_ext/map/shared/key.rb +43 -0
- data/spec/java_ext/map/shared/length.rb +12 -0
- data/spec/java_ext/map/shared/replace.rb +82 -0
- data/spec/java_ext/map/shared/store.rb +68 -0
- data/spec/java_ext/map/shared/to_s.rb +68 -0
- data/spec/java_ext/map/shared/update.rb +95 -0
- data/spec/java_ext/map/shared/value.rb +16 -0
- data/spec/java_ext/map/shared/values_at.rb +12 -0
- data/spec/java_ext/map/shift_spec.rb +44 -0
- data/spec/java_ext/map/size_spec.rb +7 -0
- data/spec/java_ext/map/sort_spec.rb +17 -0
- data/spec/java_ext/map/store_spec.rb +7 -0
- data/spec/java_ext/map/to_a_spec.rb +29 -0
- data/spec/java_ext/map/to_hash_spec.rb +11 -0
- data/spec/java_ext/map/to_s_spec.rb +20 -0
- data/spec/java_ext/map/try_convert_spec.rb +32 -0
- data/spec/java_ext/map/update_spec.rb +7 -0
- data/spec/java_ext/map/value_spec.rb +8 -0
- data/spec/java_ext/map/values_at_spec.rb +7 -0
- data/spec/java_ext/map/values_spec.rb +10 -0
- data/spec/locale_spec.rb +77 -0
- data/spec/shared/fixtures.rb +7 -0
- data/spec/shared/version.rb +123 -0
- data/spec/spec_helper.rb +50 -0
- metadata +332 -0
@@ -0,0 +1,91 @@
|
|
1
|
+
class java::lang::Number
|
2
|
+
def self.number_format_instance(format=:default, locale=nil)
|
3
|
+
case format
|
4
|
+
when :number then ::Java::JavaText::NumberFormat.getNumberInstance(locale.to_locale)
|
5
|
+
when :currency then ::Java::JavaText::NumberFormat.getCurrencyInstance(locale.to_locale)
|
6
|
+
when :percent then ::Java::JavaText::NumberFormat.getPercentInstance(locale.to_locale)
|
7
|
+
when :scientific then ::Java::JavaText::NumberFormat.getScientificInstance(locale.to_locale)
|
8
|
+
when :integer then ::Java::JavaText::NumberFormat.getIntegerInstance(locale.to_locale)
|
9
|
+
when :default then ::Java::JavaText::NumberFormat.getInstance(locale.to_locale)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
NUMBER_FORMATS = {
|
14
|
+
:number => lambda {|value, locale| number_format_instance(:number, locale).format(value.to_primitive)},
|
15
|
+
:currency => lambda {|value, locale| number_format_instance(:currency, locale).format(value.to_primitive)},
|
16
|
+
:percent => lambda {|value, locale| number_format_instance(:percent, locale).format(value.to_primitive)},
|
17
|
+
:scientific => lambda {|value, locale| number_format_instance(:scientific, locale).format(value.to_primitive)},
|
18
|
+
:integer => lambda {|value, locale| number_format_instance(:integer, locale).format(value.to_primitive)},
|
19
|
+
:default => lambda {|value, locale| number_format_instance(:default, locale).format(value.to_primitive)},
|
20
|
+
}
|
21
|
+
|
22
|
+
def to_formatted_s(format=:default, locale=nil)
|
23
|
+
return to_default_s unless formatter = self.class::NUMBER_FORMATS[format]
|
24
|
+
formatter.respond_to?(:call) ? (formatter.arity==2 ? formatter.call(self, locale) : formatter.call(self)).to_s : format_number(formatter)
|
25
|
+
end
|
26
|
+
alias :to_default_s :to_s
|
27
|
+
alias :to_s :to_formatted_s
|
28
|
+
|
29
|
+
def format_number(format, locale=nil)
|
30
|
+
symbols = ::Java::JavaText::DecimalFormatSymbols.new(locale.to_locale)
|
31
|
+
formatter = ::Java::JavaText::DecimalFormat.new(format, symbols)
|
32
|
+
formatter.format(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def to_primitive
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class java::lang::Byte
|
41
|
+
#alias :to_default_s :to_s
|
42
|
+
#alias :to_s :to_formatted_s
|
43
|
+
|
44
|
+
alias :to_primitive :byte_value
|
45
|
+
end
|
46
|
+
|
47
|
+
class java::lang::Short
|
48
|
+
#alias :to_default_s :to_s
|
49
|
+
#alias :to_s :to_formatted_s
|
50
|
+
|
51
|
+
alias :to_primitive :short_value
|
52
|
+
end
|
53
|
+
|
54
|
+
class java::lang::Integer
|
55
|
+
#alias :to_default_s :to_s
|
56
|
+
#alias :to_s :to_formatted_s
|
57
|
+
|
58
|
+
alias :to_primitive :int_value
|
59
|
+
end
|
60
|
+
|
61
|
+
class java::lang::Long
|
62
|
+
#alias :to_default_s :to_s
|
63
|
+
#alias :to_s :to_formatted_s
|
64
|
+
|
65
|
+
alias :to_primitive :long_value
|
66
|
+
end
|
67
|
+
|
68
|
+
class java::lang::Float
|
69
|
+
#alias :to_default_s :to_s
|
70
|
+
#alias :to_s :to_formatted_s
|
71
|
+
|
72
|
+
alias :to_primitive :float_value
|
73
|
+
end
|
74
|
+
|
75
|
+
class java::lang::Double
|
76
|
+
#alias :to_default_s :to_s
|
77
|
+
#alias :to_s :to_formatted_s
|
78
|
+
|
79
|
+
alias :to_primitive :double_value
|
80
|
+
end
|
81
|
+
|
82
|
+
|
83
|
+
#class Java::JavaMath::BigDecimal
|
84
|
+
# alias :to_default_s :to_s
|
85
|
+
# alias :to_s :to_formatted_s
|
86
|
+
#end
|
87
|
+
|
88
|
+
#class Java::JavaMath::BigInteger
|
89
|
+
# alias :to_default_s :to_s
|
90
|
+
# alias :to_s :to_formatted_s
|
91
|
+
#end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module JactiveSupport #:nodoc:
|
2
|
+
module JavaExtensions #:nodoc:
|
3
|
+
module SqlDate #:nodoc:
|
4
|
+
# Converting dates to formatted strings, times, and datetimes.
|
5
|
+
module Conversions
|
6
|
+
DATE_FORMATS = {
|
7
|
+
:db => "yyyy-MM-dd",
|
8
|
+
:i18n => lambda { |clazz, locale|
|
9
|
+
format = I18n.translate(:"formats.date", :default=>'')
|
10
|
+
!format.blank? ? clazz.pattern_formatter(format) : clazz.date_instance(:default, locale)
|
11
|
+
},
|
12
|
+
:number => "YYYMMdd",
|
13
|
+
:full => lambda { |clazz, locale| clazz.date_instance(:full, locale) },
|
14
|
+
:long => lambda { |clazz, locale| clazz.date_instance(:long, locale) },
|
15
|
+
:medium => lambda { |clazz, locale| clazz.date_instance(:medium, locale) },
|
16
|
+
:short => lambda { |clazz, locale| clazz.date_instance(:short, locale) },
|
17
|
+
:default => lambda { |clazz, locale| clazz.date_instance(:default, locale) },
|
18
|
+
:long_ordinal => lambda { |clazz| clazz.pattern_formatter("%B #{time.day.ordinalize}, %Y %H:%M") },
|
19
|
+
:rfc822 => lambda { |clazz| clazz.pattern_formatter("%a, %d %b %Y %H:%M:%S #{time.formatted_offset(false)}") },
|
20
|
+
:httpdate => lambda { |clazz| clazz.pattern_formatter("EEE, dd MMM yyyy HH:mm:ss z", "GMT") }
|
21
|
+
}
|
22
|
+
|
23
|
+
def to_java_sqldate
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
def acts_like_time?
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'jactive_support/json/encoding'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module java::util::Collection #:nodoc:
|
2
|
+
|
3
|
+
# Returns a JSON string representing the Array. +options+ are passed to each element.
|
4
|
+
def to_json(options = nil) #:nodoc:
|
5
|
+
"[#{map { |value| ActiveSupport::JSON.encode(value, options) } * ','}]"
|
6
|
+
end
|
7
|
+
|
8
|
+
def as_json(options = nil) #:nodoc:
|
9
|
+
self
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module java::util::Map #:nodoc:
|
2
|
+
# Returns a JSON string representing the hash.
|
3
|
+
#
|
4
|
+
# Without any +options+, the returned JSON string will include all
|
5
|
+
# the hash keys. For example:
|
6
|
+
#
|
7
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json
|
8
|
+
# # => {"name": "Konata Izumi", 1: 2, "age": 16}
|
9
|
+
#
|
10
|
+
# The keys in the JSON string are unordered due to the nature of hashes.
|
11
|
+
#
|
12
|
+
# The <tt>:only</tt> and <tt>:except</tt> options can be used to limit the
|
13
|
+
# attributes included, and will accept 1 or more hash keys to include/exclude.
|
14
|
+
#
|
15
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:only => [:name, 'age'])
|
16
|
+
# # => {"name": "Konata Izumi", "age": 16}
|
17
|
+
#
|
18
|
+
# { :name => "Konata Izumi", 'age' => 16, 1 => 2 }.to_json(:except => 1)
|
19
|
+
# # => {"name": "Konata Izumi", "age": 16}
|
20
|
+
#
|
21
|
+
# The +options+ also filter down to any hash values. This is particularly
|
22
|
+
# useful for converting hashes containing ActiveRecord objects or any object
|
23
|
+
# that responds to options in their <tt>to_json</tt> method. For example:
|
24
|
+
#
|
25
|
+
# users = User.find(:all)
|
26
|
+
# { :users => users, :count => users.size }.to_json(:include => :posts)
|
27
|
+
#
|
28
|
+
# would pass the <tt>:include => :posts</tt> option to <tt>users</tt>,
|
29
|
+
# allowing the posts association in the User model to be converted to JSON
|
30
|
+
# as well.
|
31
|
+
def to_json(options = {}) #:nodoc:
|
32
|
+
hash = as_json(options)
|
33
|
+
return ActiveSupport::JSON.encode(hash, options) if hash != self
|
34
|
+
|
35
|
+
result = '{'
|
36
|
+
result << hash.map do |key, value|
|
37
|
+
"#{ActiveSupport::JSON.encode(key.to_s)}:#{ActiveSupport::JSON.encode(value, options)}"
|
38
|
+
end * ','
|
39
|
+
result << '}'
|
40
|
+
end
|
41
|
+
|
42
|
+
def as_json(options = nil) #:nodoc:
|
43
|
+
if options
|
44
|
+
if attrs = options[:except]
|
45
|
+
except(*Array.wrap(attrs))
|
46
|
+
elsif attrs = options[:only]
|
47
|
+
slice(*Array.wrap(attrs))
|
48
|
+
else
|
49
|
+
self
|
50
|
+
end
|
51
|
+
else
|
52
|
+
self
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'active_support/rescuable'
|
2
|
+
|
3
|
+
module ActiveSupport
|
4
|
+
module Rescuable
|
5
|
+
module ClassMethods
|
6
|
+
def rescue_from(*klasses, &block)
|
7
|
+
options = klasses.extract_options!
|
8
|
+
|
9
|
+
unless options.has_key?(:with)
|
10
|
+
if block_given?
|
11
|
+
options[:with] = block
|
12
|
+
else
|
13
|
+
raise ArgumentError, "Need a handler. Supply an options hash that has a :with key as the last argument."
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
klasses.each do |klass|
|
18
|
+
key = if klass.is_a?(Class) && ( klass <= Exception || klass <= ::Java::JavaLang::Exception )
|
19
|
+
klass.name
|
20
|
+
elsif klass.is_a?(String)
|
21
|
+
klass
|
22
|
+
else
|
23
|
+
raise ArgumentError, "#{klass} is neither an Exception nor a String"
|
24
|
+
end
|
25
|
+
|
26
|
+
# put the new handler at the end because the list is read in reverse
|
27
|
+
self.rescue_handlers += [[key, options[:with]]]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def handler_for_rescue(exception)
|
33
|
+
# We go from right to left because pairs are pushed onto rescue_handlers
|
34
|
+
# as rescue_from declarations are found.
|
35
|
+
_, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
|
36
|
+
# The purpose of allowing strings in rescue_from is to support the
|
37
|
+
# declaration of handler associations for exception classes whose
|
38
|
+
# definition is yet unknown.
|
39
|
+
#
|
40
|
+
# Since this loop needs the constants it would be inconsistent to
|
41
|
+
# assume they should exist at this point. An early raised exception
|
42
|
+
# could trigger some other handler and the array could include
|
43
|
+
# precisely a string whose corresponding constant has not yet been
|
44
|
+
# seen. This is why we are tolerant to unknown constants.
|
45
|
+
#
|
46
|
+
# Note that this tolerance only matters if the exception was given as
|
47
|
+
# a string, otherwise a NameError will be raised by the interpreter
|
48
|
+
# itself when rescue_from CONSTANT is executed.
|
49
|
+
klass = self.class.const_get(klass_name) rescue nil
|
50
|
+
klass ||= klass_name.constantize rescue nil
|
51
|
+
exception.is_a?(klass) || (exception.is_a?(NativeException) && exception.cause.is_a?(klass)) if klass
|
52
|
+
end
|
53
|
+
|
54
|
+
case rescuer
|
55
|
+
when Symbol
|
56
|
+
method(rescuer)
|
57
|
+
when Proc
|
58
|
+
rescuer.bind(self)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -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
|