Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
remyvd committed Nov 5, 2015
2 parents ebe6c1b + 9423aa6 commit 2e1868d
Show file tree
Hide file tree
Showing 28 changed files with 1,135 additions and 381 deletions.
2 changes: 2 additions & 0 deletions CodeAnalysisDictionary.xml → CustomDictionary.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<Word>iraimbilanja</Word>
<Word>cinquième</Word>
<Word>ariary</Word>
<Word>Ldml</Word>
</Recognized>
<Deprecated>
<Term PreferredAlternate="EnterpriseServices">ComPlus</Term>
Expand All @@ -23,6 +24,7 @@
<Acronyms>
<CasingExceptions>
<Acronym>USDollar</Acronym>
<Acronym>ISO</Acronym>
</CasingExceptions>
</Acronyms>
</Dictionary>
1 change: 0 additions & 1 deletion NextVersion.txt

This file was deleted.

9 changes: 3 additions & 6 deletions NodaMoney.Serialization.AspNet/MoneyJavaScriptConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@ namespace NodaMoney.Serialization.AspNet
public class MoneyJavaScriptConverter : JavaScriptConverter
{
/// <summary>When overridden in a derived class, gets a collection of the supported types.</summary>
public override IEnumerable<Type> SupportedTypes
{
get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(Money) })); }
}
public override IEnumerable<Type> SupportedTypes => new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(Money) }));

/// <summary>When overridden in a derived class, converts the provided dictionary into an object of the specified type.</summary>
/// <param name="dictionary">An <see cref="T:System.Collections.Generic.IDictionary`2" /> instance of property data stored as name/value pairs.</param>
Expand All @@ -30,9 +27,9 @@ public override IEnumerable<Type> SupportedTypes
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
throw new ArgumentNullException(nameof(dictionary));
if (type != typeof(Money))
throw new ArgumentException("object should be of type Money to deserialize!", "type");
throw new ArgumentException("object should be of type Money to deserialize!", nameof(type));

