friendly_extensions 0.0.61 → 0.0.62

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/test/array_test.rb CHANGED
@@ -180,11 +180,33 @@ class ArrayTest < Test::Unit::TestCase
180
180
  assert_no_match Regexp.new("'"), a.to_sql
181
181
  assert_equal a.to_sql, "(1,2,4.5)"
182
182
 
183
- # All others should be unquoted
183
+ # All others should be quoted
184
184
  a = ["a", :b]
185
185
  assert_match Regexp.new("'"), a.to_sql
186
186
  assert_equal a.to_sql, "('a','b')"
187
- end
187
+ end
188
+
189
+ #== Test interpolation
190
+ def test_array_should_interpolate
191
+
192
+ # First should not fail
193
+ assert_equal [nil,1,2].interpolate(nil), [0,1,2]
194
+ # These fail:
195
+ # assert_equal [nil, nil,1,2].interpolate(nil), [-1, 0,1,2]
196
+
197
+
198
+ # Last should not fail
199
+ assert_equal [1,2,3], [1,2,nil].interpolate(nil).map {|x| x.to_i }
200
+ # These fail:
201
+ # assert_equal [1,2,3,4], [1,2,nil,nil].interpolate(nil).map {|x| x.to_i }
202
+
203
+ # In The middle
204
+ assert_equal [1,2,3,4,5], [1,2,3,nil,5].interpolate(nil).map {|x| x.to_i }
205
+ assert_equal [1,2,3,4,5], [1,2,nil,nil,5].interpolate(nil).map {|x| x.to_i }
206
+ assert_equal [5,10,15,20,25], [5,nil,15,nil,25].interpolate(nil).map {|x| x.to_i }
207
+ assert_equal [5,10,15,20,25,30], [5,nil,15,nil,nil,30].interpolate(nil).map {|x| x.to_i }
208
+ end
209
+
188
210
 
189
211
 
190
212
 
