< Summary

Class:Azure.Data.Tables.Queryable.WebConvert
Assembly:Azure.Data.Tables
File(s):C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\Queryable\WebConvert.cs
Covered lines:49
Uncovered lines:20
Coverable lines:69
Total lines:176
Line coverage:71% (49 of 69)
Covered branches:39
Total branches:58
Branch coverage:67.2% (39 of 58)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
ConvertByteArrayToKeyString(...)-100%100%
IsKeyTypeQuoted(...)-100%50%
TryKeyPrimitiveToString(...)-85%83.33%
TryXmlPrimitiveToString(...)-55.56%56.25%
AppendDecimalMarkerToDouble(...)-75%75%

File(s)

C:\Git\azure-sdk-for-net\sdk\tables\Azure.Data.Tables\src\Queryable\WebConvert.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Diagnostics;
 6using System.Text;
 7using System.Xml;
 8
 9namespace Azure.Data.Tables.Queryable
 10{
 11    internal static class WebConvert
 12    {
 13        private const string HexValues = "0123456789ABCDEF";
 14
 15        internal static string ConvertByteArrayToKeyString(byte[] byteArray)
 16        {
 3617            StringBuilder hexBuilder = new StringBuilder(3 + (byteArray.Length * 2));
 3618            hexBuilder.Append(XmlConstants.XmlBinaryPrefix);
 3619            hexBuilder.Append("'");
 30420            for (int i = 0; i < byteArray.Length; i++)
 21            {
 11622                hexBuilder.Append(HexValues[byteArray[i] >> 4]);
 11623                hexBuilder.Append(HexValues[byteArray[i] & 0x0F]);
 24            }
 25
 3626            hexBuilder.Append("'");
 3627            return hexBuilder.ToString();
 28        }
 29
 30        internal static bool IsKeyTypeQuoted(Type type)
 31        {
 32            Debug.Assert(type != null, "type != null");
 52033            return type == typeof(System.Xml.Linq.XElement) || type == typeof(string);
 34        }
 35
 36        internal static bool TryKeyPrimitiveToString(object value, out string result)
 37        {
 38            Debug.Assert(value != null, "value != null");
 80839            Type t = value.GetType();
 80840            if (t == typeof(byte[]))
 41            {
 3642                result = ConvertByteArrayToKeyString((byte[])value);
 43            }
 44            else
 45            {
 77246                if (!TryXmlPrimitiveToString(value, out result))
 47                {
 048                    return false;
 49                }
 50
 51                Debug.Assert(result != null, "result != null");
 77252                if (t == typeof(DateTime))
 53                {
 6054                    result = XmlConstants.LiteralPrefixDateTime + "'" + result + "'";
 55                }
 71256                else if (t == typeof(decimal))
 57                {
 058                    result += XmlConstants.XmlDecimalLiteralSuffix;
 59                }
 71260                else if (t == typeof(Guid))
 61                {
 2062                    result = XmlConstants.LiteralPrefixGuid + "'" + result + "'";
 63                }
 69264                else if (t == typeof(long))
 65                {
 13666                    result += XmlConstants.XmlInt64LiteralSuffix;
 67                }
 55668                else if (t == typeof(float))
 69                {
 070                    result += XmlConstants.XmlSingleLiteralSuffix;
 71                }
 55672                else if (t == typeof(double))
 73                {
 3674                    result = AppendDecimalMarkerToDouble(result);
 75                }
 52076                else if (IsKeyTypeQuoted(t))
 77                {
 30478                    result = "'" + result.Replace("'", "''") + "'";
 79                }
 80            }
 81
 80882            return true;
 83        }
 84
 85        internal static bool TryXmlPrimitiveToString(object value, out string result)
 86        {
 87            Debug.Assert(value != null, "value != null");
 88
 77289            Type valueType = value.GetType();
 77290            valueType = Nullable.GetUnderlyingType(valueType) ?? valueType;
 91
 77292            if (typeof(string) == valueType)
 93            {
 30494                result = (string)value;
 95            }
 46896            else if (typeof(bool) == valueType)
 97            {
 7298                result = XmlConvert.ToString((bool)value);
 99            }
 396100            else if (typeof(byte) == valueType)
 101            {
 0102                result = XmlConvert.ToString((byte)value);
 103            }
 396104            else if (typeof(DateTime) == valueType)
 105            {
 60106                result = XmlConvert.ToString((DateTime)value, XmlDateTimeSerializationMode.RoundtripKind);
 107            }
 336108            else if (typeof(decimal) == valueType)
 109            {
 0110                result = XmlConvert.ToString((decimal)value);
 111            }
 336112            else if (typeof(double) == valueType)
 113            {
 36114                result = XmlConvert.ToString((double)value);
 115            }
 300116            else if (typeof(Guid) == valueType)
 117            {
 20118                result = value.ToString();
 119            }
 280120            else if (typeof(short) == valueType)
 121            {
 0122                result = XmlConvert.ToString((short)value);
 123            }
 280124            else if (typeof(int) == valueType)
 125            {
 144126                result = XmlConvert.ToString((int)value);
 127            }
 136128            else if (typeof(long) == valueType)
 129            {
 136130                result = XmlConvert.ToString((long)value);
 131            }
 0132            else if (typeof(sbyte) == valueType)
 133            {
 0134                result = XmlConvert.ToString((sbyte)value);
 135            }
 0136            else if (typeof(float) == valueType)
 137            {
 0138                result = XmlConvert.ToString((float)value);
 139            }
 0140            else if (typeof(byte[]) == valueType)
 141            {
 0142                byte[] byteArray = (byte[])value;
 0143                result = Convert.ToBase64String(byteArray);
 144            }
 0145            else if (ClientConvert.IsBinaryValue(value))
 146            {
 0147                return ClientConvert.TryKeyBinaryToString(value, out result);
 148            }
 0149            else if (typeof(System.Xml.Linq.XElement) == valueType)
 150            {
 0151                result = ((System.Xml.Linq.XElement)value).ToString(System.Xml.Linq.SaveOptions.None);
 152            }
 153            else
 154            {
 0155                result = null;
 0156                return false;
 157            }
 158
 159            Debug.Assert(result != null, "result != null");
 772160            return true;
 161        }
 162
 163        private static string AppendDecimalMarkerToDouble(string input)
 164        {
 188165            foreach (char c in input)
 166            {
 76167                if (!char.IsDigit(c))
 168                {
 36169                    return input;
 170                }
 171            }
 172
 0173            return input + ".0";
 174        }
 175    }
 176}