250 lines
5.1 KiB
C#
250 lines
5.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
namespace AMWD.Common.Tests.Extensions
|
|
{
|
|
[TestClass]
|
|
public class StringExtensionsTests
|
|
{
|
|
[TestMethod]
|
|
public void ShouldReturnEmptyList()
|
|
{
|
|
// arrange
|
|
string hex = "";
|
|
|
|
// act
|
|
var bytes = hex.HexToBytes();
|
|
|
|
// assert
|
|
Assert.IsNotNull(bytes);
|
|
Assert.IsFalse(bytes.Any());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnEmptyListWhenInvalid()
|
|
{
|
|
// arrange
|
|
string hex1 = "aff";
|
|
string hex2 = "de:ad:be:e";
|
|
string hex3 = "hell";
|
|
|
|
// act
|
|
var bytes1 = hex1.HexToBytes();
|
|
var bytes2 = hex2.HexToBytes(":");
|
|
var bytes3 = hex3.HexToBytes();
|
|
|
|
// assert
|
|
Assert.IsNotNull(bytes1);
|
|
Assert.IsFalse(bytes1.Any());
|
|
|
|
Assert.IsNotNull(bytes2);
|
|
Assert.IsFalse(bytes2.Any());
|
|
|
|
Assert.IsNotNull(bytes3);
|
|
Assert.IsFalse(bytes3.Any());
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldConvertHexToBytes()
|
|
{
|
|
// arrange
|
|
string hex = "deadbeef";
|
|
|
|
// act
|
|
var bytes = hex.HexToBytes();
|
|
|
|
// assert
|
|
Assert.IsNotNull(bytes);
|
|
Assert.AreEqual(4, bytes.Count());
|
|
Assert.AreEqual(0xde, bytes.ElementAt(0));
|
|
Assert.AreEqual(0xad, bytes.ElementAt(1));
|
|
Assert.AreEqual(0xbe, bytes.ElementAt(2));
|
|
Assert.AreEqual(0xef, bytes.ElementAt(3));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldConvertHexToBytesWithDelimiter()
|
|
{
|
|
// arrange
|
|
string hex = "af:fe";
|
|
|
|
// act
|
|
var bytes = hex.HexToBytes(":");
|
|
|
|
// assert
|
|
Assert.IsNotNull(bytes);
|
|
Assert.AreEqual(2, bytes.Count());
|
|
Assert.AreEqual(0xaf, bytes.ElementAt(0));
|
|
Assert.AreEqual(0xfe, bytes.ElementAt(1));
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnNullWhenInvalid()
|
|
{
|
|
// arrange
|
|
byte[] bytes1 = null;
|
|
byte[] bytes2 = Array.Empty<byte>();
|
|
|
|
// act
|
|
string hex1 = bytes1.BytesToHex();
|
|
string hex2 = bytes2.BytesToHex();
|
|
|
|
// assert
|
|
Assert.IsNull(hex1);
|
|
Assert.IsNull(hex2);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnHexString()
|
|
{
|
|
// arrange
|
|
byte[] bytes = new byte[] { 0xaf, 0xfe };
|
|
|
|
// act
|
|
string hex = bytes.BytesToHex();
|
|
|
|
// assert
|
|
Assert.IsNotNull(hex);
|
|
Assert.AreEqual("affe", hex);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReturnHexStringWithDelimiter()
|
|
{
|
|
// arrange
|
|
byte[] bytes = new byte[] { 0xde, 0xad, 0xbe, 0xef };
|
|
|
|
// act
|
|
string hex = bytes.BytesToHex("_");
|
|
|
|
// assert
|
|
Assert.IsNotNull(hex);
|
|
Assert.AreEqual("de_ad_be_ef", hex);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldEncodeStringToHex()
|
|
{
|
|
// arrange
|
|
string plain = "Hello";
|
|
|
|
// act
|
|
string hex = plain.HexEncode(Encoding.UTF8);
|
|
|
|
// assert
|
|
Assert.AreEqual("48656c6c6f", hex);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldDecodeStringFromHex()
|
|
{
|
|
// arrange
|
|
string hex = "48656c6c6f";
|
|
|
|
// act
|
|
string plain = hex.HexDecode(Encoding.UTF8);
|
|
|
|
// assert
|
|
Assert.AreEqual("Hello", plain);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldEncodeStringToBase64()
|
|
{
|
|
// arrange
|
|
string plain = "Hello";
|
|
|
|
// act
|
|
string base64 = plain.Base64Encode(Encoding.UTF8);
|
|
|
|
// assert
|
|
Assert.AreEqual("SGVsbG8=", base64);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldDecodeStringFromBase64()
|
|
{
|
|
// arrange
|
|
string base64 = "SGVsbG8=";
|
|
|
|
// act
|
|
string plain = base64.Base64Decode(Encoding.UTF8);
|
|
|
|
// assert
|
|
Assert.AreEqual("Hello", plain);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReplaceStart()
|
|
{
|
|
// arrange
|
|
string str = "Hello World!";
|
|
|
|
// act
|
|
string test1 = str.ReplaceStart("Hello", "Bye");
|
|
string test2 = str.ReplaceStart("World!", "Mars?");
|
|
|
|
// assert
|
|
Assert.AreNotEqual(str, test1);
|
|
Assert.AreEqual("Bye World!", test1);
|
|
|
|
Assert.AreEqual(str, test2);
|
|
Assert.AreNotEqual("Hello Mars?", test2);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldReplaceEnd()
|
|
{
|
|
// arrange
|
|
string str = "Hello World!";
|
|
|
|
// act
|
|
string test1 = str.ReplaceEnd("Hello", "Bye");
|
|
string test2 = str.ReplaceEnd("World!", "Mars?");
|
|
|
|
// assert
|
|
Assert.AreEqual(str, test1);
|
|
Assert.AreNotEqual("Bye World!", test1);
|
|
|
|
Assert.AreNotEqual(str, test2);
|
|
Assert.AreEqual("Hello Mars?", test2);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldParseDecimal()
|
|
{
|
|
// arrange
|
|
decimal number = 1234.56m;
|
|
string stringNumberEn = "1234.56";
|
|
string stringNumberDe = "1234,56";
|
|
|
|
// act
|
|
decimal numberEn = stringNumberEn.ParseDecimal();
|
|
decimal numberDe = stringNumberDe.ParseDecimal();
|
|
|
|
// assert
|
|
Assert.AreEqual(number, numberEn);
|
|
Assert.AreEqual(number, numberDe);
|
|
}
|
|
|
|
[TestMethod]
|
|
public void ShouldParseDecimalWithThousandsSeparator()
|
|
{
|
|
// arrange
|
|
decimal number = 1234.56m;
|
|
string stringNumberEn = "1,234.56";
|
|
string stringNumberDe = "1.234,56";
|
|
|
|
// act
|
|
decimal numberEn = stringNumberEn.ParseDecimal();
|
|
decimal numberDe = stringNumberDe.ParseDecimal();
|
|
|
|
// assert
|
|
Assert.AreEqual(number, numberEn);
|
|
Assert.AreEqual(number, numberDe);
|
|
}
|
|
}
|
|
}
|