Skip to content

Commit

Permalink
Conversion tests for mass units.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Jun 26, 2016
1 parent b084923 commit 68a3b9c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Gu.Units.Tests/Gu.Units.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="Benchmarks\FormattingBenchmarks.cs" />
<Compile Include="Benchmarks\ParsingBenchmarks.cs" />
<Compile Include="Benchmarks\SubstringCacheBenchmarks.cs" />
<Compile Include="TestHelpers\ConversionData.cs" />
<Compile Include="ConversionTests.cs" />
<Compile Include="FormatTests.cs" />
<Compile Include="Constants\Benchmarks.cs" />
Expand Down Expand Up @@ -78,6 +79,7 @@
<Compile Include="LengthTypeConverterTests.cs" />
<Compile Include="LengthUnitTests.cs" />
<Compile Include="LengthUnitTypeConverterTests.cs" />
<Compile Include="MassTests.cs" />
<Compile Include="Samples\Samples.cs" />
<Compile Include="Sandbox\DumpMarkdown.cs" />
<Compile Include="Sources\ConversionProvider.cs" />
Expand Down
33 changes: 33 additions & 0 deletions Gu.Units.Tests/MassTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Gu.Units.Tests
{
using System.Collections.Generic;
using System.Globalization;

using NUnit.Framework;

public class MassTests
{
public static IReadOnlyList<ConversionData> ConversionSource { get; } = new[]
{
new ConversionData("28.349523125 g", "1 oz"),
new ConversionData("31.1034768 g", "1 troy", false),
new ConversionData("1 lb", "0.45359237 kg"),
new ConversionData("1 gr", "64.79891 mg", false),
};

[TestCaseSource(nameof(ConversionSource))]
public void Conversion(ConversionData data)
{
var m1 = Mass.Parse(data.From, CultureInfo.InvariantCulture);
var m2 = Mass.Parse(data.To, CultureInfo.InvariantCulture);
if (data.Exactly)
{
Assert.AreEqual(m1, m2);
}
else
{
Assert.AreEqual(m1.ToString(), m2.ToString());
}
}
}
}
29 changes: 29 additions & 0 deletions Gu.Units.Tests/TestHelpers/ConversionData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Gu.Units.Tests
{
public class ConversionData
{
public ConversionData(string @from,
string to,
bool exactly = true)
{
this.From = @from;
this.To = to;
this.Exactly = exactly;
}

public string From { get; }

public string To { get; }

public bool Exactly { get; }


public override string ToString()
{
var equalitySymbol = this.Exactly
? "=="
: "~";
return $"{this.From} {equalitySymbol} {this.To}";
}
}
}

0 comments on commit 68a3b9c

Please sign in to comment.