decimal amount = decimal.Parse((string)dictionary["amount"], CultureInfo.InvariantCulture);
string code = (string)dictionary["currency"];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>NodaMoney.Serialization.AspNet</RootNamespace>
<AssemblyName>NodaMoney.Serialization.AspNet</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -46,17 +47,17 @@
<Compile Include="MoneyJavaScriptConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CodeAnalysisDictionary.xml">
<Link>Properties\CodeAnalysisDictionary.xml</Link>
</CodeAnalysisDictionary>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\NodaMoney\NodaMoney.csproj">
<Project>{1814E8DE-0E75-4E23-9F70-E039AC816B42}</Project>
<Name>NodaMoney</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<CodeAnalysisDictionary Include="..\CustomDictionary.xml">
<Link>Properties\CustomDictionary.xml</Link>
</CodeAnalysisDictionary>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
152 changes: 152 additions & 0 deletions NodaMoney.UnitTests/CurrencyBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
using System;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace NodaMoney.UnitTests
{
public class CurrencyBuilderTests
{
[TestClass]
public class GivenIWantToCreateCustomCurrency
{
[TestMethod]
public void WhenRegisterBitCoinInIsoNamespace_ThenShouldBeAvailable()
{
var builder = new CurrencyBuilder("BTC", "ISO-4217");
builder.EnglishName = "Bitcoin";
builder.Symbol = "฿";
builder.ISONumber = "123"; // iso number
builder.DecimalDigits = 8;
builder.IsObsolete = false;

Currency result = builder.Register();

Currency bitcoin = Currency.FromCode("BTC");
bitcoin.Symbol.Should().Be("฿");
bitcoin.ShouldBeEquivalentTo(result);
}

[TestMethod]
public void WhenRegisterBitCoin_ThenShouldBeAvailableByExplicitNamespace()
{
var builder = new CurrencyBuilder("BTC1", "virtual");
builder.EnglishName = "Bitcoin";
builder.Symbol = "฿";
builder.ISONumber = "123"; // iso number
builder.DecimalDigits = 8;
builder.IsObsolete = false;

Currency result = builder.Register();

Currency bitcoin = Currency.FromCode("BTC1", "virtual");
bitcoin.Symbol.Should().Be("฿");
bitcoin.ShouldBeEquivalentTo(result);
}

[TestMethod]
public void WhenBuildBitCoin_ThenItShouldSuccedButNotBeRegistered()
{
var builder = new CurrencyBuilder("BTC2", "virtual");
builder.EnglishName = "Bitcoin";
builder.Symbol = "฿";
builder.ISONumber = "123"; // iso number
builder.DecimalDigits = 8;
builder.IsObsolete = false;

Currency result = builder.Build();
result.Symbol.Should().Be("฿");

Action action = () => Currency.FromCode("BTC2", "virtual");
action.ShouldThrow<ArgumentException>().WithMessage("BTC2 is an unknown virtual currency code!");
}

[TestMethod]
public void WhenFromExistingCurrency_ThenThisShouldSucceed()
{
var builder = new CurrencyBuilder("BTC3", "virtual");

var euro = Currency.FromCode("EUR");
builder.LoadDataFromCurrency(euro);

builder.Code.Should().Be("BTC3");
builder.Namespace.Should().Be("virtual");
builder.EnglishName.Should().Be(euro.EnglishName);
builder.Symbol.Should().Be(euro.Symbol);
builder.ISONumber.Should().Be(euro.Number);
builder.DecimalDigits.Should().Be(euro.DecimalDigits);
builder.IsObsolete.Should().Be(euro.IsObsolete);
builder.ValidFrom.Should().Be(euro.ValidFrom);
builder.ValidTo.Should().Be(euro.ValidTo);
}
}

[TestClass]
public class GivenIWantToUnregisterCurrency
{
[TestMethod]
public void WhenUnregisterIsoCurrency_ThenThisMustSucceed()
{
var euro = Currency.FromCode("EUR"); // should work

CurrencyBuilder.Unregister("EUR", "ISO-4217");
Action action = () => Currency.FromCode("EUR");

action.ShouldThrow<ArgumentException>().WithMessage("*unknown*currency*");

// register again for other unit-tests
var builder = new CurrencyBuilder("EUR", "ISO-4217");
builder.LoadDataFromCurrency(euro);
builder.Register();
}

[TestMethod]
public void WhenUnregisterCustomCurrency_ThenThisMustSucceed()
{
var builder = new CurrencyBuilder("XYZ", "virtual");
builder.EnglishName = "Xyz";
builder.Symbol = "฿";
builder.ISONumber = "123"; // iso number
builder.DecimalDigits = 4;
builder.IsObsolete = false;

builder.Register();
Currency xyz = Currency.FromCode("XYZ", "virtual"); // should work

CurrencyBuilder.Unregister("XYZ", "virtual");
Action action = () => Currency.FromCode("XYZ", "virtual");

action.ShouldThrow<ArgumentException>().WithMessage("*unknown*currency*");
}

[TestMethod]
public void WhenCurrencyDoesntExist_ThenThisShouldThrow()
{
Action action = () => CurrencyBuilder.Unregister("ABC", "virtual");

action.ShouldThrow<ArgumentException>().WithMessage("*specifies a currency that is not found*");
}
}

[TestClass]
public class GivenIWantToReplaceIsoCurrencyWithOwnVersion
{
[TestMethod]
public void WhenReplacingEuroWithCustom_ThenThisShouldSucceed()
{
Currency oldEuro = CurrencyBuilder.Unregister("EUR", "ISO-4217");

var builder = new CurrencyBuilder("EUR", "ISO-4217");
builder.LoadDataFromCurrency(oldEuro);
builder.EnglishName = "New Euro";
builder.DecimalDigits = 1;

builder.Register();

Currency newEuro = Currency.FromCode("EUR");
newEuro.Symbol.Should().Be("€");
newEuro.EnglishName.Should().Be("New Euro");
newEuro.DecimalDigits.Should().Be(1);
}
}
}
}
57 changes: 46 additions & 11 deletions NodaMoney.UnitTests/CurrencyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void WhenAskingForIt_ThenAllCurrenciesShouldBeReturned()
var currencies = Currency.GetAllCurrencies();

currencies.Should().NotBeEmpty();
currencies.Length.Should().BeGreaterThan(100);
currencies.Count().Should().BeGreaterThan(100);
}

[TestMethod][Ignore]
Expand Down Expand Up @@ -56,7 +56,7 @@ public void WriteAllCurrenciesToFile()
foreach (var currency in Currency.GetAllCurrencies())
{
writer.WriteLine("EnglishName: {0}", currency.EnglishName);
writer.WriteLine("Code: {0}, Number: {1}, Sign: {2}", currency.Code, currency.Number, currency.Sign);
writer.WriteLine("Code: {0}, Number: {1}, Sign: {2}", currency.Code, currency.Number, currency.Symbol);
writer.WriteLine("MajorUnit: {0}, MinorUnit: {1}, DecimalDigits: {2}", currency.MajorUnit, currency.MinorUnit, currency.DecimalDigits);
writer.WriteLine(string.Empty);
}
Expand Down Expand Up @@ -96,9 +96,10 @@ public void WhenIsoCodeIsExisting_ThenCreatingShouldSucceed()
var currency = Currency.FromCode("EUR");

