public void ConstructWithEmptyString()
        {
            // arrange
            var source = new CodeCoverageStringTextSource(string.Empty, "");
            
            // assert
            Assert.True (source.LinesCount == 0);
            
            // act
            var result = source.GetLine(1); // not existing line index
            
            // assert
            Assert.True (result == string.Empty);
            
            // act
            result = source.GetLine(0); // invalid line index
            
            // assert
            Assert.True (result == string.Empty);

            // act
            var sp = new SequencePoint { StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 6};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == string.Empty);

            // act
            sp = new SequencePoint { StartLine = -1, StartColumn = -1, EndLine = -2, EndColumn = 6};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == string.Empty);
        }
        static internal void ResetAfterLoading()
        {
            var points = InstrumentPoints
                .Where(x => x != null)
                .GroupBy(x => x.UniqueSequencePoint)
                .Select(g => g.OrderBy(x => x.OrigSequencePoint).First())
                .ToList();

            var max = (int)points.Max(x => x.UniqueSequencePoint);
            
            InstrumentPoints.Clear();
            InstrumentPoints.Add(null);

            for (var i = 1; i <= max; i++)
            {
                var point = new SequencePoint();
                InstrumentPoints[i] = point;
                point.UniqueSequencePoint = (uint)i;
            }

            foreach (var instrumentationPoint in points)
            {
                InstrumentPoints[(int)instrumentationPoint.UniqueSequencePoint] = instrumentationPoint;
            }

            _instrumentPoint = max;
        }
        public void DoesEqualSimilar()
        {
            var point1 = new SequencePoint {FileId = 1, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1};
            var point2 = new SequencePoint {FileId = 1, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1};

            Assert.IsTrue(comparer.Equals(point1, point2));
        }
        private void GetSequencePointsForToken(int token, List <SequencePoint> list)
        {
            var methodDefinition = GetMethodDefinition(token);

            if (methodDefinition == null)
            {
                return;
            }
            UInt32 ordinal = 0;

            foreach (var instruction in methodDefinition.Body.Instructions)
            {
                if (instruction.SequencePoint != null &&
                    instruction.SequencePoint.StartLine != StepOverLineCode)
                {
                    var sp    = instruction.SequencePoint;
                    var point = new SequencePoint()
                    {
                        EndColumn   = sp.EndColumn,
                        EndLine     = sp.EndLine,
                        Offset      = instruction.Offset,
                        Ordinal     = ordinal++,
                        StartColumn = sp.StartColumn,
                        StartLine   = sp.StartLine,
                        Document    = sp.Document.Url,
                    };
                    list.Add(point);
                }
            }
        }
        public void DoesNotEqualDisimilar(int fileId, int startLine, int startColumn, int endLine, int endColumn)
        {
            var point1 = new SequencePoint { FileId = 1, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1 };
            var point2 = new SequencePoint
            {
                FileId = (uint)fileId,
                StartLine = startLine,
                StartColumn = startColumn,
                EndLine = endLine,
                EndColumn = endColumn
            };

            Assert.IsFalse(comparer.Equals(point1, point2));
            
        }
        public void UsageThatCoversGetHashCode() {

            var sequencePointsSet = new HashSet<SequencePoint>(comparer);
            var point1 = new SequencePoint {FileId = 1, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1};
            var point2 = new SequencePoint {FileId = 1, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1};
            var point3 = new SequencePoint {FileId = 2, StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 1};

            Assert.True (sequencePointsSet.Add(point1));
            Assert.False (sequencePointsSet.Add(point1));

            Assert.True (sequencePointsSet.Contains(point2));
            Assert.False (sequencePointsSet.Add(point2));

            Assert.False (sequencePointsSet.Contains(point3));
            Assert.True (sequencePointsSet.Add(point3));

        }
        public void ConstructWithFiveLines()
        {
            // arrange
            const string input = "\tfirst line\n \n\tthird line\r\n \r   fifth line\r";
            var source = new CodeCoverageStringTextSource(input, "");
            
            // assert
            Assert.True (source.LinesCount == 5);

            // act
            var result = source.GetLine(1); // existing line index
            
            // assert
            Assert.True (result == "\tfirst line\n");
            
            // act
            result = source.GetLine(2); // existing line index
            
            // assert
            Assert.True (result == " \n");
            
            // act
            result = source.GetLine(3); // existing line index
            
            // assert
            Assert.True (result == "\tthird line\r\n");
            
            // act
            result = source.GetLine(4); // existing line index
            
            // assert
            Assert.True (result == " \r");
            
            // act
            result = source.GetLine(5); // existing line index
            
            // assert
            Assert.True (result == "   fifth line\r");
            
            // act
            result = source.GetLine(9); // invalid line index

            // assert
            Assert.True (result == string.Empty);
            
            // act third line request
            var sp = new SequencePoint { StartLine = 3, StartColumn = 8, EndLine = 3, EndColumn = 12};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == "line");
            
            // act invalid two lines request
            sp = new SequencePoint { StartLine = 1, StartColumn = 8, EndLine = 2, EndColumn = 13};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == "line\n \n");
            
            // act valid two lines request
            sp = new SequencePoint { StartLine = 1, StartColumn = 8, EndLine = 2, EndColumn = 2};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == "line\n ");
            
            // act three lines request
            sp = new SequencePoint { StartLine = 1, StartColumn = 8, EndLine = 3, EndColumn = 12};
            result = source.GetText(sp);
            
            // assert
            Assert.True (result == "line\n \n\tthird line");
        }
