diff --git a/main/OpenCover.Framework/Utility/CodeCoverageStringTextSource.cs b/main/OpenCover.Framework/Utility/CodeCoverageStringTextSource.cs index d31d2fb6e..d8d95b298 100644 --- a/main/OpenCover.Framework/Utility/CodeCoverageStringTextSource.cs +++ b/main/OpenCover.Framework/Utility/CodeCoverageStringTextSource.cs @@ -96,62 +96,54 @@ public CodeCoverageStringTextSource(string source, string filePath) private LineInfo[] InitLines () { - var lineInfoList = new List(); int offset = 0; int counter = 0; - bool newLine = false; - bool cr = false; - bool lf = false; - const ushort carriageReturn = 0xD; - const ushort lineFeed = 0xA; - LineInfo line; + var lineInfoList = new List(); foreach (var ch in _textSource) { - switch ((ushort)ch) { - case carriageReturn: - if (lf || cr) { - lf = false; - newLine = true; // cr after cr|lf - } else { - cr = true; // cr found - } - break; - case lineFeed: - if (lf) { - newLine = true; // lf after lf - } else { - lf = true; // lf found - } - break; - default: - if (cr || lf) { - cr = false; - lf = false; - newLine = true; // any non-line-end char after any line-end - } - break; - } - if (newLine) { // newLine detected - add line - newLine = false; - line = new LineInfo - { - Offset = offset, - Length = counter - offset - }; - lineInfoList.Add(line); + if (NextChar(ch)) { // newLine detected - add line + lineInfoList.Add(new LineInfo { Offset = offset, Length = counter - offset }); offset = counter; } ++counter; } // Add last line - line = new LineInfo - { - Offset = offset, - Length = counter - offset - }; - lineInfoList.Add(line); + lineInfoList.Add(new LineInfo { Offset = offset, Length = counter - offset }); return lineInfoList.ToArray(); } + private const ushort carriageReturn = 0xD; + private const ushort lineFeed = 0xA; + + private bool cr = false; + private bool lf = false; + + private bool NextChar(ushort ch) + { + switch (ch) { + case carriageReturn: + if (lf || cr) { + lf = false; // cr after cr|lf + return true; + } + cr = true; // cr found + break; + case lineFeed: + if (lf) { // lf after lf + return true; + } + lf = true; // lf found + break; + default: + if (cr || lf) { // any non-line-end char after any line-end + cr = false; + lf = false; + return true; + } + break; + } + return false; + } + /// Return text/source using SequencePoint line/col info /// /// diff --git a/main/OpenCover.Test/Framework/Utility/CodeCoverageStringTextSourceTest.cs b/main/OpenCover.Test/Framework/Utility/CodeCoverageStringTextSourceTest.cs index 9e98d75a6..60a7292a7 100644 --- a/main/OpenCover.Test/Framework/Utility/CodeCoverageStringTextSourceTest.cs +++ b/main/OpenCover.Test/Framework/Utility/CodeCoverageStringTextSourceTest.cs @@ -390,6 +390,7 @@ public void GetSource() Assert.True (source.FileTime == DateTime.MinValue); // arrange + var timeReference = DateTime.Now; System.IO.File.WriteAllLines(cSharpFileName, lines); // act on existing file source = CodeCoverageStringTextSource.GetSource(cSharpFileName); @@ -400,11 +401,16 @@ public void GetSource() Assert.True (source.FilePath == cSharpFileName); Assert.True (source.FileFound); Assert.True (source.FileTime == System.IO.File.GetLastWriteTime (cSharpFileName)); + Assert.False (source.IsChanged (source.FileTime)); + Assert.False (source.IsChanged (DateTime.MinValue)); + Assert.False (source.IsChanged (DateTime.Now)); + Assert.True (source.IsChanged (timeReference)); // destroy temp file System.IO.File.Delete(cSharpFileName); // arrange + timeReference = DateTime.Now; System.IO.File.WriteAllLines(vBasicFileName, lines); // act on existing file source = CodeCoverageStringTextSource.GetSource(vBasicFileName); @@ -415,6 +421,10 @@ public void GetSource() Assert.True (source.FilePath == vBasicFileName); Assert.True (source.FileFound); Assert.True (source.FileTime == System.IO.File.GetLastWriteTime (vBasicFileName)); + Assert.False (source.IsChanged (source.FileTime)); + Assert.False (source.IsChanged (DateTime.MinValue)); + Assert.False (source.IsChanged (DateTime.Now)); + Assert.True (source.IsChanged (timeReference)); // destroy temp file System.IO.File.Delete(vBasicFileName);