|
4 | 4 | require 'yaml' |
5 | 5 |
|
6 | 6 | RSpec.describe Money::Bank::VariableExchange do |
| 7 | + subject(:bank) { described_class.new } |
| 8 | + |
7 | 9 | describe "#initialize" do |
8 | 10 | context "without &block" do |
9 | 11 | let(:bank) do |
|
108 | 110 |
|
109 | 111 | describe "#add_rate" do |
110 | 112 | it 'delegates to store#add_rate' do |
111 | | - expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 |
112 | | - expect(subject.add_rate('USD', 'EUR', 1.25)).to be 1.25 |
| 113 | + expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 |
| 114 | + expect(bank.add_rate('USD', 'EUR', 1.25)).to be 1.25 |
113 | 115 | end |
114 | 116 |
|
115 | 117 | it "adds rates with correct ISO codes" do |
116 | | - expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 0.788332676) |
117 | | - subject.add_rate("USD", "EUR", 0.788332676) |
| 118 | + expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 0.788332676) |
| 119 | + bank.add_rate("USD", "EUR", 0.788332676) |
118 | 120 |
|
119 | | - expect(subject.store).to receive(:add_rate).with('EUR', 'JPY', 122.631477) |
120 | | - subject.add_rate("EUR", "YEN", 122.631477) |
| 121 | + expect(bank.store).to receive(:add_rate).with('EUR', 'JPY', 122.631477) |
| 122 | + bank.add_rate("EUR", "YEN", 122.631477) |
121 | 123 | end |
122 | 124 |
|
123 | 125 | it "treats currency names case-insensitively" do |
124 | | - subject.add_rate("usd", "eur", 1) |
125 | | - expect(subject.get_rate('USD', 'EUR')).to eq 1 |
| 126 | + bank.add_rate("usd", "eur", 1) |
| 127 | + expect(bank.get_rate('USD', 'EUR')).to eq 1 |
126 | 128 | end |
127 | 129 | end |
128 | 130 |
|
129 | 131 | describe "#set_rate" do |
130 | 132 | it 'delegates to store#add_rate' do |
131 | | - expect(subject.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 |
132 | | - expect(subject.set_rate('USD', 'EUR', 1.25)).to be 1.25 |
| 133 | + expect(bank.store).to receive(:add_rate).with('USD', 'EUR', 1.25).and_return 1.25 |
| 134 | + expect(bank.set_rate('USD', 'EUR', 1.25)).to be 1.25 |
133 | 135 | end |
134 | 136 |
|
135 | 137 | it "sets a rate" do |
136 | | - subject.set_rate('USD', 'EUR', 1.25) |
137 | | - expect(subject.store.get_rate('USD', 'EUR')).to eq 1.25 |
| 138 | + bank.set_rate('USD', 'EUR', 1.25) |
| 139 | + expect(bank.store.get_rate('USD', 'EUR')).to eq 1.25 |
138 | 140 | end |
139 | 141 |
|
140 | 142 | it "raises an UnknownCurrency error when an unknown currency is passed" do |
141 | | - expect { subject.set_rate('AAA', 'BBB', 1.25) }.to raise_error(Money::Currency::UnknownCurrency) |
| 143 | + expect { bank.set_rate('AAA', 'BBB', 1.25) }.to raise_error(Money::Currency::UnknownCurrency) |
142 | 144 | end |
143 | 145 | end |
144 | 146 |
|
145 | 147 | describe "#get_rate" do |
146 | 148 | it "returns a rate" do |
147 | | - subject.set_rate('USD', 'EUR', 1.25) |
148 | | - expect(subject.get_rate('USD', 'EUR')).to eq 1.25 |
| 149 | + bank.set_rate('USD', 'EUR', 1.25) |
| 150 | + expect(bank.get_rate('USD', 'EUR')).to eq 1.25 |
149 | 151 | end |
150 | 152 |
|
151 | 153 | it "raises an UnknownCurrency error when an unknown currency is passed" do |
152 | | - expect { subject.get_rate('AAA', 'BBB') }.to raise_error(Money::Currency::UnknownCurrency) |
| 154 | + expect { bank.get_rate('AAA', 'BBB') }.to raise_error(Money::Currency::UnknownCurrency) |
153 | 155 | end |
154 | 156 |
|
155 | 157 | it "delegates options to store, options are a no-op" do |
156 | | - expect(subject.store).to receive(:get_rate).with('USD', 'EUR') |
157 | | - subject.get_rate('USD', 'EUR') |
| 158 | + expect(bank.store).to receive(:get_rate).with('USD', 'EUR') |
| 159 | + bank.get_rate('USD', 'EUR') |
158 | 160 | end |
159 | 161 | end |
160 | 162 |
|
161 | 163 | describe "#export_rates" do |
162 | 164 | before do |
163 | | - subject.set_rate('USD', 'EUR', 1.25) |
164 | | - subject.set_rate('USD', 'JPY', 2.55) |
| 165 | + bank.set_rate('USD', 'EUR', 1.25) |
| 166 | + bank.set_rate('USD', 'JPY', 2.55) |
165 | 167 |
|
166 | 168 | @rates = { "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 } |
167 | 169 | end |
168 | 170 |
|
169 | 171 | context "with format == :json" do |
170 | 172 | it "returns rates formatted as json" do |
171 | | - json = subject.export_rates(:json) |
| 173 | + json = bank.export_rates(:json) |
172 | 174 | expect(JSON.load(json)).to eq @rates |
173 | 175 | end |
174 | 176 | end |
175 | 177 |
|
176 | 178 | context "with format == :ruby" do |
177 | 179 | it "returns rates formatted as ruby objects" do |
178 | | - expect(Marshal.load(subject.export_rates(:ruby))).to eq @rates |
| 180 | + expect(Marshal.load(bank.export_rates(:ruby))).to eq @rates |
179 | 181 | end |
180 | 182 | end |
181 | 183 |
|
182 | 184 | context "with format == :yaml" do |
183 | 185 | it "returns rates formatted as yaml" do |
184 | | - yaml = subject.export_rates(:yaml) |
| 186 | + yaml = bank.export_rates(:yaml) |
185 | 187 | expect(YAML.load(yaml)).to eq @rates |
186 | 188 | end |
187 | 189 | end |
188 | 190 |
|
189 | 191 | context "with unknown format" do |
190 | 192 | it "raises Money::Bank::UnknownRateFormat" do |
191 | | - expect { subject.export_rates(:foo) }.to raise_error Money::Bank::UnknownRateFormat |
| 193 | + expect { bank.export_rates(:foo) }.to raise_error Money::Bank::UnknownRateFormat |
192 | 194 | end |
193 | 195 | end |
194 | 196 |
|
|
198 | 200 | expect(File).to receive(:open).with('null', 'w').and_yield(f) |
199 | 201 | expect(f).to receive(:write).with(JSON.dump(@rates)) |
200 | 202 |
|
201 | | - subject.export_rates(:json, 'null') |
| 203 | + bank.export_rates(:json, 'null') |
202 | 204 | end |
203 | 205 | end |
204 | 206 |
|
205 | 207 | it "delegates execution to store, options are a no-op" do |
206 | | - expect(subject.store).to receive(:transaction) |
207 | | - subject.export_rates(:yaml, nil, foo: 1) |
| 208 | + expect(bank.store).to receive(:transaction) |
| 209 | + bank.export_rates(:yaml, nil, foo: 1) |
208 | 210 | end |
209 | 211 | end |
210 | 212 |
|
211 | 213 | describe "#import_rates" do |
212 | 214 | context "with format == :json" do |
213 | 215 | it "loads the rates provided" do |
214 | 216 | s = '{"USD_TO_EUR":1.25,"USD_TO_JPY":2.55}' |
215 | | - subject.import_rates(:json, s) |
216 | | - expect(subject.get_rate('USD', 'EUR')).to eq 1.25 |
217 | | - expect(subject.get_rate('USD', 'JPY')).to eq 2.55 |
| 217 | + bank.import_rates(:json, s) |
| 218 | + expect(bank.get_rate('USD', 'EUR')).to eq 1.25 |
| 219 | + expect(bank.get_rate('USD', 'JPY')).to eq 2.55 |
218 | 220 | end |
219 | 221 | end |
220 | 222 |
|
221 | 223 | context "with format == :ruby" do |
222 | 224 | let(:dump) { Marshal.dump({ "USD_TO_EUR" => 1.25, "USD_TO_JPY" => 2.55 }) } |
223 | 225 |
|
224 | 226 | it "loads the rates provided" do |
225 | | - subject.import_rates(:ruby, dump) |
| 227 | + bank.import_rates(:ruby, dump) |
226 | 228 |
|
227 | | - expect(subject.get_rate('USD', 'EUR')).to eq 1.25 |
228 | | - expect(subject.get_rate('USD', 'JPY')).to eq 2.55 |
| 229 | + expect(bank.get_rate('USD', 'EUR')).to eq 1.25 |
| 230 | + expect(bank.get_rate('USD', 'JPY')).to eq 2.55 |
229 | 231 | end |
230 | 232 |
|
| 233 | + # rubocop:disable RSpec/SubjectStub |
231 | 234 | it "prints a warning" do |
232 | | - allow(subject).to receive(:warn) |
| 235 | + allow(bank).to receive(:warn) |
233 | 236 |
|
234 | | - subject.import_rates(:ruby, dump) |
| 237 | + bank.import_rates(:ruby, dump) |
235 | 238 |
|
236 | | - expect(subject) |
| 239 | + expect(bank) |
237 | 240 | .to have_received(:warn) |
238 | 241 | .with(include('[WARNING] Using :ruby format when importing rates is potentially unsafe')) |
239 | 242 | end |
| 243 | + # rubocop:enable RSpec/SubjectStub |
240 | 244 | end |
241 | 245 |
|
242 | 246 | context "with format == :yaml" do |
243 | 247 | it "loads the rates provided" do |
244 | 248 | s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n" |
245 | | - subject.import_rates(:yaml, s) |
246 | | - expect(subject.get_rate('USD', 'EUR')).to eq 1.25 |
247 | | - expect(subject.get_rate('USD', 'JPY')).to eq 2.55 |
| 249 | + bank.import_rates(:yaml, s) |
| 250 | + expect(bank.get_rate('USD', 'EUR')).to eq 1.25 |
| 251 | + expect(bank.get_rate('USD', 'JPY')).to eq 2.55 |
248 | 252 | end |
249 | 253 | end |
250 | 254 |
|
251 | 255 | context "with unknown format" do |
252 | 256 | it "raises Money::Bank::UnknownRateFormat" do |
253 | | - expect { subject.import_rates(:foo, "") }.to raise_error Money::Bank::UnknownRateFormat |
| 257 | + expect { bank.import_rates(:foo, "") } |
| 258 | + .to raise_error Money::Bank::UnknownRateFormat |
254 | 259 | end |
255 | 260 | end |
256 | 261 |
|
257 | 262 | it "delegates execution to store#transaction" do |
258 | | - expect(subject.store).to receive(:transaction) |
| 263 | + expect(bank.store).to receive(:transaction) |
259 | 264 | s = "--- \nUSD_TO_EUR: 1.25\nUSD_TO_JPY: 2.55\n" |
260 | | - subject.import_rates(:yaml, s, foo: 1) |
| 265 | + bank.import_rates(:yaml, s, foo: 1) |
261 | 266 | end |
262 | 267 | end |
263 | 268 |
|
264 | 269 | describe "#marshal_dump" do |
265 | 270 | it "does not raise an error" do |
266 | | - expect { Marshal.dump(subject) }.not_to raise_error |
| 271 | + expect { Marshal.dump(bank) }.not_to raise_error |
267 | 272 | end |
268 | 273 |
|
269 | 274 | it "works with Marshal.load" do |
270 | | - bank = Marshal.load(Marshal.dump(subject)) |
| 275 | + new_bank = Marshal.load(Marshal.dump(bank)) |
271 | 276 |
|
272 | | - expect(bank.rates).to eq subject.rates |
273 | | - expect(bank.rounding_method).to eq subject.rounding_method |
| 277 | + expect(new_bank.rates).to eq bank.rates |
| 278 | + expect(new_bank.rounding_method).to eq bank.rounding_method |
274 | 279 | end |
275 | 280 | end |
276 | 281 | end |
0 commit comments