Beispiel #8
0
 /// <summary>
 /// Is Start/End Line/Column equal
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 public bool IsPositionEqual (SequencePoint sp) {
     return sp != null && IsLineEqual (sp) && IsColumnEqual (sp);
 }
 public void ConstructWithTwoLines()
 {
     // arrange
     const string input = "\tfirst line\n\tsecond line\r";
     var source = new CodeCoverageStringTextSource(input, "");
     
     // assert
     Assert.True (source.LinesCount == 2);
     
     // act with existing line index
     var result = source.GetLine(1);
     
     // assert
     Assert.True (result == "\tfirst line\n");
     
     // act with existing line index
     result = source.GetLine(2);
     
     // assert
     Assert.True (result == "\tsecond line\r");
     
     // act with invalid line index
     result = source.GetLine(0);
     
     // assert
     Assert.True (result == string.Empty);
     
     // act
     var sp = new SequencePoint { StartLine = 2, StartColumn = 9, EndLine = 2, EndColumn = 13};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "line");
     
     // act with two lines request
     sp = new SequencePoint { StartLine = 1, StartColumn = 8, EndLine = 2, EndColumn = 13};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "line\n\tsecond line");
     
     // act with extended two lines request
     sp = new SequencePoint { StartLine = 1, StartColumn = -8, EndLine = 2, EndColumn = 30};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "\tfirst line\n\tsecond line\r");
     
     // act with invalid first line request
     sp = new SequencePoint { StartLine = 1, StartColumn = 28, EndLine = 2, EndColumn = 30};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "\tsecond line\r");
     
     // act with invalid first line and invalid second line request
     sp = new SequencePoint { StartLine = 1, StartColumn = 28, EndLine = 2, EndColumn = 0};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "");
 }
 public void ConstructWithTwoLinesNoCrLfAtEof()
 {
     // arrange
     const string input = "\tfirst line\r\tsecond line";
     var source = new CodeCoverageStringTextSource(input, "");
     
     // assert
     Assert.True (source.LinesCount == 2);
     
     // act
     var result = source.GetLine(1); // existing line index
     
     // assert
     Assert.True (result == "\tfirst line\r");
     
     // act
     result = source.GetLine(2); // existing line index
     
     // assert
     Assert.True (result == "\tsecond line");
     
     // act
     result = source.GetLine(0); // invalid line index
     
     // assert
     Assert.True (result == string.Empty);
     
     // act on first line
     var sp = new SequencePoint { StartLine = 1, StartColumn = 8, EndLine = 1, EndColumn = 12};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "line");
     
     // act on second line
     sp = new SequencePoint { StartLine = 2, StartColumn = 9, EndLine = 2, EndColumn = 13};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "line");
 }
