1
0

Solution restructured to use multiple test projects

This commit is contained in:
2024-07-04 18:22:26 +02:00
parent 508379d704
commit df6763b99b
144 changed files with 387 additions and 1693 deletions

View File

@@ -0,0 +1,351 @@
using System;
using System.Linq;
using System.Net;
using System.Text;
namespace AMWD.Common.Tests.Extensions
{
[TestClass]
public class StringExtensionsTest
{
[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 = [];
// act
string hex1 = bytes1.BytesToHex();
string hex2 = bytes2.BytesToHex();
// assert
Assert.IsNull(hex1);
Assert.IsNull(hex2);
}
[TestMethod]
public void ShouldReturnHexString()
{
// arrange
byte[] bytes = [0xaf, 0xfe];
// act
string hex = bytes.BytesToHex();
// assert
Assert.IsNotNull(hex);
Assert.AreEqual("affe", hex);
}
[TestMethod]
public void ShouldReturnHexStringWithDelimiter()
{
// arrange
byte[] bytes = [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 hex1 = plain.HexEncode();
string hex2 = plain.HexEncode(Encoding.Default);
// assert
Assert.AreEqual("48656c6c6f", hex1);
Assert.AreEqual("48656c6c6f", hex2);
}
[TestMethod]
public void ShouldDecodeStringFromHex()
{
// arrange
string hex = "48656c6c6f";
// act
string plain1 = hex.HexDecode();
string plain2 = hex.HexDecode(Encoding.Default);
// assert
Assert.AreEqual("Hello", plain1);
Assert.AreEqual("Hello", plain2);
}
[TestMethod]
public void ShouldEncodeStringToBase64()
{
// arrange
string plain = "Hello";
// act
string base641 = plain.Base64Encode();
string base642 = plain.Base64Encode(Encoding.Default);
// assert
Assert.AreEqual("SGVsbG8=", base641);
Assert.AreEqual("SGVsbG8=", base642);
}
[TestMethod]
public void ShouldDecodeStringFromBase64()
{
// arrange
string base64 = "SGVsbG8=";
// act
string plain1 = base64.Base64Decode();
string plain2 = base64.Base64Decode(Encoding.Default);
// assert
Assert.AreEqual("Hello", plain1);
Assert.AreEqual("Hello", plain2);
}
[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);
}
[TestMethod]
public void ShouldValidateEmailAddressWithoutRecordCheck()
{
// arrange
string validEmailWithoutTag = "test@gmail.com";
string validEmailWithTag = "test+tag@not.exists";
string invalidEmailWithoutTag = "<Test Account> test@gmail.com";
string invalidEmailWithTag = "<Test Account> test+tag@not.exists";
string nullStr = null;
// act
bool validWithoutTag = validEmailWithoutTag.IsValidEmailAddress(checkForDnsRecord: false);
bool validWithTag = validEmailWithTag.IsValidEmailAddress(checkForDnsRecord: false);
bool invalidWithoutTag = !invalidEmailWithoutTag.IsValidEmailAddress(checkForDnsRecord: false);
bool invalidWithTag = !invalidEmailWithTag.IsValidEmailAddress(checkForDnsRecord: false);
bool nullTest = nullStr.IsValidEmailAddress(checkForDnsRecord: false);
// assert
Assert.IsTrue(validWithoutTag);
Assert.IsTrue(validWithTag);
Assert.IsTrue(invalidWithoutTag);
Assert.IsTrue(invalidWithTag);
Assert.IsFalse(nullTest);
}
[TestMethod]
public void ShouldValidateEmailAddressWithRecordCheck()
{
// arrange
string validEmail = "test@gmail.com";
string invalidEmail = "test@not.exists";
// act
bool valid = validEmail.IsValidEmailAddress(checkForDnsRecord: true);
bool invalid = !invalidEmail.IsValidEmailAddress(checkForDnsRecord: true);
// assert
Assert.IsTrue(valid);
Assert.IsTrue(invalid);
}
[TestMethod]
public void ShouldValidateEmailAddressWithRecordCheckDefinedNameservers()
{
// arrange
string validEmail = "test@gmail.com";
string invalidEmail = "test@not.exists";
var nameserver = new IPEndPoint(IPAddress.Parse("1.1.1.1"), 53);
// act
bool valid = validEmail.IsValidEmailAddress(new[] { nameserver });
bool invalid = !invalidEmail.IsValidEmailAddress(new[] { nameserver });
// assert
Assert.IsTrue(valid);
Assert.IsTrue(invalid);
}
[TestMethod]
public void ShouldWorkWithNullOrEmptyString()
{
// arrange
string nullStr = null;
string emptyStr = "";
// act
string hexEncodeNull = nullStr.HexEncode();
string hexEncodeEmpty = emptyStr.HexEncode();
string hexDecodeNull = nullStr.HexDecode();
string hexDecodeEmpty = emptyStr.HexDecode();
// assert
Assert.IsNull(hexEncodeNull);
Assert.AreEqual("", hexEncodeEmpty);
Assert.IsNull(hexDecodeNull);
Assert.AreEqual("", hexDecodeEmpty);
}
[TestMethod]
public void ShouldAddCustomLineTermination()
{
// arrange
string value = "abc";
var sb = new StringBuilder();
// act
sb.AppendLine(value, "\r");
sb.AppendLine(value, "\r");
// assert
Assert.AreEqual($"{value}\r{value}\r", sb.ToString());
Assert.IsFalse(sb.ToString().Contains('\n'));
}
}
}