-
Notifications
You must be signed in to change notification settings - Fork 640
Add ability to nest Money.with_rounding_mode blocks
#1128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9e109a5
Move .with_rounding_mode mode specs to it's own block
pawelma 1989fa3
Refactor .rounding_mode deprecated method spec
pawelma 187a005
Add failing spec for the .with_rounding_mode blocks nesting
pawelma ed324fd
Allow .with_rounding_mode blocks nesting
pawelma 97da6d3
Test concurrency for the .with_rounding_mode block
pawelma 69d0588
Update changelog with nested rounding mode nesting feature mention
pawelma 794a6ff
Update authors according to the Contribution Guidelines notes
pawelma a278132
Merge branch 'main' into main
pawelma 6d474c6
Merge branch 'main' into main
semmons99 63a8c4d
Merge branch 'main' into main
yukideluxe 56c2805
Merge branch 'main' into main
pawelma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,17 +224,33 @@ | |
| expect(Money.from_amount(1, "USD", bank).bank).to eq bank | ||
| end | ||
|
|
||
| it 'warns about rounding_mode deprecation' do | ||
| context 'given a currency is provided' do | ||
| context 'and the currency is nil' do | ||
| let(:currency) { nil } | ||
|
|
||
| it "should have the default currency" do | ||
| expect(Money.from_amount(1, currency).currency).to eq Money.default_currency | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe '.with_rounding_mode' do | ||
| it 'sets the .rounding_mode method deprecated' do | ||
| allow(Money).to receive(:warn) | ||
| allow(Money).to receive(:with_rounding_mode).and_call_original | ||
|
|
||
| expect(Money.from_amount(1.999).to_d).to eq 2 | ||
| expect(Money.rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| rounding_block = lambda do | ||
| Money.from_amount(1.999).to_d | ||
| end).to eq 1.99 | ||
| end | ||
|
|
||
| expect(Money.from_amount(1.999).to_d).to eq 2 | ||
| expect(Money.rounding_mode(BigDecimal::ROUND_DOWN, &rounding_block)).to eq 1.99 | ||
| expect(Money) | ||
| .to have_received(:warn) | ||
| .with('[DEPRECATION] calling `rounding_mode` with a block is deprecated. ' \ | ||
| 'Please use `.with_rounding_mode` instead.') | ||
| expect(Money).to have_received(:with_rounding_mode).with(BigDecimal::ROUND_DOWN, &rounding_block) | ||
| end | ||
|
|
||
| it 'rounds using with_rounding_mode' do | ||
|
|
@@ -244,14 +260,77 @@ | |
| end).to eq 1.99 | ||
| end | ||
|
|
||
| context 'given a currency is provided' do | ||
| context 'and the currency is nil' do | ||
| let(:currency) { nil } | ||
| it 'allows blocks nesting' do | ||
| Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| expect(Money.rounding_mode).to eq(BigDecimal::ROUND_DOWN) | ||
|
|
||
| it "should have the default currency" do | ||
| expect(Money.from_amount(1, currency).currency).to eq Money.default_currency | ||
| Money.with_rounding_mode(BigDecimal::ROUND_UP) do | ||
| expect(Money.rounding_mode).to eq(BigDecimal::ROUND_UP) | ||
| expect(Money.from_amount(2.137).to_d).to eq 2.14 | ||
| end | ||
|
|
||
| expect( | ||
| Money.rounding_mode | ||
| ).to eq(BigDecimal::ROUND_DOWN), 'Outer mode should be restored after inner block' | ||
| expect(Money.from_amount(2.137).to_d).to eq 2.13 | ||
| end | ||
|
|
||
| expect( | ||
| Money.rounding_mode | ||
| ).to eq(BigDecimal::ROUND_HALF_EVEN), 'Original mode should be restored after outer block' | ||
| expect(Money.from_amount(2.137).to_d).to eq 2.14 | ||
| end | ||
|
|
||
| it 'safely handles concurrent usage in different threads' do | ||
| test_value = 1.999 | ||
| expected_down = 1.99 | ||
| expected_up = 2.00 | ||
|
|
||
| results = Queue.new | ||
|
|
||
| test_money_with_rounding_mode = lambda do |rounding_mode| | ||
| Thread.new do | ||
| Money.with_rounding_mode(rounding_mode) do | ||
| results.push({ | ||
| set_rounding_mode: rounding_mode, | ||
| mode: Money.rounding_mode, | ||
| result: Money.from_amount(test_value).to_d | ||
| }) | ||
|
|
||
| # Sleep to allow interleaving with other thread | ||
| sleep 0.01 | ||
|
|
||
| results.push({ | ||
| set_rounding_mode: rounding_mode, | ||
| mode: Money.rounding_mode, | ||
| result: Money.from_amount(test_value).to_d | ||
| }) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| [ | ||
| test_money_with_rounding_mode.call(BigDecimal::ROUND_DOWN), | ||
| test_money_with_rounding_mode.call(BigDecimal::ROUND_UP) | ||
| ].each(&:join) | ||
|
|
||
| all_results = [] | ||
| all_results << results.pop until results.empty? | ||
|
|
||
| round_down_results = all_results.select { |r| r[:set_rounding_mode] == BigDecimal::ROUND_DOWN } | ||
| round_up_results = all_results.select { |r| r[:set_rounding_mode] == BigDecimal::ROUND_UP } | ||
|
|
||
| round_down_results.each do |result| | ||
| expect(result[:mode]).to eq(BigDecimal::ROUND_DOWN) | ||
| expect(result[:result]).to eq(expected_down) | ||
| end | ||
|
|
||
| round_up_results.each do |result| | ||
| expect(result[:mode]).to eq(BigDecimal::ROUND_UP) | ||
| expect(result[:result]).to eq(expected_up) | ||
| end | ||
|
|
||
| expect(Money.rounding_mode).to eq(BigDecimal::ROUND_HALF_EVEN) | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -337,23 +416,23 @@ def expectation.fractional | |
|
|
||
| context "with a block" do | ||
| it "respects the rounding_mode" do | ||
| expect(Money.rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| expect(Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks for changing this to use the non-deprecated method! 😍 |
||
| Money.new(1.9).fractional | ||
| end).to eq 1 | ||
|
|
||
| expect(Money.rounding_mode(BigDecimal::ROUND_UP) do | ||
| expect(Money.with_rounding_mode(BigDecimal::ROUND_UP) do | ||
| Money.new(1.1).fractional | ||
| end).to eq 2 | ||
|
|
||
| expect(Money.rounding_mode).to eq BigDecimal::ROUND_HALF_EVEN | ||
| end | ||
|
|
||
| it "works for multiplication within a block" do | ||
| Money.rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| Money.with_rounding_mode(BigDecimal::ROUND_DOWN) do | ||
| expect((Money.new(1_00) * "0.019".to_d).fractional).to eq 1 | ||
| end | ||
|
|
||
| Money.rounding_mode(BigDecimal::ROUND_UP) do | ||
| Money.with_rounding_mode(BigDecimal::ROUND_UP) do | ||
| expect((Money.new(1_00) * "0.011".to_d).fractional).to eq 2 | ||
| end | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I always find it very difficult to test these scenarios and you made it so easy. I'll keep this in mind for my future self 👏🏻