money_parser 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'money_parser'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
9
+
10
+ def random_amount_of_spaces
11
+ ' ' * rand(2)
12
+ end
13
+
14
+ def randomly_replace_zeros_by_o(string)
15
+ indexes = string.
16
+ each_char.
17
+ each_with_index.
18
+ to_a.
19
+ select{|l, idx| l == '0'}.
20
+ map{|_, idx| idx }
21
+
22
+ return nil if indexes.empty?
23
+ idx = indexes[rand(indexes.size)]
24
+ string.clone[idx] = 'O'
25
+ end
26
+
27
+ def generate_tests specs
28
+ code = ''
29
+ specs.inject([]) {|accu, pair|
30
+
31
+ money_string, result = pair
32
+
33
+ accu << [money_string, result]
34
+ accu << [random_amount_of_spaces + "-" + random_amount_of_spaces + money_string, result * -1]
35
+
36
+ tuple_with_o = [randomly_replace_zeros_by_o(money_string), result]
37
+
38
+ accu << tuple_with_o unless tuple_with_o[0].nil?
39
+
40
+ accu
41
+ }.each {|money_string, result|
42
+
43
+ code << %!
44
+
45
+ describe "\"#{money_string}\" should be parsed as #{result}" do
46
+ MoneyParser.parse("\"#{money_string}\").should == #{result}
47
+ end
48
+ !
49
+ }
50
+ code
51
+ end
data/specification.rb ADDED
@@ -0,0 +1,52 @@
1
+ SPECIFICATION = [
2
+ ["2000.01" , 2000.01],
3
+ ["2000,01" , 2000.01],
4
+ ["2000.1" , 2000.1],
5
+ ["2000,1" , 2000.1],
6
+ ["2000" , 2000.0],
7
+ [".01" , 0.01],
8
+ [",01" , 0.01],
9
+ ["0.01" , 0.01],
10
+ ["0,01" , 0.01],
11
+ [".1" , 0.1],
12
+ [",1" , 0.1],
13
+ ["0.1" , 0.1],
14
+ ["0,1" , 0.1],
15
+ ["2,000.01" , 2000.01],
16
+ ["2.000,01" , 2000.01],
17
+ ["2 000.01" , 2000.01],
18
+ ["2 000,01" , 2000.01],
19
+ ["1,222,000.01", 1222000.01],
20
+ ["1.222.000,01", 1222000.01],
21
+ ["1 222 000.01", 1222000.01],
22
+ ["1 222 000,01", 1222000.01],
23
+ ["2,000.1" , 2000.1],
24
+ ["2.000,1" , 2000.1],
25
+ ["2 000.1" , 2000.1],
26
+ ["2 000,1" , 2000.1],
27
+ ["1,222,000.1" , 1222000.1],
28
+ ["1.222.000,1" , 1222000.1],
29
+ ["1 222 000.1" , 1222000.1],
30
+ ["1 222 000,1" , 1222000.1],
31
+ ["2,000.10" , 2000.1],
32
+ ["2.000,10" , 2000.1],
33
+ ["2 000.10" , 2000.1],
34
+ ["2 000,10" , 2000.1],
35
+ ["1,222,000.10", 1222000.1],
36
+ ["1.222.000,10", 1222000.1],
37
+ ["1 222 000.10", 1222000.1],
38
+ ["1 222 000,10", 1222000.1],
39
+ ["1 222 000" , 1222000.0],
40
+ ["1 222 000" , 1222000.0],
41
+ ["1,222,000" , 1222000.0],
42
+ ["1.222.000" , 1222000.0],
43
+ ["1 222 000" , 1222000.0],
44
+ ["1 222 000" , 1222000.0],
45
+ ["2,123" , 2123.0],
46
+ ["2.123" , 2123.0],
47
+ ["2,12" , 2.12],
48
+ ["" , nil],
49
+ ["1" , 1.0],
50
+ [" " , nil],
51
+ ["hello" , nil]
52
+ ]