motion-support 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +3 -0
- data/Gemfile +1 -1
- data/README.md +231 -2
- data/Rakefile +3 -3
- data/app/app_delegate.rb +0 -2
- data/examples/Inflector/.gitignore +16 -0
- data/examples/Inflector/Gemfile +5 -0
- data/examples/Inflector/Rakefile +13 -0
- data/examples/Inflector/app/app_delegate.rb +8 -0
- data/examples/Inflector/app/inflections.rb +5 -0
- data/examples/Inflector/app/inflector_view_controller.rb +120 -0
- data/examples/Inflector/app/words_view_controller.rb +101 -0
- data/examples/Inflector/resources/Default-568h@2x.png +0 -0
- data/examples/Inflector/spec/main_spec.rb +9 -0
- data/lib/motion-support/core_ext/array.rb +13 -0
- data/lib/motion-support/core_ext/class.rb +8 -0
- data/lib/motion-support/core_ext/hash.rb +16 -0
- data/lib/motion-support/core_ext/integer.rb +9 -0
- data/lib/motion-support/core_ext/module.rb +14 -0
- data/lib/motion-support/core_ext/numeric.rb +8 -0
- data/lib/motion-support/core_ext/object.rb +14 -0
- data/lib/motion-support/core_ext/range.rb +8 -0
- data/lib/motion-support/core_ext/string.rb +14 -0
- data/lib/motion-support/core_ext/time.rb +19 -0
- data/lib/motion-support/core_ext.rb +3 -0
- data/lib/motion-support/inflector.rb +12 -156
- data/lib/motion-support.rb +5 -5
- data/motion/_stdlib/cgi.rb +22 -0
- data/motion/_stdlib/date.rb +77 -0
- data/motion/_stdlib/time.rb +19 -0
- data/motion/core_ext/array/access.rb +28 -0
- data/motion/core_ext/array/conversions.rb +86 -0
- data/motion/core_ext/array/extract_options.rb +11 -0
- data/motion/core_ext/array/grouping.rb +99 -0
- data/motion/core_ext/array/prepend_and_append.rb +7 -0
- data/motion/core_ext/array/wrap.rb +45 -0
- data/{lib/motion-support → motion/core_ext}/array.rb +0 -4
- data/motion/core_ext/class/attribute.rb +119 -0
- data/motion/core_ext/class/attribute_accessors.rb +168 -0
- data/motion/core_ext/date/acts_like.rb +8 -0
- data/motion/core_ext/date/calculations.rb +117 -0
- data/motion/core_ext/date/conversions.rb +56 -0
- data/motion/core_ext/date_and_time/calculations.rb +232 -0
- data/motion/core_ext/enumerable.rb +90 -0
- data/motion/core_ext/hash/deep_merge.rb +27 -0
- data/motion/core_ext/hash/except.rb +15 -0
- data/motion/core_ext/hash/indifferent_access.rb +19 -0
- data/motion/core_ext/hash/keys.rb +138 -0
- data/motion/core_ext/hash/reverse_merge.rb +22 -0
- data/motion/core_ext/hash/slice.rb +40 -0
- data/motion/core_ext/integer/inflections.rb +27 -0
- data/motion/core_ext/integer/multiple.rb +10 -0
- data/motion/core_ext/integer/time.rb +41 -0
- data/{lib/motion-support → motion/core_ext}/metaclass.rb +0 -0
- data/motion/core_ext/module/aliasing.rb +69 -0
- data/motion/core_ext/module/anonymous.rb +19 -0
- data/motion/core_ext/module/attr_internal.rb +38 -0
- data/motion/core_ext/module/attribute_accessors.rb +64 -0
- data/motion/core_ext/module/delegation.rb +175 -0
- data/motion/core_ext/module/introspection.rb +60 -0
- data/motion/core_ext/module/reachable.rb +5 -0
- data/motion/core_ext/module/remove_method.rb +12 -0
- data/motion/core_ext/ns_dictionary.rb +11 -0
- data/motion/core_ext/numeric/bytes.rb +44 -0
- data/motion/core_ext/numeric/conversions.rb +7 -0
- data/motion/core_ext/numeric/time.rb +75 -0
- data/motion/core_ext/object/acts_like.rb +10 -0
- data/motion/core_ext/object/blank.rb +105 -0
- data/motion/core_ext/object/deep_dup.rb +44 -0
- data/motion/core_ext/object/duplicable.rb +83 -0
- data/motion/core_ext/object/instance_variables.rb +28 -0
- data/motion/core_ext/object/to_param.rb +58 -0
- data/motion/core_ext/object/to_query.rb +26 -0
- data/motion/core_ext/object/try.rb +78 -0
- data/motion/core_ext/range/include_range.rb +23 -0
- data/motion/core_ext/range/overlaps.rb +8 -0
- data/motion/core_ext/regexp.rb +5 -0
- data/motion/core_ext/string/access.rb +104 -0
- data/motion/core_ext/string/behavior.rb +6 -0
- data/motion/core_ext/string/exclude.rb +11 -0
- data/motion/core_ext/string/filters.rb +55 -0
- data/motion/core_ext/string/indent.rb +43 -0
- data/motion/core_ext/string/inflections.rb +195 -0
- data/motion/core_ext/string/starts_ends_with.rb +4 -0
- data/motion/core_ext/string/strip.rb +24 -0
- data/motion/core_ext/time/acts_like.rb +8 -0
- data/motion/core_ext/time/calculations.rb +215 -0
- data/motion/core_ext/time/conversions.rb +52 -0
- data/motion/duration.rb +104 -0
- data/motion/hash_with_indifferent_access.rb +251 -0
- data/motion/inflections.rb +67 -0
- data/motion/inflector/inflections.rb +203 -0
- data/motion/inflector/methods.rb +321 -0
- data/{lib/motion-support → motion}/logger.rb +0 -0
- data/{lib/motion-support → motion}/version.rb +1 -1
- data/motion-support.gemspec +2 -2
- data/spec/motion-support/_helpers/constantize_test_cases.rb +75 -0
- data/spec/motion-support/_helpers/inflector_test_cases.rb +313 -0
- data/spec/motion-support/core_ext/array/access_spec.rb +29 -0
- data/spec/motion-support/core_ext/array/conversion_spec.rb +60 -0
- data/spec/motion-support/core_ext/array/extract_options_spec.rb +15 -0
- data/spec/motion-support/core_ext/array/grouping_spec.rb +85 -0
- data/spec/motion-support/core_ext/array/prepend_and_append_spec.rb +25 -0
- data/spec/motion-support/core_ext/array/wrap_spec.rb +19 -0
- data/spec/motion-support/{array_spec.rb → core_ext/array_spec.rb} +0 -5
- data/spec/motion-support/core_ext/class/attribute_accessor_spec.rb +127 -0
- data/spec/motion-support/core_ext/class/attribute_spec.rb +92 -0
- data/spec/motion-support/core_ext/date/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/date/calculation_spec.rb +186 -0
- data/spec/motion-support/core_ext/date/conversion_spec.rb +18 -0
- data/spec/motion-support/core_ext/date_and_time/calculation_spec.rb +336 -0
- data/spec/motion-support/core_ext/enumerable_spec.rb +130 -0
- data/spec/motion-support/core_ext/hash/deep_merge_spec.rb +32 -0
- data/spec/motion-support/core_ext/hash/except_spec.rb +43 -0
- data/spec/motion-support/core_ext/hash/key_spec.rb +230 -0
- data/spec/motion-support/core_ext/hash/reverse_merge_spec.rb +26 -0
- data/spec/motion-support/core_ext/hash/slice_spec.rb +61 -0
- data/spec/motion-support/core_ext/integer/inflection_spec.rb +23 -0
- data/spec/motion-support/core_ext/integer/multiple_spec.rb +19 -0
- data/spec/motion-support/{metaclass_spec.rb → core_ext/metaclass_spec.rb} +0 -0
- data/spec/motion-support/core_ext/module/aliasing_spec.rb +143 -0
- data/spec/motion-support/core_ext/module/anonymous_spec.rb +29 -0
- data/spec/motion-support/core_ext/module/attr_internal_spec.rb +104 -0
- data/spec/motion-support/core_ext/module/attribute_accessor_spec.rb +86 -0
- data/spec/motion-support/core_ext/module/delegation_spec.rb +136 -0
- data/spec/motion-support/core_ext/module/introspection_spec.rb +70 -0
- data/spec/motion-support/core_ext/module/reachable_spec.rb +61 -0
- data/spec/motion-support/core_ext/module/remove_method_spec.rb +25 -0
- data/spec/motion-support/core_ext/numeric/bytes_spec.rb +43 -0
- data/spec/motion-support/core_ext/object/acts_like_spec.rb +21 -0
- data/spec/motion-support/core_ext/object/blank_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/deep_dup_spec.rb +54 -0
- data/spec/motion-support/core_ext/object/duplicable_spec.rb +31 -0
- data/spec/motion-support/core_ext/object/instance_variable_spec.rb +19 -0
- data/spec/motion-support/core_ext/object/to_param_spec.rb +75 -0
- data/spec/motion-support/core_ext/object/to_query_spec.rb +37 -0
- data/spec/motion-support/core_ext/object/try_spec.rb +92 -0
- data/spec/motion-support/core_ext/range/include_range_spec.rb +31 -0
- data/spec/motion-support/core_ext/range/overlap_spec.rb +43 -0
- data/spec/motion-support/core_ext/regexp_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/access_spec.rb +53 -0
- data/spec/motion-support/core_ext/string/behavior_spec.rb +7 -0
- data/spec/motion-support/core_ext/string/exclude_spec.rb +8 -0
- data/spec/motion-support/core_ext/string/filter_spec.rb +48 -0
- data/spec/motion-support/core_ext/string/indent_spec.rb +56 -0
- data/spec/motion-support/core_ext/string/inflection_spec.rb +142 -0
- data/spec/motion-support/core_ext/string/starts_end_with_spec.rb +14 -0
- data/spec/motion-support/core_ext/string/strip_spec.rb +34 -0
- data/spec/motion-support/core_ext/string_spec.rb +88 -0
- data/spec/motion-support/core_ext/time/acts_like_spec.rb +11 -0
- data/spec/motion-support/core_ext/time/calculation_spec.rb +201 -0
- data/spec/motion-support/core_ext/time/conversion_spec.rb +54 -0
- data/spec/motion-support/duration_spec.rb +107 -0
- data/spec/motion-support/hash_with_indifferent_access_spec.rb +605 -0
- data/spec/motion-support/inflector_spec.rb +474 -35
- data/spec/motion-support/ns_dictionary_spec.rb +29 -0
- metadata +212 -35
- data/lib/motion-support/cattr_accessor.rb +0 -19
- data/lib/motion-support/class_inheritable_accessor.rb +0 -23
- data/lib/motion-support/class_inheritable_array.rb +0 -29
- data/lib/motion-support/hash.rb +0 -31
- data/lib/motion-support/nilclass.rb +0 -5
- data/lib/motion-support/object.rb +0 -17
- data/lib/motion-support/string.rb +0 -71
- data/spec/motion-support/cattr_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_accessor_spec.rb +0 -49
- data/spec/motion-support/class_inheritable_array_spec.rb +0 -61
- data/spec/motion-support/hash_spec.rb +0 -31
- data/spec/motion-support/nilclass_spec.rb +0 -5
- data/spec/motion-support/object_spec.rb +0 -43
- data/spec/motion-support/string_spec.rb +0 -145
@@ -0,0 +1,21 @@
|
|
1
|
+
class DuckFoo
|
2
|
+
def acts_like_foo?
|
3
|
+
true
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "Object" do
|
8
|
+
describe "acts_like" do
|
9
|
+
it "should not act like anything" do
|
10
|
+
object = Object.new
|
11
|
+
object.should.not.acts_like(:time)
|
12
|
+
object.should.not.acts_like(:date)
|
13
|
+
object.should.not.acts_like(:foo)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should allow subclasses to act like something" do
|
17
|
+
object = DuckFoo.new
|
18
|
+
object.should.acts_like(:foo)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class EmptyTrue
|
2
|
+
def empty?() true; end
|
3
|
+
end
|
4
|
+
|
5
|
+
class EmptyFalse
|
6
|
+
def empty?() false; end
|
7
|
+
end
|
8
|
+
|
9
|
+
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ]
|
10
|
+
NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
|
11
|
+
|
12
|
+
describe "blank" do
|
13
|
+
describe "blank?" do
|
14
|
+
BLANK.each do |v|
|
15
|
+
it "should treat #{v.inspect} as blank" do
|
16
|
+
v.should.be.blank
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
NOT.each do |v|
|
21
|
+
it "should treat #{v.inspect} as NOT blank" do
|
22
|
+
v.should.not.be.blank
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "present?" do
|
28
|
+
BLANK.each do |v|
|
29
|
+
it "should treat #{v.inspect} as NOT present" do
|
30
|
+
v.should.not.be.present
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
NOT.each do |v|
|
35
|
+
it "should treat #{v.inspect} as present" do
|
36
|
+
v.should.be.present
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "presence" do
|
42
|
+
BLANK.each do |v|
|
43
|
+
it "should return nil for #{v.inspect}.presence" do
|
44
|
+
v.presence.should.be.nil
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
NOT.each do |v|
|
49
|
+
it "should return self for #{v.inspect}.presence" do
|
50
|
+
v.presence.should == v
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
describe "deep_dup" do
|
2
|
+
describe "array" do
|
3
|
+
it "should deep_dup nested array" do
|
4
|
+
array = [1, [2, 3]]
|
5
|
+
dup = array.deep_dup
|
6
|
+
dup[1][2] = 4
|
7
|
+
array[1][2].should.be.nil
|
8
|
+
dup[1][2].should == 4
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should deep_dup array with hash inside" do
|
12
|
+
array = [1, { :a => 2, :b => 3 } ]
|
13
|
+
dup = array.deep_dup
|
14
|
+
dup[1][:c] = 4
|
15
|
+
array[1][:c].should.be.nil
|
16
|
+
dup[1][:c].should == 4
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "hash" do
|
21
|
+
it "should deep_dup nested hash" do
|
22
|
+
hash = { :a => { :b => 'b' } }
|
23
|
+
dup = hash.deep_dup
|
24
|
+
dup[:a][:c] = 'c'
|
25
|
+
hash[:a][:c].should.be.nil
|
26
|
+
dup[:a][:c].should == 'c'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should deep_dup hash with array inside" do
|
30
|
+
hash = { :a => [1, 2] }
|
31
|
+
dup = hash.deep_dup
|
32
|
+
dup[:a][2] = 'c'
|
33
|
+
hash[:a][2].should.be.nil
|
34
|
+
dup[:a][2].should == 'c'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should deep_dup hash with init value" do
|
38
|
+
zero_hash = Hash.new 0
|
39
|
+
hash = { :a => zero_hash }
|
40
|
+
dup = hash.deep_dup
|
41
|
+
dup[:a][44].should == 0
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "Object" do
|
46
|
+
it "should deep_dup object" do
|
47
|
+
object = Object.new
|
48
|
+
dup = object.deep_dup
|
49
|
+
dup.instance_variable_set(:@a, 1)
|
50
|
+
object.should.not.be.instance_variable_defined(:@a)
|
51
|
+
dup.should.be.instance_variable_defined(:@a)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
describe "duplicable?" do
|
2
|
+
before do
|
3
|
+
@raise_dup = [nil, false, true, :symbol, 1, 2.3, 5.seconds]
|
4
|
+
@yes = ['1', Object.new, /foo/, [], {}, Time.now, Class.new, Module.new, BigDecimal.new('4.56')]
|
5
|
+
@no = []
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return false for non-duplicable objects" do
|
9
|
+
(@raise_dup + @no).each do |v|
|
10
|
+
v.should.not.be.duplicable
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should return true for duplicable objects" do
|
15
|
+
@yes.each do |v|
|
16
|
+
v.should.be.duplicable
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should not raise when dupping duplicable objects" do
|
21
|
+
(@yes + @no).each do |v|
|
22
|
+
lambda { v.dup }.should.not.raise
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should raise when dupping non-duplicable objects" do
|
27
|
+
@raise_dup.each do |v|
|
28
|
+
lambda { v.dup }.should.raise TypeError
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
describe "Object" do
|
2
|
+
before do
|
3
|
+
@source = Object.new
|
4
|
+
@source.instance_variable_set(:@bar, 'bar')
|
5
|
+
@source.instance_variable_set(:@baz, 'baz')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "instance_variable_names" do
|
9
|
+
it "should return all instance variable names" do
|
10
|
+
@source.instance_variable_names.sort.should == %w(@bar @baz)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "instance_values" do
|
15
|
+
it "should return the values of all instance variables as a hash" do
|
16
|
+
@source.instance_values.should == {'bar' => 'bar', 'baz' => 'baz'}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
class ArrayToParam < String
|
2
|
+
def to_param
|
3
|
+
"#{self}1"
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class HashToParam < String
|
8
|
+
def to_param
|
9
|
+
"#{self}-1"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "to_param" do
|
14
|
+
describe "object" do
|
15
|
+
it "should delegate to to_s" do
|
16
|
+
foo = Object.new
|
17
|
+
def foo.to_s; 'foo' end
|
18
|
+
foo.to_param.should == 'foo'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "nil" do
|
23
|
+
it "should return self" do
|
24
|
+
nil.to_param.should == nil
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "true, false" do
|
29
|
+
it "should return self" do
|
30
|
+
true.to_param.should == true
|
31
|
+
false.to_param.should == false
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "array" do
|
36
|
+
it "should convert string array" do
|
37
|
+
%w().to_param.should == ''
|
38
|
+
%w(hello world).to_param.should == 'hello/world'
|
39
|
+
%w(hello 10).to_param.should == 'hello/10'
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should convert number array" do
|
43
|
+
[10, 20].to_param.should == '10/20'
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should convert custom object array" do
|
47
|
+
[ArrayToParam.new('custom'), ArrayToParam.new('param')].to_param.should == 'custom1/param1'
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe "hash" do
|
52
|
+
it "should convert a string hash" do
|
53
|
+
{}.to_param.should == ''
|
54
|
+
{ :hello => "world" }.to_param.should == 'hello=world'
|
55
|
+
{ "hello" => 10 }.to_param.should == 'hello=10'
|
56
|
+
{:hello => "world", "say_bye" => true}.to_param.should == 'hello=world&say_bye=true'
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should convert a number hash" do
|
60
|
+
{10 => 20, 30 => 40, 50 => 60}.to_param.should == '10=20&30=40&50=60'
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should convert an object hash" do
|
64
|
+
{HashToParam.new('custom') => HashToParam.new('param'), HashToParam.new('custom2') => HashToParam.new('param2')}.to_param.should == 'custom-1=param-1&custom2-1=param2-1'
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should escape keys and values" do
|
68
|
+
{ 'param 1' => 'A string with / characters & that should be ? escaped' }.to_param.should == 'param+1=A+string+with+%2F+characters+%26+that+should+be+%3F+escaped'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should order keys in ascending order" do
|
72
|
+
Hash[*%w(b 1 c 0 a 2)].to_param.should == 'a=2&b=1&c=0'
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Should
|
2
|
+
def equal_query(query)
|
3
|
+
@object.to_query.split('&').should == query.split('&')
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
describe "to_query" do
|
8
|
+
it "should do a simple converion" do
|
9
|
+
{ :a => 10 }.should.equal_query 'a=10'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should escape for CGI" do
|
13
|
+
{ 'a:b' => 'c d' }.should.equal_query 'a%3Ab=c+d'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should work with nil parameter value" do
|
17
|
+
empty = Object.new
|
18
|
+
def empty.to_param; nil end
|
19
|
+
{ 'a' => empty }.should.equal_query 'a='
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should do a nested conversion" do
|
23
|
+
{ :person => Hash[:login, 'seckar', :name, 'Nicholas'] }.should.equal_query 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should do a multiply nested query" do
|
27
|
+
Hash[:account, {:person => {:id => 20}}, :person, {:id => 10}].should.equal_query 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10'
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should work with array values" do
|
31
|
+
{ :person => {:id => [10, 20]} }.should.equal_query 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not sort array values" do
|
35
|
+
{ :person => {:id => [20, 10]} }.should.equal_query 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10'
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
describe "Object" do
|
2
|
+
before do
|
3
|
+
@string = "Hello"
|
4
|
+
end
|
5
|
+
|
6
|
+
describe "try" do
|
7
|
+
it "should return nil for nonexisting method" do
|
8
|
+
method = :undefined_method
|
9
|
+
@string.should.not.respond_to method
|
10
|
+
@string.try(method).should.be.nil
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return nil for nonexisting method with arguments" do
|
14
|
+
method = :undefined_method
|
15
|
+
@string.should.not.respond_to method
|
16
|
+
@string.try(method, 'llo', 'y').should.be.nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should delegate to existing method" do
|
20
|
+
@string.try(:size).should == 5
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should forward arguments to existing method" do
|
24
|
+
@string.try(:sub, 'llo', 'y').should == 'Hey'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should forward block to existing method" do
|
28
|
+
@string.try(:sub, 'llo') { |match| 'y' }.should == 'Hey'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return nil when called on nil even if method is defined on nil" do
|
32
|
+
nil.try(:to_s).should.be.nil
|
33
|
+
nil.try(:to_i).should.be.nil
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should work as expected on false" do
|
37
|
+
false.try(:to_s).should == 'false'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should pass existing receiver to block" do
|
41
|
+
@string.try { |s| s.reverse }.should == @string.reverse
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should not pass nil to block" do
|
45
|
+
ran = false
|
46
|
+
nil.try { ran = true }
|
47
|
+
ran.should == false
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should not call private method" do
|
51
|
+
klass = Class.new do
|
52
|
+
private
|
53
|
+
|
54
|
+
def private_method
|
55
|
+
'private method'
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
klass.new.try(:private_method).should == nil
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "try!" do
|
64
|
+
it "should raise error for nonexisting method" do
|
65
|
+
method = :undefined_method
|
66
|
+
@string.should.not.respond_to method
|
67
|
+
lambda { @string.try!(method) }.should.raise NoMethodError
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should raise error for nonexisting method with arguments" do
|
71
|
+
method = :undefined_method
|
72
|
+
@string.should.not.respond_to method
|
73
|
+
lambda { @string.try!(method, 'llo', 'y') }.should.raise NoMethodError
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should pass existing receiver to block" do
|
77
|
+
@string.try! { |s| s.reverse }.should == @string.reverse
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should raise error when calling private method" do
|
81
|
+
klass = Class.new do
|
82
|
+
private
|
83
|
+
|
84
|
+
def private_method
|
85
|
+
'private method'
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
lambda { klass.new.try!(:private_method) }.should.raise NoMethodError
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
describe "Range" do
|
2
|
+
describe "include?" do
|
3
|
+
it "should should include identical inclusive" do
|
4
|
+
(1..10).should.include(1..10)
|
5
|
+
end
|
6
|
+
|
7
|
+
it "should should include identical exclusive" do
|
8
|
+
(1...10).should.include(1...10)
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should should include other with exlusive end" do
|
12
|
+
(1..10).should.include(1...10)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should exclusive end should not include identical with inclusive end" do
|
16
|
+
(1...10).should.not.include(1..10)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should should not include overlapping first" do
|
20
|
+
(2..8).should.not.include(1..3)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should should not include overlapping last" do
|
24
|
+
(2..8).should.not.include(5..9)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should should include identical exclusive with floats" do
|
28
|
+
(1.0...10.0).should.include(1.0...10.0)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
describe "Range" do
|
2
|
+
describe "overlaps?" do
|
3
|
+
it "should overlaps last inclusive" do
|
4
|
+
(1..5).overlaps?(5..10).should.be.true
|
5
|
+
end
|
6
|
+
|
7
|
+
it "should overlaps last exclusive" do
|
8
|
+
(1...5).overlaps?(5..10).should.be.false
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should overlaps first inclusive" do
|
12
|
+
(5..10).overlaps?(1..5).should.be.true
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should overlaps first exclusive" do
|
16
|
+
(5..10).overlaps?(1...5).should.be.false
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should should compare identical inclusive" do
|
20
|
+
((1..10) === (1..10)).should.be.true
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should should compare identical exclusive" do
|
24
|
+
((1...10) === (1...10)).should.be.true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should should compare other with exlusive end" do
|
28
|
+
((1..10) === (1...10)).should.be.true
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should overlaps on time" do
|
32
|
+
time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
|
33
|
+
time_range_2 = Time.utc(2005, 12, 10, 17, 00)..Time.utc(2005, 12, 10, 18, 00)
|
34
|
+
time_range_1.overlaps?(time_range_2).should.be.true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should no overlaps on time" do
|
38
|
+
time_range_1 = Time.utc(2005, 12, 10, 15, 30)..Time.utc(2005, 12, 10, 17, 30)
|
39
|
+
time_range_2 = Time.utc(2005, 12, 10, 17, 31)..Time.utc(2005, 12, 10, 18, 00)
|
40
|
+
time_range_1.overlaps?(time_range_2).should.be.false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
describe "String" do
|
2
|
+
describe "access" do
|
3
|
+
it "should access" do
|
4
|
+
s = "hello"
|
5
|
+
s.at(0).should == "h"
|
6
|
+
|
7
|
+
s.from(2).should == "llo"
|
8
|
+
s.to(2).should == "hel"
|
9
|
+
|
10
|
+
s.first.should == "h"
|
11
|
+
s.first(2).should == "he"
|
12
|
+
s.first(0).should == ""
|
13
|
+
|
14
|
+
s.last.should == "o"
|
15
|
+
s.last(3).should == "llo"
|
16
|
+
s.last(10).should == "hello"
|
17
|
+
s.last(0).should == ""
|
18
|
+
|
19
|
+
'x'.first.should == 'x'
|
20
|
+
'x'.first(4).should == 'x'
|
21
|
+
|
22
|
+
'x'.last.should == 'x'
|
23
|
+
'x'.last(4).should == 'x'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should access returns a real string" do
|
27
|
+
hash = {}
|
28
|
+
hash["h"] = true
|
29
|
+
hash["hello123".at(0)] = true
|
30
|
+
hash.keys.should == %w(h)
|
31
|
+
|
32
|
+
hash = {}
|
33
|
+
hash["llo"] = true
|
34
|
+
hash["hello".from(2)] = true
|
35
|
+
hash.keys.should == %w(llo)
|
36
|
+
|
37
|
+
hash = {}
|
38
|
+
hash["hel"] = true
|
39
|
+
hash["hello".to(2)] = true
|
40
|
+
hash.keys.should == %w(hel)
|
41
|
+
|
42
|
+
hash = {}
|
43
|
+
hash["hello"] = true
|
44
|
+
hash["123hello".last(5)] = true
|
45
|
+
hash.keys.should == %w(hello)
|
46
|
+
|
47
|
+
hash = {}
|
48
|
+
hash["hello"] = true
|
49
|
+
hash["hello123".first(5)] = true
|
50
|
+
hash.keys.should == %w(hello)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
describe "String" do
|
2
|
+
describe "filters" do
|
3
|
+
describe "squish" do
|
4
|
+
before do
|
5
|
+
@original = %{\u180E\u180E A string surrounded by unicode mongolian vowel separators,
|
6
|
+
with tabs(\t\t), newlines(\n\n), unicode nextlines(\u0085\u0085) and many spaces( ). \u180E\u180E}
|
7
|
+
|
8
|
+
@expected = "A string surrounded by unicode mongolian vowel separators, " +
|
9
|
+
"with tabs( ), newlines( ), unicode nextlines( ) and many spaces( )."
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should squish string" do
|
13
|
+
@original.squish.should == @expected
|
14
|
+
@original.should.not == @expected
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should squish! string" do
|
18
|
+
@original.squish!.should == @expected
|
19
|
+
@original.should == @expected
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "truncate" do
|
24
|
+
it "should truncate string" do
|
25
|
+
"Hello World!".truncate(12).should == "Hello World!"
|
26
|
+
"Hello World!!".truncate(12).should == "Hello Wor..."
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should truncate with omission and seperator" do
|
30
|
+
"Hello World!".truncate(10, :omission => "[...]").should == "Hello[...]"
|
31
|
+
"Hello Big World!".truncate(13, :omission => "[...]", :separator => ' ').should == "Hello[...]"
|
32
|
+
"Hello Big World!".truncate(14, :omission => "[...]", :separator => ' ').should == "Hello Big[...]"
|
33
|
+
"Hello Big World!".truncate(15, :omission => "[...]", :separator => ' ').should == "Hello Big[...]"
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should truncate with omission and regexp seperator" do
|
37
|
+
"Hello Big World!".truncate(13, :omission => "[...]", :separator => /\s/).should == "Hello[...]"
|
38
|
+
"Hello Big World!".truncate(14, :omission => "[...]", :separator => /\s/).should == "Hello Big[...]"
|
39
|
+
"Hello Big World!".truncate(15, :omission => "[...]", :separator => /\s/).should == "Hello Big[...]"
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should truncate multibyte" do
|
43
|
+
"\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 \354\225\204\353\235\274\353\246\254\354\230\244".force_encoding(Encoding::UTF_8).truncate(10).should ==
|
44
|
+
"\354\225\204\353\246\254\353\236\221 \354\225\204\353\246\254 ...".force_encoding(Encoding::UTF_8)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
describe "String" do
|
2
|
+
describe "indent" do
|
3
|
+
it "should not indent strings that only contain newlines (edge cases)" do
|
4
|
+
['', "\n", "\n" * 7].each do |str|
|
5
|
+
str.indent!(8).should.be.nil
|
6
|
+
str.indent(8).should == str
|
7
|
+
str.indent(1, "\t").should == str
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should indent by default with spaces if the existing indentation uses them" do
|
12
|
+
"foo\n bar".indent(4).should == " foo\n bar"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should indent by default with tabs if the existing indentation uses them" do
|
16
|
+
"foo\n\t\bar".indent(1).should == "\tfoo\n\t\t\bar"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should indent by default with spaces as a fallback if there is no indentation" do
|
20
|
+
"foo\nbar\nbaz".indent(3).should == " foo\n bar\n baz"
|
21
|
+
end
|
22
|
+
|
23
|
+
# Nothing is said about existing indentation that mixes spaces and tabs, so
|
24
|
+
# there is nothing to test.
|
25
|
+
|
26
|
+
it "should use the indent char if passed" do
|
27
|
+
<<ACTUAL.indent(4, '.').should == <<EXPECTED
|
28
|
+
def some_method(x, y)
|
29
|
+
some_code
|
30
|
+
end
|
31
|
+
ACTUAL
|
32
|
+
.... def some_method(x, y)
|
33
|
+
.... some_code
|
34
|
+
.... end
|
35
|
+
EXPECTED
|
36
|
+
|
37
|
+
<<ACTUAL.indent(2, ' ').should == <<EXPECTED
|
38
|
+
def some_method(x, y)
|
39
|
+
some_code
|
40
|
+
end
|
41
|
+
ACTUAL
|
42
|
+
def some_method(x, y)
|
43
|
+
some_code
|
44
|
+
end
|
45
|
+
EXPECTED
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should not indent blank lines by default" do
|
49
|
+
"foo\n\nbar".indent(1).should == " foo\n\n bar"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should indent blank lines if told so" do
|
53
|
+
"foo\n\nbar".indent(1, nil, true).should == " foo\n \n bar"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|