|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +FOO = { |
| 4 | + priority: 1, |
| 5 | + iso_code: "FOO", |
| 6 | + iso_numeric: "840", |
| 7 | + name: "United States Dollar", |
| 8 | + symbol: "$", |
| 9 | + subunit: "Cent", |
| 10 | + subunit_to_unit: 1000, |
| 11 | + symbol_first: true, |
| 12 | + html_entity: "$", |
| 13 | + decimal_mark: ".", |
| 14 | + thousands_separator: ",", |
| 15 | + smallest_denomination: 1, |
| 16 | +}.freeze |
| 17 | + |
3 | 18 | RSpec.describe Money::Currency do |
4 | | - FOO = '{ "priority": 1, "iso_code": "FOO", "iso_numeric": "840", "name": "United States Dollar", "symbol": "$", "subunit": "Cent", "subunit_to_unit": 1000, "symbol_first": true, "html_entity": "$", "decimal_mark": ".", "thousands_separator": ",", "smallest_denomination": 1 }' |
5 | | - |
6 | | - def register_foo(opts = {}) |
7 | | - foo_attrs = JSON.parse(FOO, symbolize_names: true) |
8 | | - # Pass an array of attribute names to 'skip' to remove them from the 'FOO' |
9 | | - # json before registering foo as a currency. |
10 | | - Array(opts[:skip]).each { |attr| foo_attrs.delete(attr) } |
11 | | - described_class.register(foo_attrs) |
| 19 | + # Pass an array of attribute names to skip to remove them from the registered |
| 20 | + # currency. |
| 21 | + def register_foo(skip: []) |
| 22 | + described_class.register(FOO.except(*skip)) |
12 | 23 | end |
13 | 24 |
|
14 | 25 | def unregister_foo |
15 | | - described_class.unregister(JSON.parse(FOO, symbolize_names: true)) |
| 26 | + described_class.unregister(FOO) |
16 | 27 | end |
17 | 28 |
|
18 | 29 | describe "UnknownCurrency" do |
@@ -45,19 +56,22 @@ def unregister_foo |
45 | 56 | end |
46 | 57 |
|
47 | 58 | describe ".find_by_iso_numeric" do |
| 59 | + let(:mock_class) do |
| 60 | + Class.new do |
| 61 | + def to_s |
| 62 | + "208" |
| 63 | + end |
| 64 | + end |
| 65 | + end |
| 66 | + |
48 | 67 | it "returns currency matching given numeric code" do |
49 | 68 | expect(described_class.find_by_iso_numeric(978)).to eq described_class.new(:eur) |
50 | 69 | expect(described_class.find_by_iso_numeric(208)).not_to eq described_class.new(:eur) |
51 | 70 | expect(described_class.find_by_iso_numeric('840')).to eq described_class.new(:usd) |
52 | 71 | expect(described_class.find_by_iso_numeric(51)).to eq described_class.new(:amd) |
53 | 72 |
|
54 | | - class Mock |
55 | | - def to_s |
56 | | - '208' |
57 | | - end |
58 | | - end |
59 | | - expect(described_class.find_by_iso_numeric(Mock.new)).to eq described_class.new(:dkk) |
60 | | - expect(described_class.find_by_iso_numeric(Mock.new)).not_to eq described_class.new(:usd) |
| 73 | + expect(described_class.find_by_iso_numeric(mock_class.new)).to eq described_class.new(:dkk) |
| 74 | + expect(described_class.find_by_iso_numeric(mock_class.new)).not_to eq described_class.new(:usd) |
61 | 75 | end |
62 | 76 |
|
63 | 77 | it "returns nil if no currency has the given numeric code" do |
@@ -171,9 +185,9 @@ def to_s |
171 | 185 | end |
172 | 186 |
|
173 | 187 | specify ":iso_code must be present" do |
174 | | - expect { |
| 188 | + expect do |
175 | 189 | described_class.register(name: "New Currency") |
176 | | - }.to raise_error(KeyError) |
| 190 | + end.to raise_error(KeyError) |
177 | 191 | end |
178 | 192 | end |
179 | 193 |
|
|
0 commit comments