mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-15 20:54:58 +00:00
feat: Adding TryFromIpIdString method
This commit is contained in:
@@ -61,10 +61,10 @@ namespace ICD.Common.Utils.Tests
|
|||||||
Assert.AreEqual("[-3 - 5]", StringUtils.RangeFormat(-3, 5));
|
Assert.AreEqual("[-3 - 5]", StringUtils.RangeFormat(-3, 5));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[TestCase("foobar", "Foobar")]
|
||||||
public void UppercaseFirstTest()
|
public void UppercaseFirstTest(string value, string expected)
|
||||||
{
|
{
|
||||||
Assert.AreEqual("Foobar", StringUtils.UppercaseFirst("foobar"));
|
Assert.AreEqual(expected, StringUtils.UppercaseFirst(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCase("test", "Test")]
|
[TestCase("test", "Test")]
|
||||||
@@ -74,16 +74,24 @@ namespace ICD.Common.Utils.Tests
|
|||||||
Assert.AreEqual(expected, StringUtils.ToTitleCase(input));
|
Assert.AreEqual(expected, StringUtils.ToTitleCase(input));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[TestCase((byte)0x67, "0x67")]
|
||||||
public void ToIpIdStringTest()
|
public void ToIpIdStringTest(byte ipid, string expected)
|
||||||
{
|
{
|
||||||
Assert.AreEqual("0x67", StringUtils.ToIpIdString(0x67));
|
Assert.AreEqual(expected, StringUtils.ToIpIdString(ipid));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test, UsedImplicitly]
|
[TestCase("0x67", (byte)0x67)]
|
||||||
public void FromIpIdStringTest()
|
public void FromIpIdStringTest(string value, byte expected)
|
||||||
{
|
{
|
||||||
Assert.AreEqual(0x67, StringUtils.FromIpIdString("0x67"));
|
Assert.AreEqual(expected, StringUtils.FromIpIdString(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase("0x67", true, (byte)0x67)]
|
||||||
|
public void TryFromIpIdStringTest(string value, bool expected, byte expectedOutput)
|
||||||
|
{
|
||||||
|
byte output;
|
||||||
|
Assert.AreEqual(expected, StringUtils.TryFromIpIdString(value, out output));
|
||||||
|
Assert.AreEqual(expectedOutput, output);
|
||||||
}
|
}
|
||||||
|
|
||||||
[UsedImplicitly]
|
[UsedImplicitly]
|
||||||
|
|||||||
@@ -564,6 +564,30 @@ namespace ICD.Common.Utils
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Formats "0xFF" to an IPID.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="result"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool TryFromIpIdString(string value, out byte result)
|
||||||
|
{
|
||||||
|
if (value == null)
|
||||||
|
throw new ArgumentNullException("value");
|
||||||
|
|
||||||
|
result = 0;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
result = (byte)Convert.ToInt64(value, 16);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (ArgumentOutOfRangeException)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all whitespace from the string.
|
/// Removes all whitespace from the string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user