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