Beispiel #11
0
 /// <summary>
 /// Is FileId equal? (If FileId is 0 then file is unknown)
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 public bool IsFileIdEqual(SequencePoint sp)
 {
     return(sp != null && FileId != 0 && FileId == sp.FileId);
 }
		/// <summary>
		/// Adds the word spans for sequence points covering multiple lines.
		/// </summary>
		/// <param name="snapshot">The snapshot.</param>
		/// <param name="wordSpans">The word spans.</param>
		/// <param name="sequencePoint">The sequence point.</param>
		/// <param name="sequencePointStartLine">The sequence point start line.</param>
		/// <param name="sequencePointEndLine">The sequence point end line.</param>
		/// <param name="covered">if set to <c>true</c> [covered].</param>
        protected void AddWordSpansForSequencePointsCoveringMultipleLines(ITextSnapshot snapshot, 
																		List<SnapshotSpan> wordSpans, 
																		SequencePoint sequencePoint, 
																		int sequencePointStartLine, 
																		int sequencePointEndLine,
																		bool covered)
		{
			int totalCharacters = 0;

			var selectedLines = snapshot.Lines
										.Where(line => line.LineNumber >= sequencePointStartLine &&
														line.LineNumber <= sequencePointEndLine);

			foreach (var selectedLine in selectedLines)
			{
				if (selectedLine.LineNumber == sequencePointStartLine)
				{
					totalCharacters = selectedLine.Length - sequencePoint.StartColumn + 1;

					AddWordSpan(wordSpans, snapshot, selectedLine.Extent.Start.Position + sequencePoint.StartColumn - 1, totalCharacters, covered);
				}
				else if (selectedLine.LineNumber == sequencePointEndLine)
				{
					var temp = selectedLine.Length - (sequencePoint.EndColumn - 1);
					totalCharacters = selectedLine.Length - temp;

					AddWordSpan(wordSpans, snapshot, selectedLine.Extent.Start.Position, totalCharacters, covered);
				}
				else
				{
					AddWordSpan(wordSpans, snapshot, selectedLine.Extent.Start.Position, selectedLine.Length, covered);
				}
			}
		}
        public void CanLoadExistingFileWhenInitialising()
        {
            // arrange
            var moduleHash = Guid.NewGuid().ToString();
            var persistence = new FilePersistance(_mockCommandLine.Object, _mockLogger.Object);
            persistence.Initialise(_filePath, false);
            var point = new SequencePoint()
                // BranchPoints within SequencePoint shorther than 3 characters will be removed
                {StartLine = 1, EndLine = 1, StartColumn = 1, EndColumn = 4};
            var branchPoint = new BranchPoint{Path = 0, OffsetPoints = new List<int>()};
            var branchPoint2 = new BranchPoint { Path = 1, OffsetPoints = new List<int>{1,2}};
            var file = new OpenCover.Framework.Model.File();
            var filref = new FileRef() {UniqueId = file.UniqueId};

            persistence.PersistModule(new Module
            {
                Summary = new Summary {NumSequencePoints = 1},
                Files = new[] {file},
                ModuleHash = moduleHash,
                Classes = new[]
                {
                    new Class
                    {
                        Summary = new Summary {NumSequencePoints = 1},
                        Files = new[] {file},
                        Methods = new[]
                        {
                            new Method
                            {
                                FileRef = filref,
                                MetadataToken = 1234,
                                Summary = new Summary {NumSequencePoints = 1},
                                MethodPoint = point,
                                SequencePoints = new[] {point},
                                BranchPoints = new[] {branchPoint, branchPoint2}
                            }
                        }
                    }
                }
            });
            persistence.Commit();
            
            var persistence2 = new FilePersistance(_mockCommandLine.Object, _mockLogger.Object);

            // act
            persistence2.Initialise(_filePath, true);

            // assert
            Assert.IsNotNull(persistence2.CoverageSession);
            Assert.AreEqual(moduleHash, persistence2.CoverageSession.Modules[0].ModuleHash);
            Assert.AreEqual(point.UniqueSequencePoint, persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].SequencePoints[0].UniqueSequencePoint);
            Assert.AreEqual(point.UniqueSequencePoint, persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].MethodPoint.UniqueSequencePoint);
            var method = persistence2.CoverageSession.Modules[0].Classes[0].Methods[0];
            var br1 = persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].BranchPoints[0];
            var br2 = persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].BranchPoints[1];
            Assert.AreEqual(branchPoint.UniqueSequencePoint, br1.UniqueSequencePoint);
            Assert.AreEqual(branchPoint2.UniqueSequencePoint, br2.UniqueSequencePoint);
            Assert.AreEqual(0, br1.OffsetPoints.Count);
            Assert.AreEqual(2, br2.OffsetPoints.Count);
            Assert.AreEqual(1, br2.OffsetPoints[0]);
            Assert.AreEqual(2, br2.OffsetPoints[1]);

            // the method and sequence point if point to same offset need to merge
            Assert.AreSame(method.MethodPoint, method.SequencePoints[0]);

            // the loaded summary object needs to be cleared
            Assert.AreEqual(0, persistence2.CoverageSession.Summary.NumSequencePoints);
            Assert.AreEqual(0, persistence2.CoverageSession.Modules[0].Summary.NumSequencePoints);
            Assert.AreEqual(0, persistence2.CoverageSession.Modules[0].Classes[0].Summary.NumSequencePoints);
            Assert.AreEqual(0, persistence2.CoverageSession.Modules[0].Classes[0].Methods[0].Summary.NumSequencePoints);
        }
Beispiel #14
0
 /// <summary>
 /// SonnarQube wants no more than 3 boolean conditions
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 private bool IsColumnEqual (SequencePoint sp) {
     return StartColumn == sp.StartColumn && EndColumn == sp.EndColumn;
 }
