< Summary

Class:Azure.Core.TestFramework.EnumValuesAttribute
Assembly:Azure.Core.TestFramework
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\EnumValuesAttribute.cs
Covered lines:35
Uncovered lines:0
Coverable lines:35
Total lines:121
Line coverage:100% (35 of 35)
Covered branches:45
Total branches:46
Branch coverage:97.8% (45 of 46)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
.ctor(...)-100%100%
get_Exclude()-100%100%
GetData(...)-100%50%
GetMembers(...)-100%100%
GetMembersImpl()-100%100%
Includes(...)-100%100%
Excludes(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core.TestFramework\src\EnumValuesAttribute.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections;
 6using System.Collections.Generic;
 7using System.Linq;
 8using System.Reflection;
 9using NUnit.Framework.Interfaces;
 10using NUnit.Framework.Internal;
 11
 12namespace Azure.Core.TestFramework
 13{
 14    /// <summary>
 15    /// Provides values to NUnit that are public, static, read-only fields and properties of the declared type.
 16    /// Use this in place of ValuesAttribute for enum-like structs.
 17    /// </summary>
 18    [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
 19    public class EnumValuesAttribute : Attribute, IParameterDataSource
 20    {
 21        private readonly string[] _names;
 22
 23        /// <summary>
 24        /// Creates a new instance of the <see cref="EnumValuesAttribute"/> class.
 25        /// All public, static, read-only fields and properties of the declared type are included.
 26        /// </summary>
 6927        public EnumValuesAttribute()
 28        {
 6929        }
 30
 31        /// <summary>
 32        /// Creates a new instance of the <see cref="EnumValuesAttribute"/> class.
 33        /// Only public, static, read-only fields and properties of the declared type with any of the specified <paramre
 34        /// </summary>
 2435        public EnumValuesAttribute(params string[] names)
 36        {
 2437            _names = names;
 2438        }
 39
 40        /// <summary>
 41        /// Gets or sets a list of field and properties names to exclude. Field and property names specified here will b
 42        /// </summary>
 135843        public string[] Exclude { get; set; }
 44
 9145        public IEnumerable GetData(IParameterInfo parameter) => GetMembers(parameter.ParameterType, parameter.ParameterI
 46
 47        public IEnumerable<object> GetMembers(Type parameterType, string parameterName)
 48        {
 9349            object[] data = GetMembersImpl(parameterType).ToArray();
 9350            if (data is null || data.Length == 0)
 51            {
 52                // NUnit handles this exception specifically to mark the test as failed or, in some cases, skipped.
 253                throw new InvalidDataSourceException(@$"No enumeration members found on parameter ""{parameterName}"".")
 54            }
 55
 9156            return data;
 57        }
 58
 59        private IEnumerable<object> GetMembersImpl(Type type)
 60        {
 61            const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly;
 62
 9363            if (type.IsValueType)
 64            {
 9165                PropertyInfo[] properties = type.GetProperties(bindingFlags);
 142466                for (int i = 0; i < properties.Length; ++i)
 67                {
 62168                    PropertyInfo property = properties[i];
 62169                    if (property.PropertyType == type && property.CanRead && !property.CanWrite && Includes(property.Nam
 70                    {
 45571                        yield return property.GetValue(null);
 72                    }
 73                }
 74
 9175                FieldInfo[] fields = type.GetFields(bindingFlags);
 11276                for (int i = 0; i < fields.Length; ++i)
 77                {
 4078                    FieldInfo field = fields[i];
 4079                    if (field.FieldType == type && (field.IsInitOnly || field.IsLiteral) && Includes(field.Name) && !Exc
 80                    {
 2081                        yield return field.GetValue(null);
 82                    }
 83                }
 9184            }
 9385        }
 86
 87        private bool Includes(string name)
 88        {
 64589            if (_names is null || _names.Length == 0)
 90            {
 46191                return true;
 92            }
 93
 120494            for (int i = 0; i < _names.Length; ++i)
 95            {
 48496                if (string.Equals(_names[i], name, StringComparison.Ordinal))
 97                {
 6698                    return true;
 99                }
 100            }
 101
 118102            return false;
 103        }
 104
 105        private bool Excludes(string name)
 106        {
 527107            if (Exclude != null)
 108            {
 992109                for (int i = 0; i < Exclude.Length; ++i)
 110                {
 316111                    if (string.Equals(Exclude[i], name, StringComparison.Ordinal))
 112                    {
 52113                        return true;
 114                    }
 115                }
 116            }
 117
 475118            return false;
 119        }
 120    }
 121}