Ejemplo n.º 1
0
        /// <summary>
        /// Opens a coverage file..
        /// </summary>
        /// <param name="path">
        /// The path to the file to open. If null or empty, the user is prompted to select a file.
        /// </param>
        public void OpenFile(string path)
        {
            bool canceled = false;

            // if we were not given a path, let the user browse for one.
            if (string.IsNullOrEmpty(path))
            {
                //TODO (testing): hide this behind a service interface
                var ofd = new OpenFileDialog
                {
                    Filter      = "Coverage XML File (*.coveragexml)|*.coveragexml",
                    Multiselect = false,
                };

                if (!string.IsNullOrEmpty(Settings.Default.CoverageXmlDirectory))
                {
                    ofd.InitialDirectory = Settings.Default.CoverageXmlDirectory;
                }

                try
                {
                    ofd.FileOk += CheckFileOpenOK;

                    if (ofd.ShowDialog(Owner) == true)
                    {
                        path = ofd.FileName;

                        Settings.Default.CoverageXmlDirectory = Path.GetDirectoryName(path);
                        Settings.Default.Save();
                    }
                    else
                    {
                        // user canceled
                        canceled = true;
                    }
                }
                finally
                {
                    ofd.FileOk -= CheckFileOpenOK;
                }
            }

            if (!canceled)
            {
                var reader = new CoverageReader();

                var model = reader.ReadCoverageXml(path);
                model.AdditionalData["FullPath"] = path;

                CoverageRows.Add(new CoverageNodeViewModel(model));
            }
        }
        public void CanParseCoverageFile()
        {
            // Not a typical unit test but it makes sense to use an example real cobertura xml file and parse it, then assert everything that is expected
            // matches what is returned.
            // start off by asserting that the filename and class results are available, then assert the contents of each class result.

            // arrange
            string filename = "example.coverage.cobertura.xml";
            var    sut      = new CoverageReader();

            // act
            var coverageResults = sut.ReadFile(filename);

            // Assert
            coverageResults.FileName.Should().Be(filename);
            coverageResults.ClassResults.Should().NotBeNull();
            coverageResults.ClassResults.Count.Should().Be(3);

            var calendarCoverage = coverageResults.ClassResults["Calendar\\Calendar.cs"];

            calendarCoverage.Should().NotBeNull();
            calendarCoverage.FileName.Should().Be("Calendar\\Calendar.cs");

            var calculatorCoverage = coverageResults.ClassResults["Calculator\\Calculator.cs"];

            calculatorCoverage.Should().NotBeNull();
            calculatorCoverage.FileName.Should().Be("Calculator\\Calculator.cs");

            var multiplierCoverage = coverageResults.ClassResults["Calculator\\Multiplier.cs"];

            multiplierCoverage.Should().NotBeNull();
            multiplierCoverage.FileName.Should().Be("Calculator\\Multiplier.cs");

            this.AssertCalendarResults(calendarCoverage);
            this.AssertCalculatorResults(calculatorCoverage);
            this.AssertMultiplierResults(multiplierCoverage);
        }