@@ -0,0 +1,221 @@
1
+ require "test/unit"
2
+ require "array"
3
+ require "dummy_class"
4
+
5
+ class ArrayTest < Test::Unit::TestCase
6
+
7
+ #== Tests for .sum
8
+ def test_array_should_sum
9
+ assert_equal [1,2].sum, 3
10
+ end
11
+
12
+ #== Tests for .avg
13
+ def test_array_avg_with_numbers
14
+ test_array = [4,5]
15
+
16
+ assert_equal test_array.avg, 4.5
17
+ end
18
+
19
+ def test_array_avg_with_attributes
20
+ test_array = []
21
+ test_array << DummyClass.new(:number => 4)
22
+ test_array << DummyClass.new(:number => 5)
23
+
24
+ assert_equal test_array.avg(:number), 4.5
25
+ end
26
+
27
+ def test_array_avg_should_no_break_with_nil_and_emtpy
28
+ assert_equal [3,nil].avg, 3
29
+ assert_equal [].avg, 0
30
+ end
31
+
32
+
33
+ #== Tests for .array_to_hash
34
+ def test_array_should_make_to_hash
35
+ assert_equal [1,2].array_to_hash(""), {1 => "", 2 => ""}
36
+ assert_equal ["A","B"].array_to_hash(1), {"A" => 1, "B" => 1}
37
+ assert_equal ["A",nil].array_to_hash(1), {"A" => 1}
38
+ end
39
+
40
+ #== Tests for .to_structured_hash
41
+ def test_array_should_get_structured_with_symbol_and_nested_string
42
+
43
+ # Create Test Datas
44
+ test_data_row = []
45
+ test_data_row << DummyClass.new(:sec_id => 1, :name => "bla-1")
46
+ test_data_row << DummyClass.new(:sec_id => 1, :name => "bla-2")
47
+ test_data_row << DummyClass.new(:sec_id => 1, :name => "bla-3")
48
+
49
+ test_data_row << DummyClass.new(:sec_id => 2, :name => "blub-1")
50
+ test_data_row << DummyClass.new(:sec_id => 2, :name => "blub-2")
51
+
52
+ test_data_hash_sym = test_data_row.to_structured_hash(:sec_id)
53
+ test_data_hash_str = test_data_row.to_structured_hash("return_self.sec_id")
54
+
55
+ # Assertions
56
+ assert_equal test_data_hash_sym.keys.size, 2
57
+ assert_equal test_data_hash_sym[1].size, 3
58
+ assert_equal test_data_hash_sym[2].size, 2
59
+
60
+ assert_equal test_data_hash_sym, test_data_hash_str
61
+ end
62
+
63
+ #== Tests for .sum_with_attribute
64
+ def test_array_should_do_sum_with_attributes
65
+
66
+ test_array = []
67
+ test_array << DummyClass.new(:number => 4)
68
+ test_array << DummyClass.new(:number => 5)
69
+
70
+ # Using symbol
71
+ assert_equal test_array.sum_with_attribute(:number), 9
72
+
73
+ # Using nested string
74
+ assert_equal test_array.sum_with_attribute("return_self.number"), 9
75
+
76
+ # Using symbol with options
77
+ assert_equal test_array.sum_with_attribute(:get_number, :double => true), 18
78
+
79
+ # Using nested string with options
80
+ assert_equal test_array.sum_with_attribute("return_self.get_number", :double => true), 18
81
+ end
82
+
83
+ #== Tests for .count_for
84
+ def test_array_should_count_for_items
85
+ a1 = [1,2,3]
86
+
87
+ assert_equal a1.count_for(1), 1
88
+
89
+ a2 = %w(a a b c)
90
+ assert_equal a2.count_for("a"), 2
91
+
92
+ a3 = %w(a b c)
93
+ assert_equal a3.count_for("x"), 0
94
+
95
+ a4 = [1,:a, nil]
96
+ assert_equal a4.count_for(nil), 1
97
+ end
98
+
99
+ #== Tests for .next
100
+ def test_get_next_array_item_without_cycle
101
+ a = [1,2,3]
102
+ assert_equal a.next(2), 3
103
+ assert_equal a.next(3), 3
104
+ assert_equal a.next(4), 4
105
+
106
+
107
+ end
108
+
109
+ def test_get_next_array_item_with_cycle
110
+ a = [1,2,3]
111
+ assert_equal a.next(3, :cycle => true), 1
112
+ assert_equal a.next(1, :cycle => true), 2
113
+ end
114
+
115
+ #== Tests for .isec
116
+ def test_array_must_isec_with_array
117
+ assert_raises ArgumentError do
118
+ [1,2,3].isec(3)
119
+ end
120
+ end
121
+
122
+ def test_array_isec
123
+ a1 = %w(a b c d)
124
+ a2 = %w(c d e f)
125
+ a3 = %w(a f x y)
126
+ a4 = %w(1 2 3 4)
127
+
128
+ assert_equal a1.isec(a2), %w(c d)
129
+ assert_equal (a1+a2).isec(a3), %w(a f)
130
+ assert_equal a3.isec(a4), []
131
+ end
132
+
133
+ #== Tests for .except
134
+ def test_array_except
135
+ a = [1,2,3, "a", "b"]
136
+ assert_equal a.except(1), [2,3, "a", "b"]
137
+ assert_equal a.except(1,2), [3, "a", "b"]
138
+ assert_equal a.except(1,"a"), [2,3, "b"]
139
+ end
140
+
141
+ #== Tests for .seperate
142
+ def test_array_seperation
143
+ {4 => (1..10).to_a, 25 => (1..100).to_a}.each do |n, array|
144
+
145
+ sep_array = array.seperate(n)
146
+
147
+ assert_equal sep_array.size, n
148
+ assert_equal sep_array.flatten, array
149
+ end
150
+ end
151
+
152
+ def test_array_should_seperate__arrays_correctly
153
+ # Fix for:
154
+ # [[1, "a"], [2, "b"], [3, "c"]].seperate(2).last
155
+ # => [[2, "b"], [[3, "c"]]] - Last Array is in an other array
156
+
157
+ assert_equal [[1, "a"], [2, "b"], [3, "c"]].seperate(2).last.last, [3,"c"]
158
+ assert_equal [[1, "a"], [2, "b"], [3, "c"], [4, "d"]].seperate(2).last.last, [4,"d"]
159
+
160
+ end
161
+
162
+ #== Tests for .stack
163
+ def test_array_stack
164
+ {4 => (1..10).to_a, 25 => (1..100).to_a}.each do |n, array|
165
+
166
+ sep_array = array.stack(n)
167
+
168
+ assert_equal sep_array.first.size, n
169
+ assert_equal sep_array.flatten, array
170
+ end
171
+ end
172
+
173
+ #== Tests for .to_sql
174
+ def test_array_to_sql
175
+ # Empty array is NULL
176
+ assert_equal [].to_sql, "(NULL)"
177
+
178
+ # Numbers should be unquoted
179
+ a = [1,2,4.5]
180
+ assert_no_match Regexp.new("'"), a.to_sql
181
+ assert_equal a.to_sql, "(1,2,4.5)"
182
+
183
+ <<<<<<< HEAD
184
+ # All others should be quoted
185
+ a = ["a", :b]
186
+ assert_match Regexp.new("'"), a.to_sql
187
+ assert_equal a.to_sql, "('a','b')"
188
+ end
189
+
190
+ #== Test interpolation
191
+ def test_array_should_interpolate
192
+
193
+ # First should not fail
194
+ assert_equal [nil,1,2].interpolate(nil), [0,1,2]
195
+ # These fail:
196
+ # assert_equal [nil, nil,1,2].interpolate(nil), [-1, 0,1,2]
197
+
198
+
199
+ # Last should not fail
200
+ assert_equal [1,2,3], [1,2,nil].interpolate(nil).map {|x| x.to_i }
201
+ # These fail:
202
+ # assert_equal [1,2,3,4], [1,2,nil,nil].interpolate(nil).map {|x| x.to_i }
203
+
204
+ # In The middle
205
+ assert_equal [1,2,3,4,5], [1,2,3,nil,5].interpolate(nil).map {|x| x.to_i }
206
+ assert_equal [1,2,3,4,5], [1,2,nil,nil,5].interpolate(nil).map {|x| x.to_i }
207
+ assert_equal [5,10,15,20,25], [5,nil,15,nil,25].interpolate(nil).map {|x| x.to_i }
208
+ assert_equal [5,10,15,20,25,30], [5,nil,15,nil,nil,30].interpolate(nil).map {|x| x.to_i }
209
+ end
210
+
211
+ =======
212
+ # All others should be unquoted
213
+ a = ["a", :b]
214
+ assert_match Regexp.new("'"), a.to_sql
215
+ assert_equal a.to_sql, "('a','b')"
216
+ end
217
+ >>>>>>> ad58e345c5de2638e34e44d69bb19b0ebebcd41e
218
+
219
+
220
+
221
+ end
data/test/numeric_test.rb CHANGED
@@ -75,6 +75,16 @@ class NumericTest < Test::Unit::TestCase
75
75
  assert_equal 73.to_time(:discard_hour => true), "01:13"
