|
2 | 2 |
|
3 | 3 | RSpec.describe RuboCop::Cop::Performance::BigDecimalWithNumericArgument, :config do
|
4 | 4 | context 'when Ruby >= 3.1', :ruby31 do
|
5 |
| - it 'registers an offense and corrects when using `BigDecimal` with string' do |
6 |
| - expect_offense(<<~RUBY) |
7 |
| - BigDecimal('1') |
8 |
| - ^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 5 | + it 'does not register an offense and corrects when using `BigDecimal` with integer' do |
| 6 | + expect_no_offenses(<<~RUBY) |
| 7 | + BigDecimal(1) |
9 | 8 | RUBY
|
| 9 | + end |
10 | 10 |
|
11 |
| - expect_correction(<<~RUBY) |
12 |
| - BigDecimal(1) |
| 11 | + it 'does not register an offense and corrects when using `Integer#to_d` for integer' do |
| 12 | + expect_no_offenses(<<~RUBY) |
| 13 | + 1.to_d |
13 | 14 | RUBY
|
14 | 15 | end
|
15 | 16 |
|
16 |
| - it 'registers an offense and corrects when using `String#to_d`' do |
| 17 | + it 'registers an offense and corrects when using `BigDecimal` with float' do |
17 | 18 | expect_offense(<<~RUBY)
|
18 |
| - '1'.to_d |
19 |
| - ^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 19 | + BigDecimal(1.5, exception: true) |
| 20 | + ^^^ Convert float literal to string and pass it to `BigDecimal`. |
20 | 21 | RUBY
|
21 | 22 |
|
22 | 23 | expect_correction(<<~RUBY)
|
23 |
| - BigDecimal(1) |
| 24 | + BigDecimal('1.5', exception: true) |
24 | 25 | RUBY
|
25 | 26 | end
|
26 | 27 |
|
27 |
| - it 'registers an offense and corrects when using `BigDecimal` with float string' do |
| 28 | + it 'registers an offense and corrects when using `Float#to_d`' do |
28 | 29 | expect_offense(<<~RUBY)
|
29 |
| - BigDecimal('1.5', exception: true) |
30 |
| - ^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 30 | + 1.5.to_d(exception: true) |
| 31 | + ^^^ Convert float literal to string and pass it to `BigDecimal`. |
31 | 32 | RUBY
|
32 | 33 |
|
33 | 34 | expect_correction(<<~RUBY)
|
34 |
| - BigDecimal(1.5, exception: true) |
| 35 | + BigDecimal('1.5', exception: true) |
35 | 36 | RUBY
|
36 | 37 | end
|
37 | 38 |
|
38 |
| - it 'registers an offense and corrects when using float `String#to_d`' do |
| 39 | + it 'registers an offense when using `BigDecimal` with float and precision' do |
39 | 40 | expect_offense(<<~RUBY)
|
40 |
| - '1.5'.to_d(exception: true) |
41 |
| - ^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 41 | + BigDecimal(3.14, 1) |
| 42 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
42 | 43 | RUBY
|
43 | 44 |
|
44 | 45 | expect_correction(<<~RUBY)
|
45 |
| - BigDecimal(1.5, exception: true) |
| 46 | + BigDecimal('3.14', 1) |
46 | 47 | RUBY
|
47 | 48 | end
|
48 | 49 |
|
49 |
| - it 'registers an offense when using `BigDecimal` with float string and precision' do |
| 50 | + it 'registers an offense when using `Float#to_d` with precision' do |
50 | 51 | expect_offense(<<~RUBY)
|
51 |
| - BigDecimal('3.14', 1) |
52 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 52 | + 3.14.to_d(1) |
| 53 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
53 | 54 | RUBY
|
54 | 55 |
|
55 | 56 | expect_correction(<<~RUBY)
|
56 |
| - BigDecimal(3.14, 1) |
| 57 | + BigDecimal('3.14', 1) |
57 | 58 | RUBY
|
58 | 59 | end
|
59 | 60 |
|
60 |
| - it 'registers an offense when using float `String#to_d` with precision' do |
| 61 | + it 'registers an offense when using `BigDecimal` with float and non-literal precision' do |
61 | 62 | expect_offense(<<~RUBY)
|
62 |
| - '3.14'.to_d(1) |
63 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 63 | + precision = 1 |
| 64 | + BigDecimal(3.14, precision) |
| 65 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
64 | 66 | RUBY
|
65 | 67 |
|
66 | 68 | expect_correction(<<~RUBY)
|
67 |
| - BigDecimal(3.14, 1) |
| 69 | + precision = 1 |
| 70 | + BigDecimal('3.14', precision) |
68 | 71 | RUBY
|
69 | 72 | end
|
70 | 73 |
|
71 |
| - it 'registers an offense when using `BigDecimal` with float string and non-literal precision' do |
| 74 | + it 'registers an offense when using `Float#to_d` with non-literal precision' do |
72 | 75 | expect_offense(<<~RUBY)
|
73 | 76 | precision = 1
|
74 |
| - BigDecimal('3.14', precision) |
75 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 77 | + 3.14.to_d(precision) |
| 78 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
76 | 79 | RUBY
|
77 | 80 |
|
78 | 81 | expect_correction(<<~RUBY)
|
79 | 82 | precision = 1
|
80 |
| - BigDecimal(3.14, precision) |
| 83 | + BigDecimal('3.14', precision) |
81 | 84 | RUBY
|
82 | 85 | end
|
83 | 86 |
|
84 |
| - it 'registers an offense when using float `String#to_d` with non-literal precision' do |
| 87 | + it 'registers an offense when using `BigDecimal` with float, precision, and a keyword argument' do |
85 | 88 | expect_offense(<<~RUBY)
|
86 |
| - precision = 1 |
87 |
| - '3.14'.to_d(precision) |
88 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 89 | + BigDecimal(3.14, 1, exception: true) |
| 90 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
89 | 91 | RUBY
|
90 | 92 |
|
91 | 93 | expect_correction(<<~RUBY)
|
92 |
| - precision = 1 |
93 |
| - BigDecimal(3.14, precision) |
| 94 | + BigDecimal('3.14', 1, exception: true) |
94 | 95 | RUBY
|
95 | 96 | end
|
96 | 97 |
|
97 |
| - it 'registers an offense when using `BigDecimal` with float string, precision, and a keyword argument' do |
| 98 | + it 'registers an offense when using `Float#to_d` with precision and a keyword argument' do |
98 | 99 | expect_offense(<<~RUBY)
|
99 |
| - BigDecimal('3.14', 1, exception: true) |
100 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 100 | + 3.14.to_d(1, exception: true) |
| 101 | + ^^^^ Convert float literal to string and pass it to `BigDecimal`. |
101 | 102 | RUBY
|
102 | 103 |
|
103 | 104 | expect_correction(<<~RUBY)
|
104 |
| - BigDecimal(3.14, 1, exception: true) |
| 105 | + BigDecimal('3.14', 1, exception: true) |
105 | 106 | RUBY
|
106 | 107 | end
|
107 | 108 |
|
108 |
| - it 'registers an offense when using float `String#to_d` with precision and a keyword argument' do |
| 109 | + it 'registers an offense when using `BigDecimal` with integer string' do |
109 | 110 | expect_offense(<<~RUBY)
|
110 |
| - '3.14'.to_d(1, exception: true) |
111 |
| - ^^^^^^ Convert string literal to numeric and pass it to `BigDecimal`. |
| 111 | + BigDecimal('1') |
| 112 | + ^^^ Convert string literal to integer and pass it to `BigDecimal`. |
112 | 113 | RUBY
|
113 | 114 |
|
114 | 115 | expect_correction(<<~RUBY)
|
115 |
| - BigDecimal(3.14, 1, exception: true) |
| 116 | + BigDecimal(1) |
116 | 117 | RUBY
|
117 | 118 | end
|
118 | 119 |
|
119 |
| - it 'does not register an offense when using `BigDecimal` with integer' do |
120 |
| - expect_no_offenses(<<~RUBY) |
121 |
| - BigDecimal(1) |
| 120 | + it 'registers an offense when using `String#to_d` for integer string' do |
| 121 | + expect_offense(<<~RUBY) |
| 122 | + '1'.to_d |
| 123 | + ^^^ Convert string literal to integer and pass it to `BigDecimal`. |
122 | 124 | RUBY
|
123 |
| - end |
124 | 125 |
|
125 |
| - it 'does not register an offense when using `Integer#to_d`' do |
126 |
| - expect_no_offenses(<<~RUBY) |
| 126 | + expect_correction(<<~RUBY) |
127 | 127 | 1.to_d
|
128 | 128 | RUBY
|
129 | 129 | end
|
|
0 commit comments