Пример #1
0
		public void Object_Field_OfType_TargetObject_BindingFlags()
		{
			var target = new FieldMethodsTestFixture();

			var publicInstance = BindingFlags.Public | BindingFlags.Instance;
			var nonpublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;


			var result = _.Object.Field.OfType(target, typeof (int), publicInstance);

			Assert.IsTrue(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldShowInt")));

			//check it doesn't pick up the wrong type
			Assert.IsFalse(result.Any(a=>a == typeof(FieldMethodsTestFixture).GetField("ShouldShowString")));

			//and that it doesn't pick up the wrong accesiblity
			Assert.IsFalse(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldntShowInt")));

			//check that the only field it did pick up was the right one
			Assert.AreEqual(1,result.Count());


			//check less accessible string 
			result = _.Object.Field.OfType(target, typeof (int), nonpublicInstance);

			//check the right one was found
			Assert.IsTrue(result.Any(a=>a == typeof(FieldMethodsTestFixture).GetField("ShouldntShowInt",nonpublicInstance)));

			//check wrong type didn't happen
			Assert.IsFalse(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldntShowString")));

			//check that the wrong accesiblity wasn't picked up
			Assert.IsFalse(result.Any(a=>a == typeof(FieldMethodsTestFixture).GetField("ShouldShowInt")));

			//check that was the only field picked up
			Assert.AreEqual(1,result.Count());

		}
Пример #2
0
		public void Object_Fields_Find_TargetObject_NameTypeBindingFlagsCaseSenstive()
		{
			var target = new FieldMethodsTestFixture();

			var bound = _.Function.Partial<object, string, Type, bool, BindingFlags, FieldInfo>(_.Object.Field.Find,
				target);

			var publicInstance = BindingFlags.Public | BindingFlags.Instance;
			var nonpublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;

			//check positive cases
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowString", publicInstance),
				bound("ShouldShowString", typeof (string), true, publicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowString", publicInstance),
				bound("shouldshowstring", typeof (string), false, publicInstance));

			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowInt", publicInstance),
				bound("ShouldShowInt", typeof (int), true, publicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowInt", publicInstance),
				bound("ShouldShowInt", typeof (int), false, publicInstance));

			//check to make sure the case sensitive search is being done correctly
			Assert.IsNull(bound("shouldshowstring", typeof (string), true, publicInstance));
			Assert.IsNull(bound("shouldntshowint", typeof (int), true, publicInstance));

			//check that nonpublic fields aren't showing up when doing a search for public fields
			Assert.IsNull(bound("ShouldntShowString", typeof (string), true, publicInstance));
			Assert.IsNull(bound("ShouldntShowInt", typeof (int), true, publicInstance));

			//check nonpublic can be accessed
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowString", nonpublicInstance),
				bound("ShouldntShowString", typeof (string), true, nonpublicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowString", nonpublicInstance),
				bound("shouldntshowstring", typeof (string), false, nonpublicInstance));

			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowInt", nonpublicInstance),
				bound("ShouldntShowInt", typeof (int), true, nonpublicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowInt", nonpublicInstance),
				bound("shouldntshowint", typeof (int), false, nonpublicInstance));


			//check to make sure the case sensitive search is being done correctly
			Assert.IsNull(bound("shouldntshowstring", typeof (string), true, nonpublicInstance));
			Assert.IsNull(bound("shouldntshowint", typeof (int), true, nonpublicInstance));

			//check that public fields aren't showing up when doing a search for public fields
			Assert.IsNull(bound("ShoudShowString", typeof (string), true, nonpublicInstance));
			Assert.IsNull(bound("ShouldShowInt", typeof (string), true, nonpublicInstance));

		}
Пример #3
0
		public void Object_Fields_OfType_TargetObject()

		{
			var target = new FieldMethodsTestFixture();


			var result = _.Object.Field.OfType(target, typeof (int));
			Assert.IsTrue(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldShowInt")));
			Assert.IsFalse(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldShowString")));
			Assert.AreEqual(1, result.Count());

			result = _.Object.Field.OfType(target, typeof (string));
			Assert.IsTrue(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldShowString")));
			Assert.IsFalse(result.Any(a => a == typeof (FieldMethodsTestFixture).GetField("ShouldShowInt")));
			Assert.AreEqual(1, result.Count());
		}
Пример #4
0
		public void Object_Fields_Find_TargetObject_NameType()
		{

			var target = new FieldMethodsTestFixture();

			var bound = _.Function.Partial<object, string, Type, FieldInfo>(_.Object.Field.Find, target);

			//default should only show public instance fields by default
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowString"),
				bound("ShouldShowString", typeof (string)));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowInt"),
				bound("ShouldShowInt", typeof (int)));

			//check mismatching types
			Assert.IsNull(bound("ShouldShowString", typeof (int)));
			Assert.IsNull(bound("ShouldShowInt", typeof (string)));

			//check the non public fields are being protected

			Assert.IsNull(bound("ShouldntShowString", typeof (string)));
			Assert.IsNull(bound("ShouldntShowInt", typeof (int)));


		}
Пример #5
0
		public void Object_Fields_Find_TargetObject_NameTypeBindingFlags()
		{
			var target = new FieldMethodsTestFixture();

			var bound = _.Function.Partial<object, string, Type, BindingFlags, FieldInfo>(_.Object.Field.Find, target);

			var publicInstance = BindingFlags.Public | BindingFlags.Instance;
			var nonpublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;

			//check positive cases
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowString", publicInstance),
				bound("ShouldShowString", typeof (string), publicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldShowInt", publicInstance),
				bound("ShouldShowInt", typeof (int), publicInstance));

			//check mismatching types
			Assert.IsNull(bound("ShouldShowString", typeof (int), publicInstance));
			Assert.IsNull(bound("ShouldShowInt", typeof (string), nonpublicInstance));

			//check that nonpublic fields aren't showing up when doing a search for public fields
			Assert.IsNull(bound("ShouldntShowString", typeof (string), publicInstance));
			Assert.IsNull(bound("ShouldntShowInt", typeof (int), publicInstance));


			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowString", nonpublicInstance),
				bound("ShouldntShowString", typeof (string), nonpublicInstance));
			Assert.AreEqual(typeof (FieldMethodsTestFixture).GetField("ShouldntShowInt", nonpublicInstance),
				bound("ShouldntShowInt", typeof (int), nonpublicInstance));

			//check to make sure public fields aren't showing up when looking for nonpublic fields
			Assert.IsNull(bound("ShouldShowString", typeof (string), nonpublicInstance));
			Assert.IsNull(bound("ShouldShowInt", typeof (int), nonpublicInstance));

		}
Пример #6
0
		public void Object_Fields_Values_NonGenericBindingFlags()
		{
			int privateInt = 1;
			string privateString = "private string";

			var target = new FieldMethodsTestFixture(privateInt, privateString);

			target.ShouldShowInt = 1;
			target.ShouldShowString = "A String";

			var publicInstance = BindingFlags.Instance | BindingFlags.Public;
			var nonpublicInstance = BindingFlags.Instance | BindingFlags.NonPublic;

			var allValues = _.Object.Field.Values(target, publicInstance);

			Assert.IsTrue(allValues.Any(a => a.Equals(target.ShouldShowInt)));
			Assert.IsTrue(allValues.Any(a => a.Equals(target.ShouldShowString)));
			Assert.AreEqual(2, allValues.Count());


			var allNonPublicValues = _.Object.Field.Values(target, nonpublicInstance);
			Assert.IsTrue(allNonPublicValues.Any(a => a.Equals(privateInt)));
			Assert.IsTrue(allNonPublicValues.Any(a => a.Equals(privateString)));
			Assert.AreEqual(2, allNonPublicValues.Count());

		}
Пример #7
0
		public void Object_Fields_Values_NonGeneric()
		{
			var target = new FieldMethodsTestFixture();

			target.ShouldShowInt = 1;
			target.ShouldShowString = "A String";

			var allValues = _.Object.Field.Values(target);

			Assert.IsTrue(allValues.Any(a => a.Equals(target.ShouldShowInt)));
			Assert.IsTrue(allValues.Any(a => a.Equals(target.ShouldShowString)));
			Assert.AreEqual(2, allValues.Count());

		}
Пример #8
0
		public void Object_Fields_Values_GenericBindingFlags()
		{
			int privateInt = -1;
			string privateString = "private string";
			var target = new FieldMethodsTestFixture(privateInt, privateString);

			target.ShouldShowInt = 1;
			target.ShouldShowString = "A String";

			var publicInstance = BindingFlags.Public | BindingFlags.Instance;
			var nonpublicInstance = BindingFlags.NonPublic | BindingFlags.Instance;

			var publicInts = _.Object.Field.Values<int>(target, publicInstance);

			Assert.IsTrue(publicInts.Any(a => a == target.ShouldShowInt));
			Assert.AreEqual(1, publicInts.Count());

			var privateInts = _.Object.Field.Values<int>(target, nonpublicInstance);


			Assert.IsTrue(privateInts.Any(a => a == privateInt));
			Assert.AreEqual(1, privateInts.Count());


			var publicStrings = _.Object.Field.Values<string>(target, publicInstance);

			Assert.IsTrue(publicStrings.Any(a => a == target.ShouldShowString));
			Assert.AreEqual(1, publicStrings.Count());

			var privateStrings = _.Object.Field.Values<string>(target, nonpublicInstance);

			Assert.IsTrue(privateStrings.Any(a => a == privateString));
			Assert.AreEqual(1, privateStrings.Count());

			//check that a field with no matching fields does not magically produce one

			var allDecimals = _.Object.Field.Values<decimal>(target, publicInstance);
			Assert.IsTrue(!allDecimals.Any());
		}
Пример #9
0
		public void Object_Fields_Values_Generic()
		{
			var target = new FieldMethodsTestFixture();

			target.ShouldShowInt = 1;
			target.ShouldShowString = "A String";

			var allInts = _.Object.Field.Values<int>(target);

			Assert.IsTrue(allInts.Any(a => a == target.ShouldShowInt));
			Assert.AreEqual(1, allInts.Count());

			var allStrings = _.Object.Field.Values<string>(target);

			Assert.IsTrue(allStrings.Any(a => a == target.ShouldShowString));
			Assert.AreEqual(1, allStrings.Count());

			//check that a field with no matching properties does not magically produce one

			var allDecimals = _.Object.Field.Values<decimal>(target);
			Assert.IsTrue(!allDecimals.Any());
		}
Пример #10
0
		public async Task ObjectField()
		{
			var target = new FieldMethodsTestFixture();

			

			await Util.Tasks.Start(() =>
			{
				//test true one public string field
				//test no type
				//test with type
				//test not with wrong type
				//test not private method

				var showFieldNoArgs = _.Object.Field.Find(target, "ShouldShowString");
				var showFieldTypeArgs = _.Object.Field.Find(target, "ShouldShowString", typeof(string));
				var shouldNotShowFieldWrongArgs = _.Object.Field.Find(target, "ShouldShowString", typeof(int));
				var shouldntShowFieldPrivate = _.Object.Field.Find(target, "ShoulntShowString");
				var shouldntShowFieldPrivateWrongType = _.Object.Field.Find(target, "ShoulntShowString", typeof(string));

				Assert.IsNotNull(showFieldNoArgs);

				foreach (var item in new[ ] { showFieldTypeArgs })
					Assert.AreEqual(showFieldNoArgs, item);

				foreach (var item in new[ ] { shouldNotShowFieldWrongArgs, shouldntShowFieldPrivate, shouldntShowFieldPrivateWrongType })
					Assert.IsNull(item);

			}, () =>
			{
				//test true one public int field
				//test no type
				//test with type
				//test not with wrong type
				//test not private method

				var showFieldNoArgs = _.Object.Field.Find(target, "ShouldShowInt");
				var showFieldTypeArgs = _.Object.Field.Find(target, "ShouldShowInt", typeof(int));
				var shouldNotShowFieldWrongArgs = _.Object.Field.Find(target, "ShouldntShowInt", typeof(string));
				var shouldntShowFieldPrivate = _.Object.Field.Find(target, "ShouldntShowInt");
				var shouldntShowFieldPrivateWrongType = _.Object.Field.Find(target, "ShouldintShowInt", typeof(string));

				Assert.IsNotNull(showFieldNoArgs);

				foreach (var item in new[ ] { showFieldTypeArgs })
					Assert.AreEqual(showFieldNoArgs, item);

				foreach (var item in new[ ] { shouldNotShowFieldWrongArgs, shouldntShowFieldPrivate, shouldntShowFieldPrivateWrongType })
					Assert.IsNull(item);
			}, () =>
			{
				//test true no properties picked up

				//shouldnt show properties
				Assert.IsNull(_.Object.Field.Find(target, "ShouldNotShowProperty"));
				Assert.IsNull(_.Object.Field.Find(target, "ShouldNotShowProperty", typeof(int)));
				Assert.IsNull(_.Object.Field.Find(target, "ShouldNotShowProperty", typeof(string)));
			});
		}