Exemple #1
0
 public static CoverageResult Merge(CoverageResult result1, CoverageResult result2)
 {
     return(result1 + result2);
 }
Exemple #2
0
        public CoverageResult MergeWith(CoverageResult otherResult)
        {
            if (otherResult == null)
            {
                return(this);
            }

            // Create an anonymous data that holds every parameter we need to construct the coverage result types
            var result = Assemblies.Select(asm => new
            {
                asm.Name,
                Types = asm.Types.Select(type => new
                {
                    type.FullName,
                    Methods = type.Methods.Select(meth => new
                    {
                        meth.FullName,
                        meth.Source,
                        meth.StartLine,
                        meth.StartColumn,
                        meth.EndLine,
                        meth.EndColumn,
                        meth.VisitCount
                    }).ToList()
                }).ToList()
            }).ToList()
            ;

            // Update the anonymous data with results from the second coverage result
            foreach (var asm in otherResult.Assemblies)
            {
                var targetAssembly = result.Find(a => a.Name == asm.Name);

                if (targetAssembly == null)
                {
                    // This assembly is not present in the result, add everything about it to the result
                    result.Add(new
                    {
                        asm.Name,
                        Types = asm.Types.Select(type => new
                        {
                            type.FullName,
                            Methods = type.Methods.Select(meth => new
                            {
                                meth.FullName,
                                meth.Source,
                                meth.StartLine,
                                meth.StartColumn,
                                meth.EndLine,
                                meth.EndColumn,
                                meth.VisitCount
                            }).ToList()
                        }).ToList()
                    });
                }
                else
                {
                    // Assembly is already present, check for types
                    foreach (var type in asm.Types)
                    {
                        var targetType = targetAssembly.Types.Find(a => a.FullName == type.FullName);
                        if (targetType == null)
                        {
                            // This type is not present in the result, add everything about it to the result
                            targetAssembly.Types.Add(new
                            {
                                type.FullName,
                                Methods = type.Methods.Select(meth => new
                                {
                                    meth.FullName,
                                    meth.Source,
                                    meth.StartLine,
                                    meth.StartColumn,
                                    meth.EndLine,
                                    meth.EndColumn,
                                    meth.VisitCount
                                }).ToList()
                            });
                        }
                        else
                        {
                            // Type is already present, check for methods
                            foreach (var meth in type.Methods)
                            {
                                var targetMethod = targetType.Methods.Find(a => a.FullName == meth.FullName);
                                if (targetMethod == null)
                                {
                                    // This method is not present in the result, add it to the result
                                    targetType.Methods.Add(new
                                    {
                                        meth.FullName,
                                        meth.Source,
                                        meth.StartLine,
                                        meth.StartColumn,
                                        meth.EndLine,
                                        meth.EndColumn,
                                        meth.VisitCount
                                    });
                                }
                                else
                                {
                                    // Method is already present, sum up the visit count of methods from result 1 and result 2
                                    targetType.Methods.Remove(targetMethod);
                                    targetType.Methods.Add(new
                                    {
                                        meth.FullName,
                                        meth.Source,
                                        meth.StartLine,
                                        meth.StartColumn,
                                        meth.EndLine,
                                        meth.EndColumn,
                                        VisitCount = meth.VisitCount + targetMethod.VisitCount
                                    });
                                }
                            }
                        }
                    }
                }
            }

            return(new CoverageResult(
                       Version,
                       UniqueId,
                       result.Select(
                           asm => new CoverageAssemblyResult(
                               asm.Name,
                               asm.Types.Select(type => new CoverageTypeResult(
                                                    type.FullName,
                                                    type.Methods.Select(meth => new CoverageMethodResult(
                                                                            meth.FullName,
                                                                            meth.Source,
                                                                            meth.StartLine,
                                                                            meth.StartColumn,
                                                                            meth.EndLine,
                                                                            meth.EndColumn,
                                                                            meth.VisitCount)).ToList().AsReadOnly()
                                                    )).ToList().AsReadOnly()
                               )).ToList().AsReadOnly()
                       ));
        }