public void WhenRunningTaskShouldComputeMetrics()
        {
            ITaskItem[] providers = new TaskItemMock[1];
            providers[0] = new TaskItemMock("MSBuildCodeMetrics.Core.UnitTests.Mock.CodeMetricsProviderSingleFileMock, MSBuildCodeMetrics.Core.UnitTests").
                AddMetadata("Data", "<Metric Name=\"LinesOfCode\"><Measure " +
                    "Name=\"Method1\" Value=\"1\" /><Measure Name=\"Method2\" Value=\"2\" /><Measure " +
                    "Name=\"Method3\" Value=\"5\" /></Metric>").
                AddMetadata("ProviderName", "CodeMetricsProviderMock");

            ITaskItem[] metrics = new TaskItemMock[1];
            metrics[0] = new TaskItemMock("LinesOfCode").
                AddMetadata("ProviderName", "CodeMetricsProviderMock").
                AddMetadata("Ranges", "2;3").
                AddMetadata("Files", "C:\\foo.txt");

            FileStreamFactoryMock streamFactory = new FileStreamFactoryMock();

            CodeMetrics task = new CodeMetrics(streamFactory);
            task.BuildEngine = new BuildEngineMock();
            task.Providers = providers;
            task.OutputFileName = "report.xml";
            task.ShowDetailsReport = true;
            task.ShowSummaryReport = true;
            task.Metrics = metrics;
            task.FileOutput = true;
            Assert.AreEqual(true, task.Execute());

            streamFactory.OutputStream.Seek(0, SeekOrigin.Begin);
            MSBuildCodeMetricsReport report = MSBuildCodeMetricsReport.Deserialize(streamFactory.OutputStream);
            Assert.AreEqual(1, report.Details.Metrics.Count);
            Assert.AreEqual("LinesOfCode", report.Details.Metrics[0].MetricName);
            Assert.AreEqual("Method3", report.Details.Metrics[0].Measures[0].MeasureName);
            Assert.AreEqual(5, report.Details.Metrics[0].Measures[0].Value);
        }
        public void Initialize()
        {
            _buildEngine = new BuildEngineMock();
            _task = new CodeMetrics();
            _task.BuildEngine = _buildEngine;

            _metrics = new TaskItemMock[1];
            _metrics[0] = new TaskItemMock("LinesOfCode").
                AddMetadata("ProviderName", "CodeMetricsProviderMock").
                AddMetadata("Ranges", "2;3").
                AddMetadata("Files", "foo");

            _providers = new TaskItemMock[1];
            _providers[0] = new TaskItemMock(
                "MSBuildCodeMetrics.Core.UnitTests.Mock.CodeMetricsProviderSingleFileMock, MSBuildCodeMetrics.Core.UnitTests").
                AddMetadata("ProviderName", "CodeMetricsProviderMock").
                AddMetadata("Metrics", "LinesOfCode");
        }
        public void ExecuteTest()
        {
            string metricsExePath =
                Environment.GetEnvironmentVariable("VS120COMNTOOLS") + @"..\..\Team Tools\Static Analysis Tools\FxCop\Metrics.exe";
            string currentPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;

            ITaskItem[] providers = new TaskItemMock[1];
            providers[0] = new TaskItemMock("MSBuildCodeMetrics.VisualStudioMetrics.VisualStudioCodeMetricsProvider, MSBuildCodeMetrics.VisualStudioMetrics").
                AddMetadata("TempDir", currentPath).
                AddMetadata("MetricsExePath", metricsExePath);

            ITaskItem[] metrics = new TaskItemMock[2];
            metrics[0] = new TaskItemMock("CyclomaticComplexity").
                AddMetadata("ProviderName", "VisualStudioMetrics").
                AddMetadata("Ranges", "5;10").
                AddMetadata("Files", currentPath + @"\MSBuildCodeMetrics.Core.dll");
            metrics[1] = new TaskItemMock("LinesOfCode").
                AddMetadata("ProviderName", "VisualStudioMetrics").
                AddMetadata("Ranges", "50;100").
                AddMetadata("Files", currentPath + @"\MSBuildCodeMetrics.Core.dll");

            ITaskItem[] inputFiles = new TaskItemMock[1];
            inputFiles[0] = new TaskItemMock("MSBuildCodeMetrics.Core.dll").AddMetadata("FullPath", currentPath + @"\MSBuildCodeMetrics.Core.dll");

            CodeMetrics task = new CodeMetrics();
            task.BuildEngine = new BuildEngineMock();
            task.Providers = providers;
            task.OutputFileName = "report.xml";
            task.ShowDetailsReport = true;
            task.ShowSummaryReport = true;
            task.Metrics = metrics;
            task.FileOutput = true;
            Assert.IsTrue(task.Execute());

            FileStream fs = new FileStream("report.xml", FileMode.Open, FileAccess.Read);
            MSBuildCodeMetricsReport report;
            using (fs)
                report = MSBuildCodeMetricsReport.Deserialize(fs);

            Assert.AreEqual("CyclomaticComplexity", report.Details.Metrics[0].MetricName);
            var measures = report.Details.Metrics[0].Measures.Where(me => me.MeasureName == "MSBuildCodeMetrics.Core.CodeMetricsRunner.RegisterProvider(ICodeMetricsProvider) : void");
            Assert.AreEqual(1, measures.Count());
            Assert.AreEqual(1, measures.ToList()[0].Value);
        }
 public void WhenSettingRangeWithInvalidValueShouldReturnFalseAndSetError()
 {
     var buildEngineMock = new Mock<IBuildEngine>();
     var task = new CodeMetrics()
     {
         BuildEngine = buildEngineMock.Object,
         Metrics = new ITaskItem[]
         {
             _metrics[0] = new TaskItemMock("LinesOfCode").
                 AddMetadata("ProviderName", "CodeMetricsProviderMock").
                 AddMetadata("Ranges", "abc;abc").
                 AddMetadata("Files", "foo")
         }
     };
     task.Execute();
 }