From 6a637237e74f1c1d3f413e1ea2663e0f130edbe2 Mon Sep 17 00:00:00 2001 From: Douglas Franca Date: Mon, 22 Jan 2024 22:27:47 -0300 Subject: [PATCH] feature: Add support for ETH currency in Money library --- AUTHORS | 1 + config/currency_non_iso.json | 15 +++++++++++++++ spec/currency_spec.rb | 1 + spec/money/formatting_spec.rb | 28 ++++++++++++++++------------ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/AUTHORS b/AUTHORS index e8b6a56cbc..3486118217 100644 --- a/AUTHORS +++ b/AUTHORS @@ -30,6 +30,7 @@ Dave Kroondyk Diego Aguir Selzlein Doug Droper Douglas Miller +Douglas Suptitz Franca Ed Saunders Edwin Vlieg Eloy diff --git a/config/currency_non_iso.json b/config/currency_non_iso.json index 3778e4ed44..9f24bf182c 100644 --- a/config/currency_non_iso.json +++ b/config/currency_non_iso.json @@ -142,5 +142,20 @@ "thousands_separator": ",", "iso_numeric": "", "smallest_denomination": 1 + }, + "eth": { + "priority": 100, + "iso_code": "ETH", + "name": "Ethereum", + "symbol": "Ξ", + "alternate_symbols": [], + "subunit": "Wei", + "subunit_to_unit": 100000000, + "symbol_first": true, + "html_entity": "Ξ", + "decimal_mark": ".", + "thousands_separator": ",", + "iso_numeric": "", + "smallest_denomination": 1 } } diff --git a/spec/currency_spec.rb b/spec/currency_spec.rb index 2e01018bbc..a9eacf8d18 100644 --- a/spec/currency_spec.rb +++ b/spec/currency_spec.rb @@ -336,6 +336,7 @@ def to_s it "returns false if the currency is not iso" do expect(described_class.new(:btc).iso?).to be false + expect(described_class.new(:eth).iso?).to be false end end diff --git a/spec/money/formatting_spec.rb b/spec/money/formatting_spec.rb index 45ce525997..5462973444 100644 --- a/spec/money/formatting_spec.rb +++ b/spec/money/formatting_spec.rb @@ -914,23 +914,27 @@ end describe ":drop_trailing_zeros option" do + currencies = ["BTC", "BCH", "ETH"] + specify "(drop_trailing_zeros: true) works as documented" do - expect(Money.new(89000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089" - expect(Money.new(89000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "0.00089" - expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089" - expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1.00089" - expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: true, symbol: false)).to eq "1" - expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: true, symbol: false)).to eq "1" + amounts = { 89000 => "0.00089", 100089000 => "1.00089", 100000000 => "1" } + + currencies.each do |currency| + amounts.each do |amount, expect| + expect(Money.new(amount, currency).format(drop_trailing_zeros: true, symbol: false)).to eq expect + end + end expect(Money.new(110, "AUD").format(drop_trailing_zeros: true, symbol: false)).to eq "1.1" end specify "(drop_trailing_zeros: false) works as documented" do - expect(Money.new(89000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000" - expect(Money.new(89000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "0.00089000" - expect(Money.new(100089000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000" - expect(Money.new(100089000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00089000" - expect(Money.new(100000000, "BTC").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000" - expect(Money.new(100000000, "BCH").format(drop_trailing_zeros: false, symbol: false)).to eq "1.00000000" + amounts = { 89000 => "0.00089000", 100089000 => "1.00089000", 100000000 => "1.00000000"} + + currencies.each do |currency| + amounts.each do |amount, expect| + expect(Money.new(amount, currency).format(drop_trailing_zeros: false, symbol: false)).to eq expect + end + end expect(Money.new(110, "AUD").format(drop_trailing_zeros: false, symbol: false)).to eq "1.10" end end