friendly_extensions 0.0.8
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.
- checksums.yaml +7 -0
- data/app/helpers/friends_forms_helper.rb +27 -0
- data/app/helpers/friends_labeled_form_helper.rb +124 -0
- data/app/models/friends_label.rb +22 -0
- data/config/initializers/values.rb +24 -0
- data/db/migrate/20140326005600_create_labels.rb +13 -0
- data/lib/alphanumeric.rb +76 -0
- data/lib/array.rb +280 -0
- data/lib/boolean.rb +32 -0
- data/lib/chars_2_remove.rb +349 -0
- data/lib/date_n_time.rb +198 -0
- data/lib/friendly_extensions/engine.rb +10 -0
- data/lib/friendly_extensions.rb +17 -0
- data/lib/hash.rb +38 -0
- data/lib/nil_class.rb +12 -0
- data/lib/numbers.rb +180 -0
- data/lib/smart_currency.rb +66 -0
- data/lib/string_and_more.rb +301 -0
- data/test/array_test.rb +213 -0
- data/test/array_test.rb.orig +221 -0
- data/test/dummy_class.rb +36 -0
- data/test/hash_test.rb +46 -0
- data/test/numeric_test.rb +94 -0
- data/test/numeric_test.rb.orig +94 -0
- data/test/string_test.rb +56 -0
- metadata +89 -0
data/test/array_test.rb
ADDED
@@ -0,0 +1,213 @@
|
|
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_not_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
|
+
# All others should be quoted
|
184
|
+
a = ["a", :b]
|
185
|
+
assert_match Regexp.new("'"), a.to_sql
|
186
|
+
assert_equal a.to_sql, "('a','b')"
|
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
|
+
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
end
|
@@ -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/dummy_class.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
class DummyClass
|
2
|
+
|
3
|
+
# This is a dummy Class to store some Data in.
|
4
|
+
# IN real life, this would be ActiveRecord, but because of the are no database actions
|
5
|
+
# required, we could just use this dummy
|
6
|
+
|
7
|
+
# Store some possible data types in the class we can mess around with!
|
8
|
+
attr_accessor :id, :sec_id, :name, :number, :date
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
|
12
|
+
self.id = options[:id]
|
13
|
+
self.sec_id = options[:sec_id]
|
14
|
+
|
15
|
+
self.name = options[:name]
|
16
|
+
self.number = options[:number] || 0
|
17
|
+
self.date = options[:date]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
# Just a dummy method
|
23
|
+
def return_self
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# Get number or number *2
|
28
|
+
def get_number(options = {})
|
29
|
+
if options[:double] == true
|
30
|
+
self.number * 2
|
31
|
+
else
|
32
|
+
return self.number
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/test/hash_test.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "hash"
|
3
|
+
|
4
|
+
class HashTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
#== Test for .ordered
|
7
|
+
def test_hash_should_get_ordered
|
8
|
+
|
9
|
+
test_hash = {
|
10
|
+
1 => "a", 2 => "b", 3 => "c"
|
11
|
+
}
|
12
|
+
|
13
|
+
# Test options with sym or integer
|
14
|
+
assert_equal test_hash.ordered(:key), test_hash.ordered(0)
|
15
|
+
assert_equal test_hash.ordered(:value), test_hash.ordered(1)
|
16
|
+
assert_equal test_hash.ordered(0), test_hash.ordered(1)
|
17
|
+
|
18
|
+
test_hash = {
|
19
|
+
1 => "z", 2 => "y", 3 => "x"
|
20
|
+
}
|
21
|
+
|
22
|
+
assert_equal test_hash.ordered(:key).map {|r| r[0]}, [1,2,3]
|
23
|
+
assert_equal test_hash.ordered(:value).map {|r| r[0]}, [3,2,1]
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
#== Test for .extract_data
|
28
|
+
def test_hash_should_extract_data
|
29
|
+
|
30
|
+
# It should work with symbol and string as keys:
|
31
|
+
test_hash = {
|
32
|
+
"field_1" => 1, "field_2" => 0, :field_3 => 1
|
33
|
+
}
|
34
|
+
|
35
|
+
# default trigger is "1", so nothing should happen now
|
36
|
+
assert_equal test_hash.extract_data, []
|
37
|
+
|
38
|
+
# Using trigger = 1 should work
|
39
|
+
assert_equal test_hash.extract_data(nil, 1), ["field_1", "field_3"]
|
40
|
+
|
41
|
+
# Now using slice and trigger
|
42
|
+
assert_equal test_hash.extract_data("field_", 1), ["1", "3"]
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
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
|
+
def test_number_should_convert_to_array
|
13
|
+
assert_equal 1.to_a, [1]
|
14
|
+
end
|
15
|
+
|
16
|
+
# Test Math Functions
|
17
|
+
def test_math_infla_defla
|
18
|
+
|
19
|
+
[2, 3.3].each do |f|
|
20
|
+
[40,250].each do |n|
|
21
|
+
i = 500
|
22
|
+
r_inf = r_def = i
|
23
|
+
|
24
|
+
n.times do |i|
|
25
|
+
r_inf = r_inf*(1+f/100.0)
|
26
|
+
r_def = r_def*(1-f/100.0)
|
27
|
+
end
|
28
|
+
|
29
|
+
assert_equal i.infla(n,f).round(5), r_inf.round(5)
|
30
|
+
assert_equal i.defla(n,f).round(5), r_def.round(5)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_number_should_get_closest
|
37
|
+
# Test for wrong limit mode:
|
38
|
+
assert_raise ArgumentError do
|
39
|
+
1.get_closest([3,4], :zonk)
|
40
|
+
end
|
41
|
+
|
42
|
+
# Check to find closest
|
43
|
+
assert_equal 2.get_closest([0,3,4], :ceil), 3
|
44
|
+
assert_equal 2.get_closest([0,3,4], :floor), 3
|
45
|
+
assert_equal -2.88.get_closest([0,3,4], :floor), 0
|
46
|
+
|
47
|
+
# Check to find closest with :floor
|
48
|
+
assert_equal 1.get_closest([0,2], :floor), 0
|
49
|
+
assert_equal 5.5.get_closest([4.5,6.5], :floor), 4.5
|
50
|
+
|
51
|
+
# Check to find closest with :ceil
|
52
|
+
assert_equal 1.get_closest([0,2], :ceil), 2
|
53
|
+
assert_equal 5.5.get_closest([4.5,6.5], :ceil), 6.5
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def test_numbers_min_and_max
|
59
|
+
assert_equal 1, 1.min(3)
|
60
|
+
assert_equal 3, 1.max(3)
|
61
|
+
assert_equal 2, 2.min_max(1,3)
|
62
|
+
assert_equal 1, -1.min_max(1,3)
|
63
|
+
assert_equal 3, 4.min_max(1,3)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_float_dividor
|
67
|
+
assert !1.fdiv(1).is_a?(Integer)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_to_q_faktor
|
71
|
+
assert_equal 2.to_q, 1.02
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_time_should_be_calc_correct
|
75
|
+
assert_equal 23.to_time, "00:00:23"
|
76
|
+
assert_equal 64.to_time, "00:01:04"
|
77
|
+
|
78
|
+
assert_equal 17.to_time(:discard_hour => true), "00:17"
|
79
|
+
assert_equal 73.to_time(:discard_hour => true), "01:13"
|
80
|
+
|
81
|
+
assert_equal 3672.to_time, "01:01:12"
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_prime_factors
|
85
|
+
|
86
|
+
[16,234,4568,34950,183649].each do |nr|
|
87
|
+
primes = nr.prime_factors
|
88
|
+
puts "#{nr} => #{primes.inspect}"
|
89
|
+
assert_equal nr, eval(primes.join("*"))
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
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
|