Beispiel #15
0
 /// <summary>
 /// Is FileId equal? (If FileId is 0 then file is unknown)
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 public bool IsFileIdEqual (SequencePoint sp) {
     return sp != null && FileId != 0 && FileId == sp.FileId;
 }
 public void ConstructWithSingleLine()
 {
     // arrange
     const string input = "single line";
     var source = new CodeCoverageStringTextSource(input, "");
     
     // assert
     Assert.True (source.LinesCount == 1);
     
     // act
     var result = source.GetLine(1); // existing line index
     
     // assert
     Assert.True (result == input);
     
     // act
     result = source.GetLine(0); // invalid line index
     
     // assert
     Assert.True (result == string.Empty);
     
     // act
     var sp = new SequencePoint { StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 7};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "single");
     
     // act with too small StartColumn
     sp = new SequencePoint { StartLine = 1, StartColumn = -1, EndLine = 1, EndColumn = 7};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "single");
     
     // act with too large StartColumn
     sp = new SequencePoint { StartLine = 1, StartColumn = 19, EndLine = 1, EndColumn = 20};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "");
     
     // act with too small EndColumn
     sp = new SequencePoint { StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 0};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "");
     
     // act with too large EndColumn
     sp = new SequencePoint { StartLine = 1, StartColumn = 1, EndLine = 1, EndColumn = 20};
     result = source.GetText(sp);
     
     // assert
     Assert.True (result == "single line");
 }
Beispiel #17
0
 /// <summary>
 /// SonnarQube wants no more than 3 boolean conditions
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 private bool IsLineEqual (SequencePoint sp) {
     return StartLine == sp.StartLine && EndLine == sp.EndLine;
 }
        public void DoesNotEqualNull()
        {
            var point = new SequencePoint();

            Assert.IsFalse(comparer.Equals(point, null));
        }
        public void DoesEqualSelf()
        {
            var point = new SequencePoint();

            Assert.IsTrue(comparer.Equals(point, point));
        }
 /// <summary>Return text/source using SequencePoint line/col info
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 public string GetText(SequencePoint sp) {
     return GetText(sp.StartLine, sp.StartColumn, sp.EndLine, sp.EndColumn );
 }
Beispiel #21
0
 /// <summary>
 /// SonnarQube wants no more than 3 boolean conditions
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 private bool IsLineEqual(SequencePoint sp)
 {
     return(StartLine == sp.StartLine && EndLine == sp.EndLine);
 }
 private void GetSequencePointsForToken(int token, List<SequencePoint> list)
 {
     var methodDefinition = GetMethodDefinition(token);
     if (methodDefinition == null) return;
     UInt32 ordinal = 0;
     foreach (var instruction in methodDefinition.Body.Instructions)
     {
         if (instruction.SequencePoint != null &&
             instruction.SequencePoint.StartLine != StepOverLineCode)
         {
             var sp = instruction.SequencePoint;
             var point = new SequencePoint()
                             {
                                 EndColumn = sp.EndColumn,
                                 EndLine = sp.EndLine,
                                 Offset = instruction.Offset,
                                 Ordinal = ordinal++,
                                 StartColumn = sp.StartColumn,
                                 StartLine = sp.StartLine,
                             };
             list.Add(point);
         }
     }
 }
Beispiel #23
0
 /// <summary>
 /// SonnarQube wants no more than 3 boolean conditions
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 private bool IsColumnEqual(SequencePoint sp)
 {
     return(StartColumn == sp.StartColumn && EndColumn == sp.EndColumn);
 }
 private static void GetSequencePointsForToken(IEnumerable<TypeDefinition> typeDefinitions, int token, List<SequencePoint> list)
 {
     foreach (var typeDefinition in typeDefinitions)
     {
         foreach (var methodDefinition in
             typeDefinition.Methods
             .Where(methodDefinition => methodDefinition.MetadataToken.ToInt32() == token)
             .Where(methodDefinition => methodDefinition.Body != null && methodDefinition.Body.Instructions != null))
         {
             UInt32 ordinal = 0;
             foreach (var instruction in methodDefinition.Body.Instructions)
             {
                 if (instruction.SequencePoint != null &&
                     instruction.SequencePoint.StartLine != stepOverLineCode)
                 {
                     var sp = instruction.SequencePoint;
                     var point = new SequencePoint()
                                     {
                                         EndColumn = sp.EndColumn,
                                         EndLine = sp.EndLine,
                                         Offset = instruction.Offset,
                                         Ordinal = ordinal++,
                                         StartColumn = sp.StartColumn,
                                         StartLine = sp.StartLine,
                                     };
                     list.Add(point);
                 }
             }
         }
         if (typeDefinition.HasNestedTypes)
             GetSequencePointsForToken(typeDefinition.NestedTypes, token, list);
     }
 }
Beispiel #25
0
 /// <summary>
 /// Is Start/End Line/Column equal
 /// </summary>
 /// <param name="sp"></param>
 /// <returns></returns>
 public bool IsPositionEqual(SequencePoint sp)
 {
     return(sp != null && IsLineEqual(sp) && IsColumnEqual(sp));
 }