Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Scalar quantity #889

Merged
merged 2 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Common/UnitDefinitions/Scalar.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"Name": "Scalar",
"BaseUnit": "Amount",
"XmlDoc": "A way of representing a number of items.",
"Units": [
{
"SingularName": "Amount",
angularsen marked this conversation as resolved.
Show resolved Hide resolved
"PluralName": "Amount",
"FromUnitToBaseFunc": "x",
"FromBaseToUnitFunc": "x",
"Localization": [
{
"Culture": "en-US",
"Abbreviations": [ "" ]
}
]
}
]
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 68 additions & 0 deletions UnitsNet.Tests/CustomCode/ScalarTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by \generate-code.bat.
//
// Changes to this file will be lost when the code is regenerated.
// The build server regenerates the code before each build and a pre-build
// step will regenerate the code on each local build.
//
// See https://github.com/angularsen/UnitsNet/wiki/Adding-a-New-Unit for how to add or edit units.
//
// Add CustomCode\Quantities\MyQuantity.extra.cs files to add code to generated quantities.
// Add UnitDefinitions\MyQuantity.json and run generate-code.bat to generate new units or quantities.
//
// </auto-generated>
//------------------------------------------------------------------------------

// Licensed under MIT No Attribution, see LICENSE file at the root.
// Copyright 2013 Andreas Gullberg Larsen (andreas.larsen84@gmail.com). Maintained at https://github.com/angularsen/UnitsNet.

using System;
using Xunit;

namespace UnitsNet.Tests.CustomCode
{
public class ScalarTests : ScalarTestsBase
{
// Override properties in base class here
protected override double AmountInOneAmount => 1;
protected override bool SupportsSIUnitSystem => false;

/// <summary>
/// Purposely did not call it an Int as it is not an int underneath.
/// </summary>
[Fact]
public void ScalarWholeNumberEqualsScalarWholeNumber()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love tests, but I believe these test cases are already covered by the generated tests:

  • ArithmeticOperators
  • ConversionRoundTrip

https://github.com/angularsen/UnitsNet/pull/889/files#diff-136cd525eae5af5b9f86b098cdab2bc0741d8119d188b00e60524aada8d407a5R184-R202

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not see those, the one I did see was
Assertion(3, ScalarUnit.Amount, Quantity.From(3, ScalarUnit.Amount)); from IQuantityTests.g.cs

Looking from someone not familiar with the codebase one thing that I will add about the tests that you listed (ArithmeticOperators & ConversionRoundTrip) - they only use integers to test. Looking at the code underneath, it will not make a difference as they are stored as a double anyway, but if that implementation ever changes there may not be sufficient test coverage. I think this is outside of the scope of this PR, so I will remove those tests.

{
int i = 1;
var scalar = Scalar.FromAmount(i);
Assert.Equal(i, scalar.Amount);
var scalar2 = Scalar.FromAmount(1000000000);
Assert.Equal(1000000000, scalar2.Amount);
}

[Fact]
public void ScalarDoubleEqualsScalarDouble()
{
var scalar = Scalar.FromAmount(1.123456789);
IQuantity quantity = scalar;
Assert.Equal(1.123456789, scalar.Amount);
var scalar2 = Scalar.FromAmount(0.0000000012345);
Assert.Equal(0.0000000012345, scalar2.Amount);
}



[Fact]
public void ScalarMathsAddition()
{
var scalar = Scalar.FromAmount(10.111111111);
var scalar2 = Scalar.FromAmount(10.111111111);
var scalar3 = scalar + scalar2;
Assert.Equal(20.222222222, scalar3.Amount);
}



}
}
4 changes: 4 additions & 0 deletions UnitsNet.Tests/GeneratedCode/IQuantityTests.g.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading