Source

CodeShape / src / CodeShapesTests / ClassAnalyzerTests.cs

using System.Collections.Generic;
using System.Linq;
using CodeShapes;
using CodeShapes.Shapes;
using NUnit.Framework;

namespace CodeShapesTests
{
	[TestFixture]
	public class ClassAnalyzerTests
	{
		private readonly ClassAnalyzer _classAnalyzer;
		
		public ClassAnalyzerTests()
		{
			_classAnalyzer = new ClassAnalyzer();
		}

		[Test]
		public void When_class_exists_then_it_doesnt_throw()
		{
			Assert.DoesNotThrow(() => _classAnalyzer.Analyze<SampleClass>());
		}


		[Test]
		public void When_there_is_fields_present_then_return()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			IEnumerable<CodeShape> fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));

			Assert.Contains(typeof(SampleClass).GetFields().First().Name, fields.Select(x=>x.Name).ToList());
		}

		[Test]
		public void When_protected_method_then_return_non_public()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			var methods = classShapes.Where(x => x.GetType().Equals(typeof(MethodCodeShape)) && x.Name.Contains("Protected"));

			Assert.True(methods.All(x=>x.Visibility == Visibility.NonPublic));
		}

		[Test]
		public void When_internal_method_then_return_non_public()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			var methods = classShapes.Where(x => x.GetType().Equals(typeof(MethodCodeShape)) && x.Name.Contains("MyPrivateMethod"));

			Assert.True(methods.All(x => x.Visibility == Visibility.NonPublic));
		}


		[Test]
		public void When_there_is_property_present_then_return()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClassWithProperty>();

			var fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));

			Assert.Contains(typeof(SampleClassWithProperty).GetFields().First().Name, fields.Select(x => x.Name).ToList());
		}
		
		[Test]
		public void When_field_is_readonly_then_its_counted_anyway()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			var fields = classShapes.Where(x => x.GetType().Equals(typeof(FieldCodeShape)));
			Assert.AreEqual(2, fields.Count());
			
		}

		[Test]
		public void When_there_is_a_public_method_on_the_class_Then_finds_it()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			var collection = classShapes.Where(x => x.GetType().Name == typeof(MethodCodeShape).Name);
			Assert.True(collection.Any(x => x.Name == "SampleMethod"));
		}

		[Test]
		public void When_there_is_a_public_method_present_the_must_return_publicMethodShape()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodCodeShape))));
		}

		[Test]
		public void When_there_is_a_private_method_present_the_must_return_privateMethodShape()
		{
			var classShapes = _classAnalyzer.Analyze<SampleClass>();

			Assert.True(classShapes.Any(x => x.GetType().Equals(typeof(MethodCodeShape))));
		}

		[Test]
		public void When_there_is_a_recursive_method_the_adjacency_list_contains_itself_as_a_dependency()
		{
			var shapes = _classAnalyzer.Analyze<ClassWithRecursiveMethod>();

			var methodShape = shapes.SingleOrDefault(s => s.Name == "ICallMyself");

			Assert.NotNull(methodShape);

			Assert.True(methodShape.AdjacencyList.Count == 1);
			Assert.AreSame(methodShape, methodShape.AdjacencyList.Values.First());
		}

		[Test]
		public void Can_find_adjacency_from_private_to_public()
		{
			var shapes = _classAnalyzer.Analyze<ClassWithBiDirectionalDependency>();

			var methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");

			Assert.IsNotEmpty(methodTwoCodeShape.AdjacencyList.Values.ToArray());
			var methodTwoAdjacency = methodTwoCodeShape.AdjacencyList.Values;


			Assert.AreEqual("MethodOne", methodTwoAdjacency.First().Name);
		}


		[Test]
		public void When_there_is_a_bidirectional_dependency_between_methods_both_methods_adjacency_lists_contain_the_other()
		{
			var shapes = _classAnalyzer.Analyze<ClassWithBiDirectionalDependency>();

			var methodOneCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodOne");
			var methodTwoCodeShape = shapes.SingleOrDefault(s => s.Name == "MethodTwo");

			var methodOneAdjacency = methodOneCodeShape.AdjacencyList.Values;
			var methodTwoAdjacency = methodTwoCodeShape.AdjacencyList.Values;

			Assert.Equals(1, methodOneAdjacency.Count);
			Assert.AreSame(methodOneAdjacency.First().Name, methodTwoCodeShape.Name);
			Assert.AreSame(methodTwoAdjacency.First().Name, methodOneCodeShape.Name);
		}

		private class ClassWithBiDirectionalDependency
		{
			public void MethodOne()
			{
				MethodTwo();
			}

			private void MethodTwo()
			{
				MethodOne();
			}
		}

		private class ClassWithRecursiveMethod
		{
			public void ICallMyself()
			{
				ICallMyself();
			}
		}
	}

	public class SampleClassWithProperty
	{
		public string SampleProperty;

		public void SampleMethod()
		{
			SampleProperty = "Blah";
		}
	}
}