|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | | -RSpec.describe Money, "formatting" do |
| 3 | +# rubocop:disable RSpec/DescribeClass |
| 4 | +RSpec.describe "Money formatting" do |
4 | 5 | context "without i18n" do |
5 | 6 | subject(:money) { Money.empty("USD") } |
6 | 7 |
|
|
108 | 109 | expect(Money.zero.format(:display_free)).to eq('free') |
109 | 110 | end |
110 | 111 |
|
111 | | - context "Locale :ja" do |
| 112 | + context "when in Japanese" do |
112 | 113 | before { @_locale = I18n.locale; I18n.locale = :ja } |
113 | 114 |
|
114 | 115 | after { I18n.locale = @_locale } |
115 | 116 |
|
116 | | - it "formats Japanese currency in Japanese properly" do |
| 117 | + it "formats Japanese currency properly" do |
117 | 118 | money = Money.new(1000, "JPY") |
118 | 119 | expect(money.format).to eq "¥1,000" |
119 | 120 | expect(money.format(format: "%n%u", symbol: "円")).to eq "1,000円" |
|
218 | 219 | Money.default_formatting_rules = nil |
219 | 220 | end |
220 | 221 |
|
221 | | - context 'acknowledges provided rule' do |
222 | | - it 'acknowledges provided rule' do |
223 | | - expect(Money.new(100, 'USD').format(with_currency: true)).to eq '$1.00 USD' |
224 | | - end |
| 222 | + it 'acknowledges provided rule' do |
| 223 | + expect(Money.new(100, 'USD').format(with_currency: true)).to eq '$1.00 USD' |
225 | 224 | end |
226 | 225 | end |
227 | 226 |
|
|
460 | 459 | expect(Money.new(100000, "ZWD").format).to eq "$1,000.00" |
461 | 460 | end |
462 | 461 |
|
463 | | - context "currency locale_backend i18n", :locale_backend_currency do |
| 462 | + context "with a currency locale_backend i18n", :locale_backend_currency do |
464 | 463 | it "respects explicit overriding of thousands_separator/delimiter when decimal_mark/separator collide and there's no decimal component for currencies that have no subunit" do |
465 | 464 | expect(Money.new(300_000, 'ISK').format(thousands_separator: ",", decimal_mark: '.')).to eq "300,000 kr." |
466 | 465 | end |
|
641 | 640 | end |
642 | 641 | end |
643 | 642 |
|
644 | | - context "custom currencies with 4 decimal places" do |
| 643 | + context "with custom currencies with 4 decimal places" do |
645 | 644 | let(:bar) do |
646 | 645 | { |
647 | 646 | priority: 1, |
|
716 | 715 | end |
717 | 716 | end |
718 | 717 |
|
719 | | - context "currencies with ambiguous signs" do |
720 | | - it "returns ambiguous signs when disambiguate is not set" do |
721 | | - expect(Money.new(1999_98, "USD").format).to eq("$1,999.98") |
722 | | - expect(Money.new(1999_98, "CAD").format).to eq("$1,999.98") |
723 | | - expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr.") |
724 | | - expect(Money.new(1999_98, "NOK").format).to eq("1.999,98 kr") |
725 | | - expect(Money.new(1999_98, "SEK").format).to eq("1 999,98 kr") |
726 | | - expect(Money.new(1999_98, "BCH").format).to eq("0.00199998 ₿") |
727 | | - expect(Money.new(1999_98, "USDC").format).to eq("0.199998 USDC") |
728 | | - end |
729 | | - |
730 | | - it "returns ambiguous signs when disambiguate is false" do |
731 | | - expect(Money.new(1999_98, "USD").format(disambiguate: false)).to eq("$1,999.98") |
732 | | - expect(Money.new(1999_98, "CAD").format(disambiguate: false)).to eq("$1,999.98") |
733 | | - expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr.") |
734 | | - expect(Money.new(1999_98, "NOK").format(disambiguate: false)).to eq("1.999,98 kr") |
735 | | - expect(Money.new(1999_98, "SEK").format(disambiguate: false)).to eq("1 999,98 kr") |
736 | | - expect(Money.new(1999_98, "BCH").format(disambiguate: false)).to eq("0.00199998 ₿") |
737 | | - expect(Money.new(1999_98, "USDC").format(disambiguate: false)).to eq("0.199998 USDC") |
738 | | - end |
739 | | - |
740 | | - it "returns disambiguate signs when disambiguate: true" do |
741 | | - expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("US$1,999.98") |
742 | | - expect(Money.new(1999_98, "CAD").format(disambiguate: true)).to eq("C$1,999.98") |
743 | | - expect(Money.new(1999_98, "DKK").format(disambiguate: true)).to eq("1.999,98 DKK") |
744 | | - expect(Money.new(1999_98, "NOK").format(disambiguate: true)).to eq("1.999,98 NOK") |
745 | | - expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK") |
746 | | - expect(Money.new(1999_98, "BCH").format(disambiguate: true)).to eq("0.00199998 ₿CH") |
747 | | - expect(Money.new(1999_98, "USDC").format(disambiguate: true)).to eq("0.199998 USDC") |
748 | | - end |
749 | | - |
750 | | - it "returns disambiguate signs when disambiguate: true and symbol: true" do |
751 | | - expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)).to eq("US$1,999.98") |
752 | | - expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: true)).to eq("C$1,999.98") |
753 | | - expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: true)).to eq("1.999,98 DKK") |
754 | | - expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: true)).to eq("1.999,98 NOK") |
755 | | - expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: true)).to eq("1 999,98 SEK") |
756 | | - expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: true)).to eq("0.00199998 ₿CH") |
757 | | - expect(Money.new(1999_98, "USDC").format(disambiguate: true, symbol: true)).to eq("0.199998 USDC") |
758 | | - end |
759 | | - |
760 | | - it "returns no signs when disambiguate: true and symbol: false" do |
761 | | - expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: false)).to eq("1,999.98") |
762 | | - expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: false)).to eq("1,999.98") |
763 | | - expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: false)).to eq("1.999,98") |
764 | | - expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: false)).to eq("1.999,98") |
765 | | - expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: false)).to eq("1 999,98") |
766 | | - expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: false)).to eq("0.00199998") |
767 | | - expect(Money.new(1999_98, "USDC").format(disambiguate: true, symbol: false)).to eq("0.199998") |
768 | | - end |
769 | | - |
770 | | - it "nevers return an ambiguous format with disambiguate: true" do |
771 | | - formatted_results = {} |
772 | | - |
773 | | - # When we format the same amount in all known currencies, disambiguate should return |
774 | | - # all different values |
775 | | - Money::Currency.all.each do |currency| |
776 | | - format = Money.new(1999_98, currency).format(disambiguate: true) |
777 | | - expect(formatted_results.keys).not_to include(format), "Format '#{format}' for #{currency} is ambiguous with currency #{formatted_results[format]}." |
778 | | - formatted_results[format] = currency |
| 718 | + context "with currencies with ambiguous signs" do |
| 719 | + context "when disambiguate is not set" do |
| 720 | + it "returns ambiguous signs" do |
| 721 | + expect(Money.new(1999_98, "USD").format).to eq("$1,999.98") |
| 722 | + expect(Money.new(1999_98, "CAD").format).to eq("$1,999.98") |
| 723 | + expect(Money.new(1999_98, "DKK").format).to eq("1.999,98 kr.") |
| 724 | + expect(Money.new(1999_98, "NOK").format).to eq("1.999,98 kr") |
| 725 | + expect(Money.new(1999_98, "SEK").format).to eq("1 999,98 kr") |
| 726 | + expect(Money.new(1999_98, "BCH").format).to eq("0.00199998 ₿") |
| 727 | + expect(Money.new(1999_98, "USDC").format).to eq("0.199998 USDC") |
| 728 | + end |
| 729 | + end |
| 730 | + |
| 731 | + context "when disambiguate is false" do |
| 732 | + it "returns ambiguous signs" do |
| 733 | + expect(Money.new(1999_98, "USD").format(disambiguate: false)).to eq("$1,999.98") |
| 734 | + expect(Money.new(1999_98, "CAD").format(disambiguate: false)).to eq("$1,999.98") |
| 735 | + expect(Money.new(1999_98, "DKK").format(disambiguate: false)).to eq("1.999,98 kr.") |
| 736 | + expect(Money.new(1999_98, "NOK").format(disambiguate: false)).to eq("1.999,98 kr") |
| 737 | + expect(Money.new(1999_98, "SEK").format(disambiguate: false)).to eq("1 999,98 kr") |
| 738 | + expect(Money.new(1999_98, "BCH").format(disambiguate: false)).to eq("0.00199998 ₿") |
| 739 | + expect(Money.new(1999_98, "USDC").format(disambiguate: false)).to eq("0.199998 USDC") |
| 740 | + end |
| 741 | + end |
| 742 | + |
| 743 | + context "when disambiguate is true" do |
| 744 | + it "returns disambiguous signs" do |
| 745 | + expect(Money.new(1999_98, "USD").format(disambiguate: true)).to eq("US$1,999.98") |
| 746 | + expect(Money.new(1999_98, "CAD").format(disambiguate: true)).to eq("C$1,999.98") |
| 747 | + expect(Money.new(1999_98, "DKK").format(disambiguate: true)).to eq("1.999,98 DKK") |
| 748 | + expect(Money.new(1999_98, "NOK").format(disambiguate: true)).to eq("1.999,98 NOK") |
| 749 | + expect(Money.new(1999_98, "SEK").format(disambiguate: true)).to eq("1 999,98 SEK") |
| 750 | + expect(Money.new(1999_98, "BCH").format(disambiguate: true)).to eq("0.00199998 ₿CH") |
| 751 | + expect(Money.new(1999_98, "USDC").format(disambiguate: true)).to eq("0.199998 USDC") |
| 752 | + end |
| 753 | + |
| 754 | + it "nevers returns an ambiguous format" do |
| 755 | + formatted_results = {} |
| 756 | + |
| 757 | + # When we format the same amount in all known currencies, disambiguate |
| 758 | + # should return all different values |
| 759 | + Money::Currency.all.each do |currency| |
| 760 | + format = Money.new(1999_98, currency).format(disambiguate: true) |
| 761 | + expect(formatted_results.keys) |
| 762 | + .not_to include(format), |
| 763 | + "Format '#{format}' for #{currency} is ambiguous with currency #{formatted_results[format]}." |
| 764 | + formatted_results[format] = currency |
| 765 | + end |
| 766 | + end |
| 767 | + end |
| 768 | + |
| 769 | + context "when disambiguate is true and symbol is true" do |
| 770 | + it "returns disambiguate signs" do |
| 771 | + expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: true)).to eq("US$1,999.98") |
| 772 | + expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: true)).to eq("C$1,999.98") |
| 773 | + expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: true)).to eq("1.999,98 DKK") |
| 774 | + expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: true)).to eq("1.999,98 NOK") |
| 775 | + expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: true)).to eq("1 999,98 SEK") |
| 776 | + expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: true)).to eq("0.00199998 ₿CH") |
| 777 | + expect(Money.new(1999_98, "USDC").format(disambiguate: true, symbol: true)).to eq("0.199998 USDC") |
| 778 | + end |
| 779 | + end |
| 780 | + |
| 781 | + context "when disambiguate is true and symbol is false" do |
| 782 | + it "returns no signs" do |
| 783 | + expect(Money.new(1999_98, "USD").format(disambiguate: true, symbol: false)).to eq("1,999.98") |
| 784 | + expect(Money.new(1999_98, "CAD").format(disambiguate: true, symbol: false)).to eq("1,999.98") |
| 785 | + expect(Money.new(1999_98, "DKK").format(disambiguate: true, symbol: false)).to eq("1.999,98") |
| 786 | + expect(Money.new(1999_98, "NOK").format(disambiguate: true, symbol: false)).to eq("1.999,98") |
| 787 | + expect(Money.new(1999_98, "SEK").format(disambiguate: true, symbol: false)).to eq("1 999,98") |
| 788 | + expect(Money.new(1999_98, "BCH").format(disambiguate: true, symbol: false)).to eq("0.00199998") |
| 789 | + expect(Money.new(1999_98, "USDC").format(disambiguate: true, symbol: false)).to eq("0.199998") |
779 | 790 | end |
780 | 791 | end |
781 | 792 |
|
|
802 | 813 | end |
803 | 814 | end |
804 | 815 | end |
| 816 | +# rubocop:enable RSpec/DescribeClass |
0 commit comments