Click here to Skip to main content
15,895,746 members
Articles / Hosted Services / Azure

Kerosene ORM: a dynamic, configuration-less and self-adaptive ORM for POCO objects supporting a SQL-like syntax from C#

Rate me:
Please Sign up or sign in to vote.
4.96/5 (71 votes)
1 Mar 2015CPOL35 min read 548.3K   4.6K   212  
The seventh version of the dynamic, configuration-less and self-adaptive Kerosene ORM library, that provides full real support for POCO objects, natural SQL-like syntax from C#, and advanced capabilities while being extremely easy to use.
// ======================================================== Test_StringEx.cs
namespace Kerosene.Tools.Test
{
	using Microsoft.VisualStudio.TestTools.UnitTesting;
	using System;
	using System.Linq;

	// ==================================================== 
	[TestClass]
	public partial class Test_StringEx
	{
		[TestMethod]
		public void Format_With_Null_Reference_Not_Allowed()
		{
			string str, res;
			try
			{
				str = (string)null;
				res = str.FormatWith("whatever"); Assert.Fail();
			}
			catch (NullReferenceException) { }
		}

		[TestMethod]
		public void Format_tWith_No_Arguments()
		{
			string str, res;

			str = "Example";
			res = str.FormatWith();
			Assert.AreEqual(str, res);
		}

		[TestMethod]
		public void Format_With_Few_Arguments_Fails()
		{
			string str, res;
			try
			{
				str = "Hello {1} {2}";
				res = str.FormatWith("James"); Assert.Fail();
			}
			catch (FormatException) { }
		}
	}

	// ==================================================== 
	public partial class Test_StringEx
	{
		[TestMethod]
		public void Null_If_Trimmed_Is_Empty()
		{
			string str, res;

			str = string.Empty;
			res = str.NullIfTrimmedIsEmpty();
			Assert.IsNull(res);

			str = "   ";
			res = str.NullIfTrimmedIsEmpty();
			Assert.IsNull(res);
		}

		[TestMethod]
		public void Empty_If_Trimmed_Is_Null()
		{
			var str = (string)null;
			var res = str.EmptyIfTrimmedIsNull();
			Assert.AreEqual(string.Empty, res);
		}
	}

	// ==================================================== 
	public partial class Test_StringEx
	{
		[TestMethod]
		public void Left_Bad_Scenarios()
		{
			string str, res;

			str = null;
			try { res = str.Left(1); Assert.Fail("Null reference shall not be allowed."); }
			catch (NullReferenceException) { }

			str = "whatever";
			try { res = str.Left(-1); Assert.Fail("Negative number of chars."); }
			catch (ArgumentException) { }
		}

		[TestMethod]
		public void Left_Good_Scenarios()
		{
			string str, res;

			str = "whatever";
			res = str.Left(0); Assert.AreEqual(string.Empty, res);
			res = str.Left(4); Assert.AreEqual("what", res);
			res = str.Left(999); Assert.AreEqual("whatever", res);
		}

		[TestMethod]
		public void Right_Bad_Scenarios()
		{
			string str, res;

			str = null;
			try { res = str.Right(1); Assert.Fail("Null reference shall not be allowed."); }
			catch (NullReferenceException) { }

			str = "whatever";
			try { res = str.Right(-1); Assert.Fail("Negative number of chars."); }
			catch (ArgumentException) { }
		}

		[TestMethod]
		public void Right_Good_Scenarios()
		{
			string str, res;

			str = "whatever";
			res = str.Right(0); Assert.AreEqual(string.Empty, res);
			res = str.Right(4); Assert.AreEqual("ever", res);
			res = str.Right(999); Assert.AreEqual("whatever", res);
		}
	}

	// ==================================================== 
	public partial class Test_StringEx
	{
		[TestMethod]
		public void Remove_Bad_Scenarios()
		{
			string source, target, res;

			source = null;
			target = "whatever";
			try { res = source.Remove(target); Assert.Fail(); }
			catch (NullReferenceException) { }
		}

		[TestMethod]
		public void Remove_With_Null_Target()
		{
			var source = "wahtever";
			var target = (string)null;
			var res = source.Remove(target);
			Assert.AreEqual(source, res);
		}

		[TestMethod]
		public void Remove_Good_Scenarios()
		{
			string source, target, res;

			source = "whatever";
			target = "ever";
			res = source.Remove(target); Assert.AreEqual("what", res);

			source = "whatever";
			target = "EVER";
			res = source.Remove(target); Assert.AreEqual(source, res);
		}

		[TestMethod]
		public void Remove_Good_Scenarios_Ignore_Case()
		{
			string source, target, res;

			source = "whatever";
			target = "EVER";
			res = source.Remove(target, StringComparison.OrdinalIgnoreCase); Assert.AreEqual("what", res);
		}
	}

	// ==================================================== 
	public partial class Test_StringEx
	{
		[TestMethod]
		public void IndexOf_Char_Good_Scenarios()
		{
			string source;
			char target;
			int res;

			source = string.Empty;
			target = 'r';
			res = source.IndexOf(target, StringComparison.CurrentCulture); Assert.AreEqual(-1, res);

			source = "whatever";
			target = 'x';
			res = source.IndexOf(target, StringComparison.CurrentCulture); Assert.AreEqual(-1, res);

			source = "whatever";
			target = 't';
			res = source.IndexOf(target, StringComparison.CurrentCulture); Assert.AreEqual(3, res);
		}

