Beispiel #1
0
        /// <summary>
        /// Performs the analysis of the source file.
        /// </summary>
        /// <param name="fileReader">The file reader.</param>
        /// <returns>The analysis result.</returns>
        internal FileAnalysis AnalyzeFile(IFileReader fileReader)
        {
            string error = null;

            string[] lines = fileReader.LoadFile(this.Path, out error);

            if (error != null)
            {
                Logger.Error(error);
                return(new FileAnalysis(this.Path, error));
            }

            this.TotalLines = lines.Length;

            int currentLineNumber = 0;

            var result = new FileAnalysis(this.Path);
            ICollection <Branch> branchesOfLine = null;

            foreach (var line in lines)
            {
                currentLineNumber++;
                int             visits          = this.lineCoverage.Length > currentLineNumber ? this.lineCoverage[currentLineNumber] : -1;
                LineVisitStatus lineVisitStatus = this.lineVisitStatus.Length > currentLineNumber ? this.lineVisitStatus[currentLineNumber] : LineVisitStatus.NotCoverable;

                var lineCoverageByTestMethod = this.lineCoveragesByTestMethod
                                               .ToDictionary(
                    l => l.Key,
                    l =>
                {
                    if (l.Value.Coverage.Length > currentLineNumber)
                    {
                        return(new ShortLineAnalysis(l.Value.Coverage[currentLineNumber], l.Value.LineVisitStatus[currentLineNumber]));
                    }
                    else
                    {
                        return(new ShortLineAnalysis(-1, LineVisitStatus.NotCoverable));
                    }
                });

                if (this.branches != null && this.branches.TryGetValue(currentLineNumber, out branchesOfLine))
                {
                    result.AddLineAnalysis(
                        new LineAnalysis(
                            visits,
                            lineVisitStatus,
                            lineCoverageByTestMethod,
                            currentLineNumber,
                            line.TrimEnd(),
                            branchesOfLine.Count(b => b.BranchVisits > 0),
                            branchesOfLine.Count));
                }
                else
                {
                    result.AddLineAnalysis(
                        new LineAnalysis(
                            visits,
                            lineVisitStatus,
                            lineCoverageByTestMethod,
                            currentLineNumber,
                            line.TrimEnd()));
                }
            }

            return(result);
        }
Beispiel #2
0
        /// <summary>
        /// Performs the analysis of the source file.
        /// </summary>
        /// <returns>The analysis result.</returns>
        internal FileAnalysis AnalyzeFile()
        {
            if (!System.IO.File.Exists(this.Path))
            {
                string error = string.Format(CultureInfo.InvariantCulture, " " + Resources.FileDoesNotExist, this.Path);
                Logger.Error(error);
                return(new FileAnalysis(this.Path, error));
            }

            try
            {
                string[] lines = System.IO.File.ReadAllLines(this.Path);

                this.TotalLines = lines.Length;

                int currentLineNumber = 0;

                var result = new FileAnalysis(this.Path);
                ICollection <Branch> branchesOfLine = null;

                foreach (var line in lines)
                {
                    currentLineNumber++;
                    int             visits          = this.lineCoverage.Length > currentLineNumber ? this.lineCoverage[currentLineNumber] : -1;
                    LineVisitStatus lineVisitStatus = this.lineVisitStatus.Length > currentLineNumber ? this.lineVisitStatus[currentLineNumber] : LineVisitStatus.NotCoverable;

                    var lineCoverageByTestMethod = this.lineCoveragesByTestMethod
                                                   .ToDictionary(
                        l => l.Key,
                        l =>
                    {
                        if (l.Value.Coverage.Length > currentLineNumber)
                        {
                            return(new ShortLineAnalysis(l.Value.Coverage[currentLineNumber], l.Value.LineVisitStatus[currentLineNumber]));
                        }
                        else
                        {
                            return(new ShortLineAnalysis(-1, LineVisitStatus.NotCoverable));
                        }
                    });

                    if (this.branches != null && this.branches.TryGetValue(currentLineNumber, out branchesOfLine))
                    {
                        result.AddLineAnalysis(
                            new LineAnalysis(
                                visits,
                                lineVisitStatus,
                                lineCoverageByTestMethod,
                                currentLineNumber,
                                line.TrimEnd(),
                                branchesOfLine.Count(b => b.BranchVisits > 0),
                                branchesOfLine.Count));
                    }
                    else
                    {
                        result.AddLineAnalysis(
                            new LineAnalysis(
                                visits,
                                lineVisitStatus,
                                lineCoverageByTestMethod,
                                currentLineNumber,
                                line.TrimEnd()));
                    }
                }

                return(result);
            }
            catch (IOException ex)
            {
                string error = string.Format(CultureInfo.InvariantCulture, " " + Resources.ErrorDuringReadingFile, this.Path, ex.Message);
                Logger.Error(error);
                return(new FileAnalysis(this.Path, error));
            }
            catch (UnauthorizedAccessException ex)
            {
                string error = string.Format(CultureInfo.InvariantCulture, " " + Resources.ErrorDuringReadingFile, this.Path, ex.Message);
                Logger.Error(error);
                return(new FileAnalysis(this.Path, error));
            }
        }