From d3d49158703e0ae2e0947fd5ed075fe170d3eeff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gonza=CC=81lez=20Garci=CC=81a?= Date: Wed, 3 Jul 2024 08:11:23 +0200 Subject: [PATCH] fixes #74: add int extensions methods --- src/NMoneys/Extensions/Money.Aliases.cs | 29 ++- src/NMoneys/Extensions/Money.Shortcuts.cs | 275 ++++++++++++++-------- src/NMoneys/Extensions/Money.ToMoney.cs | 136 ++++++----- tests/NMoneys.Tests/MoneyDemo.cs | 6 +- 4 files changed, 296 insertions(+), 150 deletions(-) diff --git a/src/NMoneys/Extensions/Money.Aliases.cs b/src/NMoneys/Extensions/Money.Aliases.cs index ece23c7..9ce063b 100644 --- a/src/NMoneys/Extensions/Money.Aliases.cs +++ b/src/NMoneys/Extensions/Money.Aliases.cs @@ -10,17 +10,40 @@ public static partial class MoneyExtensions /// /// The of the monetary quantity. /// A with the specified and . - public static Money Euros(this decimal amount) { return new Money(amount, Currency.Euro); } + public static Money Euros(this decimal amount) => new(amount, Currency.Euro); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Euros(this int amount) => new(amount, Currency.Euro); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Dollars(this decimal amount) => new(amount, Currency.Dollar); + /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Dollars(this decimal amount) { return new Money(amount, Currency.Dollar); } + public static Money Dollars(this int amount) => new(amount, Currency.Dollar); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Pounds(this decimal amount) => new(amount, Currency.Pound); + /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Pounds(this decimal amount) { return new Money(amount, Currency.Pound); } + public static Money Pounds(this int amount) => new(amount, Currency.Pound); } diff --git a/src/NMoneys/Extensions/Money.Shortcuts.cs b/src/NMoneys/Extensions/Money.Shortcuts.cs index e77ad35..addd04f 100644 --- a/src/NMoneys/Extensions/Money.Shortcuts.cs +++ b/src/NMoneys/Extensions/Money.Shortcuts.cs @@ -5,234 +5,325 @@ namespace NMoneys.Extensions; /// public static partial class MoneyExtensions { + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Aud(this decimal amount) => new Money(amount, Currency.Aud); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Aud(this decimal amount) - { - return new Money(amount, Currency.Aud); - } + public static Money Aud(this int amount) => new Money(amount, Currency.Aud); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Cad(this decimal amount) => new(amount, Currency.Cad); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Cad(this decimal amount) - { - return new Money(amount, Currency.Cad); - } + public static Money Cad(this int amount) => new(amount, Currency.Cad); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Chf(this decimal amount) - { - return new Money(amount, Currency.Chf); - } + public static Money Chf(this decimal amount) => new(amount, Currency.Chf); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Chf(this int amount) => new(amount, Currency.Chf); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Cny(this decimal amount) - { - return new Money(amount, Currency.Cny); - } + public static Money Cny(this decimal amount) => new(amount, Currency.Cny); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Cny(this int amount) => new(amount, Currency.Cny); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Dkk(this decimal amount) - { - return new Money(amount, Currency.Dkk); - } + public static Money Dkk(this decimal amount) => new(amount, Currency.Dkk); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Dkk(this int amount) => new(amount, Currency.Dkk); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Eur(this decimal amount) => new(amount, Currency.Eur); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Eur(this decimal amount) - { - return new Money(amount, Currency.Eur); - } + public static Money Eur(this int amount) => new(amount, Currency.Eur); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Gbp(this decimal amount) - { - return new Money(amount, Currency.Gbp); - } + public static Money Gbp(this decimal amount) => new(amount, Currency.Gbp); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Gbp(this int amount) => new(amount, Currency.Gbp); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Hkd(this decimal amount) - { - return new Money(amount, Currency.Hkd); - } + public static Money Hkd(this decimal amount) => new(amount, Currency.Hkd); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Hkd(this int amount) => new(amount, Currency.Hkd); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Huf(this decimal amount) => new(amount, Currency.Huf); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Huf(this decimal amount) - { - return new Money(amount, Currency.Huf); - } + public static Money Huf(this int amount) => new(amount, Currency.Huf); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Inr(this decimal amount) - { - return new Money(amount, Currency.Inr); - } + public static Money Inr(this decimal amount) => new(amount, Currency.Inr); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Inr(this int amount) => new(amount, Currency.Inr); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Jpy(this decimal amount) - { - return new Money(amount, Currency.Jpy); - } + public static Money Jpy(this decimal amount) => new(amount, Currency.Jpy); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Jpy(this int amount) => new(amount, Currency.Jpy); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Mxn(this decimal amount) - { - return new Money(amount, Currency.Mxn); - } + public static Money Mxn(this decimal amount) => new(amount, Currency.Mxn); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Mxn(this int amount) => new(amount, Currency.Mxn); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Myr(this decimal amount) - { - return new Money(amount, Currency.Myr); - } + public static Money Myr(this decimal amount) => new(amount, Currency.Myr); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Myr(this int amount) => new(amount, Currency.Myr); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Nok(this decimal amount) - { - return new Money(amount, Currency.Nok); - } + public static Money Nok(this decimal amount) => new(amount, Currency.Nok); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Nok(this int amount) => new(amount, Currency.Nok); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Nzd(this decimal amount) => new(amount, Currency.Nzd); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Nzd(this decimal amount) - { - return new Money(amount, Currency.Nzd); - } + public static Money Nzd(this int amount) => new(amount, Currency.Nzd); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Rub(this decimal amount) => new(amount, Currency.Rub); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Rub(this decimal amount) - { - return new Money(amount, Currency.Rub); - } + public static Money Rub(this int amount) => new(amount, Currency.Rub); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Sek(this decimal amount) - { - return new Money(amount, Currency.Sek); - } + public static Money Sek(this decimal amount) => new(amount, Currency.Sek); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Sek(this int amount) => new(amount, Currency.Sek); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Sgd(this decimal amount) => new(amount, Currency.Sgd); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Sgd(this decimal amount) - { - return new Money(amount, Currency.Sgd); - } + public static Money Sgd(this int amount) => new(amount, Currency.Sgd); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Thb(this decimal amount) - { - return new Money(amount, Currency.Thb); - } + public static Money Thb(this decimal amount) => new(amount, Currency.Thb); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Thb(this int amount) => new(amount, Currency.Thb); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Usd(this decimal amount) => new(amount, Currency.Usd); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Usd(this decimal amount) - { - return new Money(amount, Currency.Usd); - } + public static Money Usd(this int amount) => new(amount, Currency.Usd); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Zar(this decimal amount) => new(amount, Currency.Zar); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Zar(this decimal amount) - { - return new Money(amount, Currency.Zar); - } + public static Money Zar(this int amount) => new(amount, Currency.Zar); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Xxx(this decimal amount) - { - return new Money(amount, Currency.Xxx); - } + public static Money Xxx(this decimal amount) => new(amount, Currency.Xxx); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Xxx(this int amount) => new(amount, Currency.Xxx); + + /// + /// Creates an instance with the specified Currency. + /// + /// The of the monetary quantity. + /// A with the specified and . + public static Money Xts(this decimal amount) => new(amount, Currency.Xts); /// /// Creates an instance with the specified Currency. /// /// The of the monetary quantity. /// A with the specified and . - public static Money Xts(this decimal amount) - { - return new Money(amount, Currency.Xts); - } + public static Money Xts(this int amount) => new(amount, Currency.Xts); } diff --git a/src/NMoneys/Extensions/Money.ToMoney.cs b/src/NMoneys/Extensions/Money.ToMoney.cs index 6b70720..a56f4b1 100644 --- a/src/NMoneys/Extensions/Money.ToMoney.cs +++ b/src/NMoneys/Extensions/Money.ToMoney.cs @@ -7,65 +7,93 @@ namespace NMoneys.Extensions; /// public static partial class MoneyExtensions { + ///Creates a instance with the specified amount and Currency. + /// The of the monetary quantity. + /// The of the monetary quantity. + /// A with the specified and . + /// + public static Money ToMoney(this CurrencyIsoCode currency, decimal amount) => new(amount, currency); + + ///Creates a instance with the specified amount and Currency. + /// The of the monetary quantity. + /// The of the monetary quantity. + /// A with the specified and . + /// + public static Money ToMoney(this CurrencyIsoCode currency, int amount) => new(amount, currency); + + ///Creates a instance with the specified amount and unspecified Currency. + /// The of the monetary quantity. + /// A with the specified and unspecified currency (). + /// + public static Money ToMoney(this decimal amount) => new(amount); + + ///Creates a instance with the specified amount and unspecified Currency. + /// The of the monetary quantity. + /// A with the specified and unspecified currency (). + /// + public static Money ToMoney(this int amount) => new(amount); ///Creates a instance with the specified amount and Currency. - /// The of the monetary quantity. - /// The of the monetary quantity. - /// A with the specified and . - /// - public static Money ToMoney(this CurrencyIsoCode currency, decimal amount) - { - return new Money(amount, currency); - } + /// The of the monetary quantity. + /// The of the monetary quantity. + /// A with the specified and . + /// + public static Money ToMoney(this decimal amount, CurrencyIsoCode currency) => new(amount, currency); - ///Creates a instance with the specified amount and unspecified Currency. - /// The of the monetary quantity. - /// A with the specified and unspecified currency (). - /// - public static Money ToMoney(this decimal amount) - { - return new Money(amount); - } + ///Creates a instance with the specified amount and Currency. + /// The of the monetary quantity. + /// The of the monetary quantity. + /// A with the specified and . + /// + public static Money ToMoney(this int amount, CurrencyIsoCode currency) => new(amount, currency); + + ///Creates a instance with the specified amount and Currency. + /// The of the monetary quantity. + /// The incarnation of the . + /// A with the specified and . + /// + public static Money ToMoney(this decimal amount, Currency currency) => new(amount, currency); + + ///Creates a instance with the specified amount and Currency. + /// The of the monetary quantity. + /// The incarnation of the . + /// A with the specified and . + /// + public static Money ToMoney(this int amount, Currency currency) => new(amount, currency); - ///Creates a instance with the specified amount and Currency. - /// The of the monetary quantity. - /// The of the monetary quantity. - /// A with the specified and . - /// - public static Money ToMoney(this decimal amount, CurrencyIsoCode currency) - { - return new Money(amount, currency); - } + ///Creates an array of instances all with the specified currency and the corresponding amount. + /// Each of the of the monetary quantity. + /// The of the monetary quantity. + /// An array of with the same length as and each member with the specified + /// amount and . + /// + public static Money[] ToMoney(this IEnumerable amounts, CurrencyIsoCode currency) => + amounts.Select(x => x.ToMoney(currency)).ToArray(); - ///Creates a instance with the specified amount and Currency. - /// The of the monetary quantity. - /// The incarnation of the . - /// A with the specified and . - /// - public static Money ToMoney(this decimal amount, Currency currency) - { - return new Money(amount, currency); - } + ///Creates an array of instances all with the specified currency and the corresponding amount. + /// Each of the of the monetary quantity. + /// The of the monetary quantity. + /// An array of with the same length as and each member with the specified + /// amount and . + /// + public static Money[] ToMoney(this IEnumerable amounts, CurrencyIsoCode currency) => + amounts.Select(x => x.ToMoney(currency)).ToArray(); - ///Creates an array of instances all with the specified currency and the corresponding amount. - /// Each of the of the monetary quantity. - /// The of the monetary quantity. - /// An array of with the same length as and each member with the specified - /// amount and . - /// - public static Money[] ToMoney(this IEnumerable amounts, CurrencyIsoCode currency) - { - return amounts.Select(x => x.ToMoney(currency)).ToArray(); - } + ///Creates an array of instances all with the specified currency and the corresponding amount. + /// Each of the of the monetary quantity. + /// The incarnation of the . + /// An array of with the same length as and each member with the specified + /// amount and . + /// + public static Money[] ToMoney(this IEnumerable amounts, Currency currency) => + ToMoney(amounts, Ensuring.NotNull(nameof(currency), currency, c => c.IsoCode)); - ///Creates an array of instances all with the specified currency and the corresponding amount. - /// Each of the of the monetary quantity. - /// The incarnation of the . - /// An array of with the same length as and each member with the specified - /// amount and . - /// - public static Money[] ToMoney(this IEnumerable amounts, Currency currency) - { - return ToMoney(amounts, Ensuring.NotNull(nameof(currency), currency, c => c.IsoCode)); - } + ///Creates an array of instances all with the specified currency and the corresponding amount. + /// Each of the of the monetary quantity. + /// The incarnation of the . + /// An array of with the same length as and each member with the specified + /// amount and . + /// + public static Money[] ToMoney(this IEnumerable amounts, Currency currency) => + ToMoney(amounts, Ensuring.NotNull(nameof(currency), currency, c => c.IsoCode)); } diff --git a/tests/NMoneys.Tests/MoneyDemo.cs b/tests/NMoneys.Tests/MoneyDemo.cs index 0fbc47b..78a7422 100644 --- a/tests/NMoneys.Tests/MoneyDemo.cs +++ b/tests/NMoneys.Tests/MoneyDemo.cs @@ -44,6 +44,10 @@ public void moneys_can_be_quickly_created_for_testing_scenarios_with_extension_m 3.5m.ToMoney(Currency.Aud); 3.5m.ToMoney(CurrencyIsoCode.AUD); CurrencyIsoCode.AUD.ToMoney(3.5m); + + // also with integers + 3.Xts(); + 3.ToMoney(); } [Test] @@ -64,7 +68,7 @@ public void what_is_in_a_money() [Test] [SuppressMessage("ReSharper", "ReturnValueOfPureMethodIsNotUsed")] [SuppressMessage("Performance", "CA1806:Do not ignore method results")] - public void when_you_money_worths_nothing() + public void when_your_money_worths_nothing() { Money.Zero(); Money.Zero(Currency.Usd);