< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.EncodingConverter
Assembly: CoreWCF.WebHttp
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/EncodingConverter.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 18
Coverable lines: 18
Total lines: 68
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
CanConvertFrom(...)0%220%
CanConvertTo(...)0%220%
ConvertFrom(...)0%660%
ConvertTo(...)0%440%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.WebHttp/src/CoreWCF/EncodingConverter.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.ComponentModel;
 6using System.ComponentModel.Design.Serialization;
 7using System.Text;
 8
 9namespace CoreWCF
 10{
 11    internal class EncodingConverter : TypeConverter
 12    {
 13        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
 14        {
 015            if (typeof(string) == sourceType)
 16            {
 017                return true;
 18            }
 19
 020            return base.CanConvertFrom(context, sourceType);
 21        }
 22
 23        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
 24        {
 025            if (typeof(InstanceDescriptor) == destinationType)
 26            {
 027                return true;
 28            }
 29
 030            return base.CanConvertTo(context, destinationType);
 31        }
 32
 33        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, obj
 34        {
 035            if (value is string encoding)
 36            {
 37                Encoding retval;
 038                if (string.Compare(encoding, "utf-8", StringComparison.OrdinalIgnoreCase) == 0)
 39                {
 40                    // special case for utf-8 to match with what we do in the default text encoding
 041                    retval = Encoding.UTF8;
 42                }
 43                else
 44                {
 045                    retval = Encoding.GetEncoding(encoding);
 46                }
 047                if (retval == null)
 48                {
 049                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(value), SR.ConfigInvalidEncoding
 50                }
 051                return retval;
 52            }
 53
 054            return base.ConvertFrom(context, culture, value);
 55        }
 56
 57        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, objec
 58        {
 059            if (typeof(string) == destinationType && value is Encoding)
 60            {
 061                Encoding encoding = (Encoding)value;
 062                return encoding.HeaderName;
 63            }
 64
 065            return base.ConvertTo(context, culture, value, destinationType);
 66        }
 67    }
 68}