76
76
 
77
77
  assert_equal 3672.to_time, "01:01:12"
78
- end
78
+ end
79
+
80
+ def test_prime_factors
81
+
82
+ [16,234,4568,34950,183649].each do |nr|
83
+ primes = nr.prime_factors
84
+ puts "#{nr} => #{primes.inspect}"
85
+ assert_equal nr, eval(primes.join("*"))
86
+ end
87
+
88
+ end
79
89
 
80
90
  end
@@ -0,0 +1,94 @@
1
+ require "test/unit"
2
+ require "numbers"
3
+
4
+
5
+ class NumericTest < Test::Unit::TestCase
6
+
7
+
8
+ def test_number_to_euro
9
+ #assert_equal 0.29.to_euro, "0,29"
10
+ end
11
+
12
+ # Test Math Functions
13
+ def test_math_infla_defla
14
+
15
+ [2, 3.3].each do |f|
16
+ [40,250].each do |n|
17
+ i = 500
18
+ r_inf = r_def = i
19
+
20
+ n.times do |i|
21
+ r_inf = r_inf*(1+f/100.0)
22
+ r_def = r_def*(1-f/100.0)
23
+ end
24
+
25
+ assert_equal i.infla(n,f).round(5), r_inf.round(5)
26
+ assert_equal i.defla(n,f).round(5), r_def.round(5)
27
+ end
28
+ end
29
+
30
+ end
31
+
32
+ def test_number_should_get_closest
33
+ # Test for wrong limit mode:
34
+ assert_raise ArgumentError do
35
+ 1.get_closest([3,4], :zonk)
36
+ end
37
+
38
+ # Check to find closest
39
+ assert_equal 2.get_closest([0,3,4], :ceil), 3
40
+ assert_equal 2.get_closest([0,3,4], :floor), 3
41
+ assert_equal -2.88.get_closest([0,3,4], :floor), 0
42
+
43
+ # Check to find closest with :floor
44
+ assert_equal 1.get_closest([0,2], :floor), 0
45
+ assert_equal 5.5.get_closest([4.5,6.5], :floor), 4.5
46
+
47
+ # Check to find closest with :ceil
48
+ assert_equal 1.get_closest([0,2], :ceil), 2
49
+ assert_equal 5.5.get_closest([4.5,6.5], :ceil), 6.5
50
+
51
+ end
52
+
53
+
54
+ def test_numbers_min_and_max
55
+ assert_equal 1, 1.min(3)
56
+ assert_equal 3, 1.max(3)
57
+ assert_equal 2, 2.min_max(1,3)
58
+ assert_equal 1, -1.min_max(1,3)
59
+ assert_equal 3, 4.min_max(1,3)
60
+ end
61
+
62
+ def test_float_dividor
63
+ assert !1.fdiv(1).is_a?(Integer)
64
+ end
65
+
66
+ def test_to_q_faktor
67
+ assert_equal 2.to_q, 1.02
68
+ end
69
+
70
+ def test_time_should_be_calc_correct
71
+ assert_equal 23.to_time, "00:00:23"
72
+ assert_equal 64.to_time, "00:01:04"
73
+
74
+ assert_equal 17.to_time(:discard_hour => true), "00:17"
75
+ assert_equal 73.to_time(:discard_hour => true), "01:13"
76
+
77
+ assert_equal 3672.to_time, "01:01:12"
78
+ <<<<<<< HEAD
79
+ end
80
+
81
+ def test_prime_factors
82
+
83
+ [16,234,4568,34950,183649].each do |nr|
84
+ primes = nr.prime_factors
85
+ puts "#{nr} => #{primes.inspect}"
86
+ assert_equal nr, eval(primes.join("*"))
87
+ end
88
+
89
+ end
90
+ =======
91
+ end
92
+ >>>>>>> ad58e345c5de2638e34e44d69bb19b0ebebcd41e
93
+
94
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: friendly_extensions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.61
4
+ version: 0.0.62
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Eck
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-14 00:00:00.000000000 Z
11
+ date: 2014-02-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 3.2.9
19
+ version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 3.2.9
26
+ version: '0'
27
27
  description: Adds serveral cool features to your Ruby classes. Includes new features
