|
1 | 1 | # frozen_string_literal: true |
2 | 2 | require 'spec_helper' |
| 3 | +require 'tempfile' |
3 | 4 |
|
4 | 5 | RSpec.describe "Currency" do |
5 | 6 | CURRENCY_DATA = { |
|
17 | 18 |
|
18 | 19 | let(:currency) { Money::Currency.new('usd') } |
19 | 20 |
|
| 21 | + let(:mock_custom_currency) do |
| 22 | + { |
| 23 | + "credits" => { |
| 24 | + "iso_code" => "CREDITS", |
| 25 | + "name" => "Loyalty Points", |
| 26 | + "symbol" => "CR", |
| 27 | + "disambiguate_symbol" => "CR", |
| 28 | + "subunit_to_unit" => 1, |
| 29 | + "smallest_denomination" => 1, |
| 30 | + "decimal_mark" => "." |
| 31 | + } |
| 32 | + } |
| 33 | + end |
| 34 | + |
20 | 35 | let(:mock_crypto_currency) do |
21 | 36 | { |
22 | 37 | "usdc" => { |
|
69 | 84 | expect(Money::Currency.find("USDC")).to be_nil |
70 | 85 | end |
71 | 86 | end |
| 87 | + |
| 88 | + it "looks up custom currencies when path is set" do |
| 89 | + allow(Money::Currency).to receive(:currencies).and_return({}) |
| 90 | + allow(Money::Currency).to receive(:custom_currencies).with('/tmp/custom.yml').and_return(mock_custom_currency) |
| 91 | + |
| 92 | + configure(custom_currency_path: '/tmp/custom.yml') do |
| 93 | + currency = Money::Currency.new('CREDITS') |
| 94 | + expect(currency.iso_code).to eq('CREDITS') |
| 95 | + expect(currency.symbol).to eq('CR') |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + it "doesn't look up custom currencies when path is nil" do |
| 100 | + expect(Money::Currency.find("CREDITS")).to eq(nil) |
| 101 | + end |
| 102 | + |
| 103 | + it "can't override ISO currencies with custom currencies" do |
| 104 | + allow(Money::Currency).to receive(:custom_currencies).with('/tmp/custom.yml').and_return( |
| 105 | + "usd" => { |
| 106 | + "iso_code" => "USD", |
| 107 | + "name" => "Fake Dollar", |
| 108 | + "symbol" => "FAKE", |
| 109 | + "disambiguate_symbol" => "FAKE", |
| 110 | + "subunit_to_unit" => 1, |
| 111 | + "smallest_denomination" => 1, |
| 112 | + "decimal_mark" => "." |
| 113 | + } |
| 114 | + ) |
| 115 | + |
| 116 | + configure(custom_currency_path: '/tmp/custom.yml') do |
| 117 | + currency = Money::Currency.new('USD') |
| 118 | + expect(currency.name).to eq('United States Dollar') |
| 119 | + expect(currency.symbol).to eq('$') |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + it "can't override crypto currencies with custom currencies" do |
| 124 | + allow(Money::Currency).to receive(:currencies).and_return({}) |
| 125 | + allow(Money::Currency).to receive(:crypto_currencies).and_return(mock_crypto_currency) |
| 126 | + allow(Money::Currency).to receive(:custom_currencies).with('/tmp/custom.yml').and_return( |
| 127 | + "usdc" => { |
| 128 | + "iso_code" => "USDC", |
| 129 | + "name" => "Fake USDC", |
| 130 | + "symbol" => "FAKE", |
| 131 | + "disambiguate_symbol" => "FAKE", |
| 132 | + "subunit_to_unit" => 1, |
| 133 | + "smallest_denomination" => 1, |
| 134 | + "decimal_mark" => "." |
| 135 | + } |
| 136 | + ) |
| 137 | + |
| 138 | + configure(experimental_crypto_currencies: true, custom_currency_path: '/tmp/custom.yml') do |
| 139 | + currency = Money::Currency.new('USDC') |
| 140 | + expect(currency.name).to eq('USD Coin') |
| 141 | + expect(currency.symbol).to eq('USDC') |
| 142 | + end |
| 143 | + end |
| 144 | + |
| 145 | + it "loads custom currencies end-to-end from a YAML file" do |
| 146 | + file = Tempfile.new(['custom_currencies', '.yml']) |
| 147 | + file.write({ |
| 148 | + "credits" => { |
| 149 | + "iso_code" => "CREDITS", |
| 150 | + "name" => "Loyalty Points", |
| 151 | + "symbol" => "CR", |
| 152 | + "disambiguate_symbol" => "CR", |
| 153 | + "subunit_to_unit" => 1, |
| 154 | + "smallest_denomination" => 1, |
| 155 | + "decimal_mark" => "." |
| 156 | + } |
| 157 | + }.to_yaml) |
| 158 | + file.close |
| 159 | + |
| 160 | + configure(custom_currency_path: file.path) do |
| 161 | + money = Money.new(500, "CREDITS") |
| 162 | + expect(money.currency.iso_code).to eq("CREDITS") |
| 163 | + expect(money.currency.symbol).to eq("CR") |
| 164 | + expect(money.currency.name).to eq("Loyalty Points") |
| 165 | + expect(money.value).to eq(500) |
| 166 | + end |
| 167 | + ensure |
| 168 | + file.unlink |
| 169 | + end |
72 | 170 | end |
73 | 171 |
|
74 | 172 | describe ".find" do |
|
96 | 194 | expect(Money::Currency.find('USDC')).to eq(nil) |
97 | 195 | end |
98 | 196 | end |
| 197 | + |
| 198 | + it "returns custom currency when path is set" do |
| 199 | + allow(Money::Currency).to receive(:currencies).and_return({}) |
| 200 | + allow(Money::Currency).to receive(:custom_currencies).with('/tmp/custom.yml').and_return(mock_custom_currency) |
| 201 | + |
| 202 | + configure(custom_currency_path: '/tmp/custom.yml') do |
| 203 | + expect(Money::Currency.find('CREDITS')).not_to eq(nil) |
| 204 | + expect(Money::Currency.find('CREDITS').iso_code).to eq('CREDITS') |
| 205 | + end |
| 206 | + end |
| 207 | + |
| 208 | + it "returns nil for custom currency when path is not set" do |
| 209 | + expect(Money::Currency.find('CREDITS')).to eq(nil) |
| 210 | + end |
99 | 211 | end |
100 | 212 |
|
101 | 213 | describe ".find!" do |
|
123 | 235 | end |
124 | 236 | end |
125 | 237 |
|
| 238 | + describe ".custom_currencies" do |
| 239 | + it "loads custom currencies from the loader" do |
| 240 | + old_cache = Money::Currency.class_variable_get(:@@custom_currencies_cache) rescue nil |
| 241 | + Money::Currency.class_variable_set(:@@custom_currencies_cache, nil) |
| 242 | + |
| 243 | + allow(Money::Currency::Loader).to receive(:load_custom_currencies) |
| 244 | + .with('/tmp/custom.yml') |
| 245 | + .and_return(mock_custom_currency) |
| 246 | + |
| 247 | + expect(Money::Currency.custom_currencies('/tmp/custom.yml')).to eq(mock_custom_currency) |
| 248 | + expect(Money::Currency.custom_currencies('/tmp/custom.yml')).to eq(mock_custom_currency) # Second call to verify caching |
| 249 | + expect(Money::Currency::Loader).to have_received(:load_custom_currencies).with('/tmp/custom.yml').once |
| 250 | + |
| 251 | + Money::Currency.class_variable_set(:@@custom_currencies_cache, old_cache) if old_cache |
| 252 | + end |
| 253 | + end |
| 254 | + |
126 | 255 | CURRENCY_DATA.each do |attribute, value| |
127 | 256 | describe "##{attribute}" do |
128 | 257 | it 'returns the correct value' do |
|
0 commit comments