Commits

roundcrisis committed 6a23772

Adding a transform method

Comments (0)

Files changed (10)

src/CodeShapes/ClassAnalyzer.cs

 		public IEnumerable<FieldCodeShape> FindFields(Type type)
 		{
 			var fieldInfos = type.GetFields(BindingFlags.Public| BindingFlags.NonPublic| BindingFlags.Static| BindingFlags.Instance);
-			return fieldInfos.Select(fieldInfo => new FieldCodeShape(fieldInfo.Name));
+			return fieldInfos.Select(fieldInfo => new FieldCodeShape(fieldInfo.Name, fieldInfo.IsPublic ? Visibility.Public : Visibility.NonPublic));
 		}
 
 		private IEnumerable<ICodeShape> FindAllPrivateMethods(Type type)
 		{
 			var methodInfos = type.GetMethods( BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
-			return methodInfos.Select(methodInfo => new PrivateMethodCodeShape(methodInfo, methodInfo.Name));
+			return methodInfos.Select(methodInfo => new NonPublicMethodCodeShape(methodInfo, methodInfo.Name));
 		}
 
 		private IEnumerable<ICodeShape> FindAllPublicMethods(Type type)

src/CodeShapes/CodeShapes.csproj

     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
+    <RunCodeAnalysis>true</RunCodeAnalysis>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
     <Compile Include="Shapes\ICodeShape.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="Shapes\MethodCodeShape.cs" />
-    <Compile Include="Shapes\PrivateMethodCodeShape.cs" />
+    <Compile Include="Shapes\NonPublicMethodCodeShape.cs" />
     <Compile Include="Shapes\PublicMethodCodeShape.cs" />
   </ItemGroup>
   <ItemGroup>

src/CodeShapes/Shapes/CodeShape.cs

 {
 	public abstract class CodeShape : ICodeShape
 	{
-		protected CodeShape(string methodName)
+		protected CodeShape(string methodName, Visibility visibility)
 		{
 			Name = methodName;
+			Visibility = visibility;
 			AdjacencyList = new Dictionary<int, ICodeShape>();
 		}
-		public string Name { get; set; }
+		public string Name { get; protected set; }
+
+		public Visibility Visibility { get; private set; }
 
 		public IDictionary<int, ICodeShape> AdjacencyList { get; private set; }
 	}

src/CodeShapes/Shapes/FieldCodeShape.cs

 {
 	public class FieldCodeShape : CodeShape
 	{
-		public FieldCodeShape(string fieldName) : base(fieldName)
+		public FieldCodeShape(string fieldName, Visibility visibility) : base(fieldName, visibility)
 		{
 		}
 	}

src/CodeShapes/Shapes/ICodeShape.cs

 {
 	public interface ICodeShape
 	{
-		string Name { get; set; }
+		string Name { get; }
+		Visibility Visibility { get; }
 		IDictionary<int, ICodeShape> AdjacencyList { get;  }
 	}
+
+	public enum Visibility
+	{
+		Public,
+		NonPublic
+	}
 }

src/CodeShapes/Shapes/MethodCodeShape.cs

 {
 	public class MethodCodeShape : CodeShape
 	{
-		public MethodCodeShape(MethodBase methodBase, string methodName)
-			: base(methodName)
+		public MethodCodeShape(MethodBase methodBase, string methodName, Visibility visibility)
+			: base(methodName, visibility)
 		{
 			MethodBase = methodBase;
 		}
 
-		public MethodCodeShape(string methodName)
-			: base(methodName)
+		public MethodCodeShape(string methodName, Visibility visibility)
+			: base(methodName, visibility)
 		{}
 
 		public MethodBase MethodBase { get; set; }

src/CodeShapes/Shapes/PrivateMethodCodeShape.cs

-using System.Reflection;
-
-namespace CodeShapes.Shapes
-{
-	public class PrivateMethodCodeShape : MethodCodeShape
-	{
-		public PrivateMethodCodeShape(MethodBase methodBase, string methodName)
-			: base(methodBase, methodName)
-		{
-		}
-	}
-}

src/CodeShapes/Shapes/PublicMethodCodeShape.cs

 	public class PublicMethodCodeShape : MethodCodeShape
 	{
 		public PublicMethodCodeShape(MethodBase methodBase, string methodName)
-			: base(methodBase, methodName)
+			: base(methodBase, methodName, Visibility.Public)
 		{
 		}
 	}

src/CodeShapesTests/ClassAnalyzerTests.cs

 
 			Assert.Contains(typeof(SampleClassWithProperty).GetFields().First().Name, fields.Select(x => x.Name));
 		}
-
-
+		
 		[Fact]
 		public void When_field_is_readonly_then_its_counted_anyway()
 		{

src/CodeShapesTests/InstructionTests.cs

 using CodeShapes.Shapes;
 using QuickGraph;
 using QuickGraph.Graphviz;
+using QuickGraph.Graphviz.Dot;
 using Xunit;
 
 namespace CodeShapesTests
 		public void when_shape_collection_contains_a_method_referencing_a_property_Then_result()
 		{
 			var name = "SampleMethod";
-			IEnumerable<ICodeShape> shapes = CreateShapes(name);
+			var shapes = CreateShapes(name);
 			var transformToGraph = new TransformToGraph(shapes);
-			var adjacencyGraph = transformToGraph.Transform();
+			transformToGraph.Transform();
 
-			var generate = new GraphvizAlgorithm<ICodeShape, SEdge<ICodeShape>>(adjacencyGraph).Generate();
+//			var generate = new GraphvizAlgorithm<ICodeShape, SEdge<ICodeShape>>(adjacencyGraph);
+			
+			
 		}
 
 		private IEnumerable<ICodeShape> CreateShapes(string name)
 		{
 			var methodInfo = typeof (SampleClass).GetMethod(name);
-			var fieldCodeShape = new FieldCodeShape("PublicField");
+			var fieldCodeShape = new FieldCodeShape("PublicField", Visibility.Public);
 			var publicMethodCodeShape = new PublicMethodCodeShape(methodInfo, name);
 			publicMethodCodeShape.AdjacencyList.Add(1, fieldCodeShape);
-			return new List<ICodeShape>()
+			return new List<ICodeShape>
 			       	{
 			       		publicMethodCodeShape,
 			       		fieldCodeShape
 	public class TransformToGraph
 	{
 		private readonly IEnumerable<ICodeShape> _codeShapes;
+		AdjacencyGraph<ICodeShape, SEdge<ICodeShape>> _adjacencyGraph;
 
 		public TransformToGraph(IEnumerable<ICodeShape> codeShapes)
 		{
 			_codeShapes = codeShapes;
 		}
 
-		public AdjacencyGraph<ICodeShape, SEdge<ICodeShape>> Transform()
+		public void FormatVertex()
 		{
-			var adjacencyGraph = new AdjacencyGraph<ICodeShape, SEdge<ICodeShape>>();
+			var graphviz = new GraphvizAlgorithm<ICodeShape, SEdge<ICodeShape>>(_adjacencyGraph as IEdgeListGraph<ICodeShape, SEdge<ICodeShape>>);
+			graphviz.FormatVertex += FormatS;
+		}
+
+		private void FormatS(object sender, FormatVertexEventArgs<ICodeShape> e)
+		{
+			var codeShape = e.Vertex;
+			e.VertexFormatter.Label = codeShape.Name;
+			if (codeShape.GetType() == typeof(MethodCodeShape))
+			{
+				e.VertexFormatter.Shape = GraphvizVertexShape.Rectangle;
+			}
+		}
+
+		public void Transform()
+		{
+			_adjacencyGraph = new AdjacencyGraph<ICodeShape, SEdge<ICodeShape>>();
 			foreach (var shape in _codeShapes)
 			{
-				adjacencyGraph.AddVertex(shape);
+				_adjacencyGraph.AddVertex(shape);
 			}
 			foreach (var codeShape in _codeShapes)
 			{
 				foreach (var shape in codeShape.AdjacencyList)
 				{
-					adjacencyGraph.AddEdge(new SEdge<ICodeShape>(codeShape, shape.Value));
+					_adjacencyGraph.AddEdge(new SEdge<ICodeShape>(codeShape, shape.Value));
 				}
 			}
-			return adjacencyGraph;
+			
 		}
 	}