Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 24 additions & 14 deletions Tests/NFUnitTestArithmetic/UnitTestFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,44 @@ public enum Case
}

[TestMethod]
public void StringFormat()
public void StringFormat_00()
{
// Test a null string in String.Format - should be treated like an empty string.
string nullArg = null;
Assert.Equal(string.Format("Value is {0}", nullArg), "Value is ", "");
}

[TestMethod]
public void StringFormat_01()
{
// catch an exception if the format string is null
string nullFormat = null;
Assert.Throws(typeof(NullReferenceException), () => { string.Format(nullFormat, 12345.67); });
}

[TestMethod]
[DataRow("Left align in 10 chars: {0,-10:N2}: and then more", 1234.5641, "Left align in 10 chars: 1,234.56 : and then more")]
[DataRow("Right align in 10 chars: {0,10:N2}: and then more", 1234.5641, "Right align in 10 chars: 1,234.56: and then more")]
public void StringFormat_02(string formatString, double value, string outcomeMessage)
{
// Test alignment operator which is the "," and a number. Negative is right aligned, positive left aligned
Assert.Equal(string.Format(formatString, value), outcomeMessage);
}

Assert.Equal(string.Format("Left align in 10 chars: {0,-10:N2}: and then more", 1234.5641), "Left align in 10 chars: 1,234.56 : and then more");
Assert.Equal(string.Format("Right align in 10 chars: {0,10:N2}: and then more", 1234.5641), "Right align in 10 chars: 1,234.56: and then more");

[TestMethod]
[DataRow("{0,}", 12345.67, "Should throw with error message: Format error: empty alignment, column 3")]
[DataRow("{0,a10}", 12345.67, "Should throw with error message: Format error: wrong symbol at alignment, column 3")]
[DataRow("{0, -a10}", 12345.67, "Should throw with error message: Format error: wrong symbol at alignment, column 4")]
[DataRow("{a}", 12345.67, "Should throw with error message: Format error: wrong symbol at {}, column 1")]
[DataRow("{0:}", 12345.67, "Should throw with error message: Format error: empty format after ':', column 3")]
[DataRow("{0", 12345.67, "Should throw with error message: Format error: no closed brace, column 2")]
public void StringFormat_03(string formatString, double value, string outcomeMessage)
{
OutputHelper.WriteLine("formatString is" + formatString);
// invalid alignment cases

Assert.Throws(typeof(ArgumentException), () => { string.Format("{0,}", 12345.67); }, "Should throw with error message: Format error: empty alignment, column 3");
Assert.Throws(typeof(ArgumentException), () => { string.Format("{0,a10}", 12345.67); }, "Should throw with error message: Format error: wrong symbol at alignment, column 3");
Assert.Throws(typeof(ArgumentException), () => { string.Format("{0,-a10}", 12345.67); }, "Should throw with error message: Format error: wrong symbol at alignment, column 4");
Assert.Throws(typeof(ArgumentException), () => { string.Format("{a}", 12345.67); }, "Should throw with error message: Format error: wrong symbol at {}, column 1");
Assert.Throws(typeof(ArgumentException), () => { string.Format("{0:}", 12345.67); }, "Should throw with error message: Format error: empty format after ':', column 3");
Assert.Throws(typeof(ArgumentException), () => { string.Format("{0", 12345.67); }, "Should throw with error message: Format error: no closed brace, column 2");

Assert.Throws(typeof(ArgumentException), () => { string.Format(formatString, value); }, outcomeMessage);
}



[TestMethod]
// the D format can only be used with integers (no double or floats)
public void DecimalFormat()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@
</PropertyGroup>
<Warning Condition="!Exists('..\..\packages\nanoFramework.TestFramework.2.0.43\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
</Target>
</Project>
</Project>
2 changes: 1 addition & 1 deletion Tests/NFUnitTest_DummyAdapter/packages.config
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
<packages>
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnano1.0" />
<package id="nanoFramework.TestFramework" version="2.0.43" targetFramework="netnano1.0" developmentDependency="true" />
</packages>
</packages>