currency.Should().NotBeNull();
currency.Sign.Should().Be("€");
currency.Symbol.Should().Be("€");
currency.Code.Should().Be("EUR");
currency.EnglishName.Should().Be("Euro");
currency.IsObsolete.Should().BeFalse();
}

[TestMethod]
Expand All @@ -116,6 +117,17 @@ public void WhenIsoCodeIsNull_ThenCreatingShouldThrow()

action.ShouldThrow<ArgumentNullException>();
}

[TestMethod]
public void WhenEstionianKrone_ThenItShouldBeObsolete()
{
var currency = Currency.FromCode("EEK");

currency.Should().NotBeNull();
currency.Symbol.Should().Be("kr");
currency.IsObsolete.Should().BeTrue();
}

}

[TestClass]
Expand All @@ -127,7 +139,7 @@ public void WhenUsingRegionInfo_ThenCreatingShouldSucceed()
var currency = Currency.FromRegion(new RegionInfo("NL"));

currency.Should().NotBeNull();
currency.Sign.Should().Be("€");
currency.Symbol.Should().Be("€");
currency.Code.Should().Be("EUR");
currency.EnglishName.Should().Be("Euro");
}
Expand All @@ -146,7 +158,7 @@ public void WhenUsingRegionName_ThenCreatingShouldSucceed()
var currency = Currency.FromRegion("NL");

currency.Should().NotBeNull();
currency.Sign.Should().Be("€");
currency.Symbol.Should().Be("€");
currency.Code.Should().Be("EUR");
currency.EnglishName.Should().Be("Euro");
}
Expand All @@ -165,7 +177,7 @@ public void WhenUsingCultureInfo_ThenCreatingShouldSucceed()
var currency = Currency.FromCulture(CultureInfo.CreateSpecificCulture("nl-NL"));

currency.Should().NotBeNull();
currency.Sign.Should().Be("€");
currency.Symbol.Should().Be("€");
currency.Code.Should().Be("EUR");
currency.EnglishName.Should().Be("Euro");
}
Expand All @@ -192,7 +204,7 @@ public void WhenUsingCultureName_ThenCreatingShouldSucceed()
var currency = Currency.FromRegion("nl-NL");

currency.Should().NotBeNull();
currency.Sign.Should().Be("€");
currency.Symbol.Should().Be("€");
currency.Code.Should().Be("EUR");
currency.EnglishName.Should().Be("Euro");
}
Expand Down Expand Up @@ -313,7 +325,7 @@ public void WhenParamsAreCorrect_ThenCreatingShouldSucceed()
eur.Number.Should().Be("978");
eur.DecimalDigits.Should().Be(2);
eur.EnglishName.Should().Be("Euro");
eur.Sign.Should().Be("€");
eur.Symbol.Should().Be("€");
}

[TestMethod]
Expand Down Expand Up @@ -355,13 +367,36 @@ public void WhenDecimalDigitIsLowerThenMinusOne_ThenCreatingShouldThrow()

action.ShouldThrow<ArgumentOutOfRangeException>();
}
}

[TestClass]
public class GiveIWantToUseALotOfCurrencies
{
[TestMethod]
public void WhenDecimalDigitIsHigherThenFour_ThenCreatingShouldThrow()
public void WhenCreatingOneMillion_ThenItShouldBeWithinTwoSeconds()
{
Action action = () => { var eur = new Currency("EUR", "978", 5, "Euro", "€"); };
var sw = Stopwatch.StartNew();
var c = Currency.FromCode("EUR");
Console.WriteLine("{0} ms for first call.", sw.ElapsedMilliseconds);

action.ShouldThrow<ArgumentOutOfRangeException>();
double max = 1000000;
Action action = () =>
{
sw.Restart();
for (int i = 0; i < max; i++)
{
if (i % 3 == 0)
c = Currency.FromCode("EUR");
else if (i % 2 == 0)
c = Currency.FromCode("USD");
else
c = Currency.FromCode("JPY");
}
sw.Stop();
};

action.ExecutionTime().ShouldNotExceed(2.Seconds());
Console.WriteLine("{0} ms for creating {1:N0} currencies (avg {2:F5} ms).", sw.ElapsedMilliseconds, max, sw.ElapsedMilliseconds / (max));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
using System.Linq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NodaMoney.Extensions;

namespace NodaMoney.Extensions.UnitTests
namespace NodaMoney.UnitTests.Extensions
{
public class MoneyExtensionsSafeDivideTests
{
Expand Down
Loading

0 comments on commit 2e1868d

Please sign in to comment.