		[TestMethod]
		public void IndexOf_Char_Good_Scenarios_Ignore_Case()
		{
			string source;
			char target;
			int res;

			source = "whatever";
			target = 'X';
			res = source.IndexOf(target, StringComparison.CurrentCultureIgnoreCase); Assert.AreEqual(-1, res);

			source = "whatever";
			target = 'T';
			res = source.IndexOf(target, StringComparison.CurrentCultureIgnoreCase); Assert.AreEqual(3, res);
		}

		[TestMethod]
		public void IndexOf_Any_Bad_Scenarios()
		{
			string source;
			char[] anyOf;
			int res;

			source = null;
			anyOf = "x".ToCharArray();
			try { res = source.IndexOfAny(anyOf, StringComparison.CurrentCulture); }
			catch (NullReferenceException) { }

			source = "whatever";
			anyOf = null;
			try { res = source.IndexOfAny(anyOf, StringComparison.CurrentCulture); }
			catch (ArgumentNullException) { }
		}

		[TestMethod]
		public void IndexOf_Any_Good_Scenarios()
		{
			string source;
			char[] anyOf;
			int res;

			source = "whatever";
			anyOf = "x".ToCharArray();
			res = source.IndexOfAny(anyOf, StringComparison.CurrentCulture); Assert.AreEqual(-1, res);

			source = "whatever";
			anyOf = "xyzt".ToCharArray();
			res = source.IndexOfAny(anyOf, StringComparison.CurrentCulture); Assert.AreEqual(3, res);
		}

		[TestMethod]
		public void IndexOf_Any_Good_Scenarios_Ignore_Case()
		{
			string source;
			char[] anyOf;
			int res;

			source = "whatever";
			anyOf = "x".ToCharArray();
			res = source.IndexOfAny(anyOf, StringComparison.CurrentCultureIgnoreCase); Assert.AreEqual(-1, res);

			source = "whatever";
			anyOf = "xyzT".ToCharArray();
			res = source.IndexOfAny(anyOf, StringComparison.CurrentCultureIgnoreCase); Assert.AreEqual(3, res);
		}

		[TestMethod]
		public void IndexOf_NotValid_Bad_Scenarios()
		{
			string source;
			char[] valids;
			int res;

			source = null;
			valids = "whatever".ToCharArray();
			try { res = source.IndexOfNotValid(valids); Assert.Fail(); }
			catch (NullReferenceException) { }

			source = string.Empty;
			valids = "whatever".ToCharArray();
			try { res = source.IndexOfNotValid(valids); Assert.Fail(); }
			catch (EmptyException) { }

			source = "whatever";
			valids = null;
			try { res = source.IndexOfNotValid(valids); Assert.Fail(); }
			catch (ArgumentNullException) { }
		}

		[TestMethod]
		public void IndexOf_NotValid_Good_Scenarios()
		{
			string source;
			char[] valids;
			int res;

			source = "whatever";
			valids = "WHAT".ToCharArray();
			res = source.IndexOfNotValid(valids, StringComparison.CurrentCultureIgnoreCase);
			Assert.AreEqual(4, res);
		}
	}

	// ==================================================== 
	public partial class Test_StringEx
	{
		[TestMethod]
		public void Validated_String()
		{
			string str, tmp;

			str = null;
			tmp = str.Validated(canbeNull: true); Assert.IsNull(tmp);
			try { tmp = str.Validated(); Assert.Fail(); }
			catch (ArgumentNullException) { }

			str = "   ";
			tmp = str.Validated(emptyAsNull: true); Assert.IsNull(tmp);
			tmp = str.Validated(canbeEmpty: true); Assert.AreEqual(string.Empty, tmp);
			try { str.Validated(); Assert.Fail(); }
			catch (EmptyException) { }

			str = "Hi";
			try { tmp = str.Validated(minLen: 3); Assert.Fail(); }
			catch (ArgumentException) { }

			str = "James";
			try { tmp = str.Validated(maxLen: 3); Assert.Fail(); }
			catch (ArgumentException) { }

			var valids = "James".ToCharArray();
			str = "James Bond";
			int i = str.IndexOfNotValid(valids); Assert.AreEqual(5, i);

			var invalids = "xyzb".ToCharArray();
			i = str.IndexOfAny(invalids, StringComparison.CurrentCultureIgnoreCase);
			Assert.AreEqual(6, i);
		}
	}
}
// ======================================================== 

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Spain Spain
mbarbac has worked in start-ups, multinational tech companies, and consulting ones, serving as CIO, CTO, SW Development Director, and Consulting Director, among many other roles.

Solving complex puzzles and getting out of them business value has ever been among his main interests - and that's why he has spent his latest 25 years trying to combine his degree in Theoretical Physics with his MBA... and he is still trying to figure out how all these things can fit together.

Even if flying a lot across many countries, along with the long working days that are customary in IT management and Consultancy, he can say that, after all, he lives in Spain (at least the weekends).

Comments and Discussions