A lot of stuff is described as unit tests. It makes it obvious to see what the code does.
[TestMethod]
public void WorksWithWindowsLines()
{
string text = "aaa\r\nbbb\r\nccc";
string expected = "aaa<br />\r\nbbb<br />\r\nccc";
string actual = text.AddHtmlLineBreaks();
Assert.AreEqual(expected, actual);
}
Works with \r
, \n
and \r\n
.
[TestMethod]
public void ManyWords()
{
string input = "hello wORLD";
string expected = "Hello WORLD";
string result = input.ToUpperFirstLetters();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void ManyWords_LeavesOtherCharsAsIs()
{
string input = "heLLo wORLd";
string expected = "Hello World";
string result = input.CapitalizeWords();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void French()
{
string input = "Là bas se trouvent une çédille et un œuf. L'été fût dur. Par la fenêtre. En grève ex æquo.";
string expected = "La bas se trouvent une cedille et un oeuf. L'ete fut dur. Par la fenetre. En greve ex aequo.";
string result = input.RemoveDiacritics();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void SomeSpaces()
{
string input = " \t\n\r\u00A0\u2002\u2003\u2004\u2005\u205F";
string expected = "";
string result = input.RemoveSpaces();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test()
{
string input = "German uses the umlauts ä, ö and ü. ";
string expected = "german-uses-the-umlauts-a-o-and-u";
string result = input.MakeUrlFriendly(false);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void PreserveCase()
{
string input = "German uses the umlauts ä, ö AND ü. ";
string expected = "German-uses-the-umlauts-a-o-AND-u";
string result = input.MakeUrlFriendly(true);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Nothing()
{
string input = "test";
string expected = "test-1";
string result = input.GetIncrementedString();
Assert.AreEqual(expected, result);
}
[TestMethod]
public void After2Goes3()
{
string input = "test-2";
string expected = "test-3";
string result = input.GetIncrementedString();
Assert.AreEqual(expected, result);
}
You can pass a lambda as uniquenessCheck
argument to check against a DB or something else.
This old collection needs a few methods...
.ToDictionary() .AsEnumerable()
DateTime.UtcNow.ToPrecision(DateTimePrecision.Month)
DateTime.UtcNow.ToPrecision(DateTimePrecision.Minute)
var date1 = DateTime.UtcNow.ToPrecision(DateTimePrecision.Second)
date1.IsEqualTo(2, DateTimePrecision.Second)
var local = date1.AsLocal(); // changes the Kind to Local (does not convert)
var utc = local.AsUtc(); // changes the Kind to Utc (does not convert)
long unix = utc.ToUnixTime;
var a1 = new int[] { 0, 1, 2, };
var a2 = new int[] { 3, 4, };
var combined = a1.CombineWith(a2);
// 0, 1, 2, 3, 4
var combinedMore = a1.CombineWith(a2, combined, a1);
// 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2
.AddRange()
.RemoveAll(x => x.Value == null)
EnumTool.GetDescription // gets a name from a resource dictionary
var desc = EnumTools.GetDescription(value, EnumStrings.ResourceManager);
var desc = EnumTools.GetDescription(value, EnumStrings.ResourceManager, null, "_Desc");
Converting DateTime to UTC is a little tricky with DateTime.Kind to handle. Calling a static method is not very friendly.
As I often convert to/from UTC, those 2 methods make it nicer.
// prepare
var date = new DateTime(1234, 5, 6, 7, 8, 9, 123, DateTimeKind.Utc);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");
// before
TimeZoneInfo.ConvertTimeFromUtc(dateTime.AsUnspecified(), tz)
// after
var result = tz.ConvertFromUtc(tz, date);
// before
TimeZoneInfo.ConvertTimeToUtc(dateTime.AsUnspecified(), tz)
// after
result = tz.ConvertToUtc(tz, result);
Sometimes it is nice to use a using() { }
block as an intuitive way to finaly{do-something}
.
DisposableOnce allows you to create a disposable object that will call a delegate on dispose.
[TestMethod]
public void DelegateIsCalledOnceDispose()
{
// prepare
int disposed = 0;
var target = new DisposableOnce(() => disposed++);
// execute
target.Dispose();
// verify
Assert.AreEqual(1, disposed);
}
Lists countries from the .NET cultures.
Multi-threaded recursive file delete.
Decomposes and composes an email address (account + tag + domain).
var emailSimple = new EmailAddress("[email protected]");
Assert.AreEqual(email.Account, "someone.nice");
Assert.AreEqual(email.Tag, null);
Assert.AreEqual(email.Domain, "sometest.com");
var emailTag = new EmailAddress("[email protected]");
Assert.AreEqual(email.Account, "someone.nice");
Assert.AreEqual(email.Tag, "my-tag");
Assert.AreEqual(email.Domain, "sometest.com");
Assert.AreEqual(email.Value, "[email protected]");
Assert.AreEqual(email.ValueWithoutTag, "[email protected]");
Makes nicer phone numbers
"00123456", "0012 (0) 3456", "+12 34-56", "0012-34-56" => "+123456"