28
28
  for Array, String, Hash, Numeric, Date/Datetime and some more.
29
29
  email: it-support@friends-systems.de
@@ -32,7 +32,9 @@ extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
34
  - lib/alphanumeric.rb
35
+ - lib/alphanumeric.rb.orig
35
36
  - lib/array.rb
37
+ - lib/array.rb.orig
36
38
  - lib/boolean.rb
37
39
  - lib/chars_2_remove.rb
38
40
  - lib/date_n_time.rb
@@ -40,12 +42,17 @@ files:
40
42
  - lib/hash.rb
41
43
  - lib/nil_class.rb
42
44
  - lib/numbers.rb
45
+ - lib/numbers.rb.orig
43
46
  - lib/smart_currency.rb
47
+ - lib/smart_currency.rb.orig
44
48
  - lib/string_and_more.rb
49
+ - lib/string_and_more.rb.orig
45
50
  - test/array_test.rb
51
+ - test/array_test.rb.orig
46
52
  - test/dummy_class.rb
47
53
  - test/hash_test.rb
48
54
  - test/numeric_test.rb
55
+ - test/numeric_test.rb.orig
49
56
  - test/string_test.rb
50
57
  homepage: https://github.com/florianeck/rails_friendly_extensions
51
58
  licenses: []
@@ -72,7 +79,9 @@ specification_version: 4
72
79
  summary: Collection of useful features for Ruby/Rails Classes
73
80
  test_files:
74
81
  - test/array_test.rb
82
+ - test/array_test.rb.orig
75
83
  - test/dummy_class.rb
76
84
  - test/hash_test.rb
77
85
  - test/numeric_test.rb
86
+ - test/numeric_test.rb.orig
78
87
  - test/string_test.rb