< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.FaultReason
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/FaultReason.cs
Line coverage
67%
Covered lines: 37
Uncovered lines: 18
Coverable lines: 55
Total lines: 150
Line coverage: 67.2%
Branch coverage
56%
Covered branches: 17
Total branches: 30
Branch coverage: 56.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)0%220%
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)70%101081.25%
Init(...)100%11100%
Init(...)100%11100%
GetMatchingTranslation()100%11100%
GetMatchingTranslation(...)56.25%161652.63%
ToString()50%2266.66%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/FaultReason.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.Collections.ObjectModel;
 7using System.Globalization;
 8
 9namespace CoreWCF
 10{
 11    public class FaultReason
 12    {
 013        public FaultReason(FaultReasonText translation)
 14        {
 015            if (translation == null)
 16            {
 017                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(translation));
 18            }
 19
 020            Init(translation);
 021        }
 22
 24223        public FaultReason(string text)
 24        {
 25            // Let FaultReasonText constructor throw
 24226            Init(new FaultReasonText(text));
 24227        }
 28
 6929        internal FaultReason(string text, CultureInfo cultureInfo)
 30        {
 31            // Let FaultReasonText constructor throw
 6932            Init(new FaultReasonText(text, cultureInfo));
 6933        }
 34
 9235        public FaultReason(IEnumerable<FaultReasonText> translations)
 36        {
 9237            if (translations == null)
 38            {
 039                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(translations));
 40            }
 41
 9242            int count = 0;
 55243            foreach (FaultReasonText faultReasonText in translations)
 44            {
 18445                count++;
 46            }
 47
 9248            if (count == 0)
 49            {
 050                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(translations),
 051                    SR.AtLeastOneFaultReasonMustBeSpecified);
 52            }
 53
 9254            FaultReasonText[] array = new FaultReasonText[count];
 9255            int index = 0;
 55256            foreach (FaultReasonText faultReasonText in translations)
 57            {
 18458                array[index++] = faultReasonText ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(
 18459                        SR.NoNullTranslations);
 60            }
 9261            Init(array);
 9262        }
 63
 64        private void Init(FaultReasonText translation)
 65        {
 31166            Init(new FaultReasonText[] { translation });
 31167        }
 68
 69        private void Init(FaultReasonText[] translations)
 70        {
 40371            Translations = new ReadOnlyCollection<FaultReasonText>(translations);
 40372        }
 73
 74        public FaultReasonText GetMatchingTranslation()
 75        {
 576            return GetMatchingTranslation(CultureInfo.CurrentCulture);
 77        }
 78
 79        public FaultReasonText GetMatchingTranslation(CultureInfo cultureInfo)
 80        {
 5281            if (cultureInfo == null)
 82            {
 083                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(cultureInfo));
 84            }
 85
 86            // If there's only one translation, use it
 5287            if (Translations.Count == 1)
 88            {
 4889                return Translations[0];
 90            }
 91
 92            // Search for an exact match
 2493            for (int i = 0; i < Translations.Count; i++)
 94            {
 895                if (Translations[i].Matches(cultureInfo))
 96                {
 097                    return Translations[i];
 98                }
 99            }
 100
 101            // If no exact match is found, proceed by looking for the a translation with a language that is a parent of 
 102
 4103            if (Translations.Count == 0)
 104            {
 0105                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
 0106                    new ArgumentException(SR.NoMatchingTranslationFoundForFaultText));
 107            }
 108
 109            // Search for a more general language
 4110            string localLang = cultureInfo.Name;
 0111            while (true)
 112            {
 4113                int idx = localLang.LastIndexOf('-');
 114
 115                // We don't want to accept xml:lang=""
 4116                if (idx == -1)
 117                {
 118                    break;
 119                }
 120
 121                // Clip off the last subtag and look for a match
 0122                localLang = localLang.Substring(0, idx);
 123
 0124                for (int i = 0; i < Translations.Count; i++)
 125                {
 0126                    if (Translations[i].XmlLang == localLang)
 127                    {
 0128                        return Translations[i];
 129                    }
 130                }
 131            }
 132
 133            // Return the first translation if no match is found
 4134            return Translations[0];
 135        }
 136
 137        // public on full framework, but exposes a SynchronizedReadOnlyCollection
 1495138        internal ReadOnlyCollection<FaultReasonText> Translations { get; private set; }
 139
 140        public override string ToString()
 141        {
 5142            if (Translations.Count == 0)
 143            {
 0144                return string.Empty;
 145            }
 146
 5147            return GetMatchingTranslation().Text;
 148        }
 149    }
 150}