| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections; |
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | | using System.Reflection; |
| | 9 | | using NUnit.Framework.Interfaces; |
| | 10 | | using NUnit.Framework.Internal; |
| | 11 | |
|
| | 12 | | namespace 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> |
| 69 | 27 | | public EnumValuesAttribute() |
| | 28 | | { |
| 69 | 29 | | } |
| | 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> |
| 24 | 35 | | public EnumValuesAttribute(params string[] names) |
| | 36 | | { |
| 24 | 37 | | _names = names; |
| 24 | 38 | | } |
| | 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> |
| 1358 | 43 | | public string[] Exclude { get; set; } |
| | 44 | |
|
| 91 | 45 | | public IEnumerable GetData(IParameterInfo parameter) => GetMembers(parameter.ParameterType, parameter.ParameterI |
| | 46 | |
|
| | 47 | | public IEnumerable<object> GetMembers(Type parameterType, string parameterName) |
| | 48 | | { |
| 93 | 49 | | object[] data = GetMembersImpl(parameterType).ToArray(); |
| 93 | 50 | | 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. |
| 2 | 53 | | throw new InvalidDataSourceException(@$"No enumeration members found on parameter ""{parameterName}"".") |
| | 54 | | } |
| | 55 | |
|
| 91 | 56 | | return data; |
| | 57 | | } |
| | 58 | |
|
| | 59 | | private IEnumerable<object> GetMembersImpl(Type type) |
| | 60 | | { |
| | 61 | | const BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly; |
| | 62 | |
|
| 93 | 63 | | if (type.IsValueType) |
| | 64 | | { |
| 91 | 65 | | PropertyInfo[] properties = type.GetProperties(bindingFlags); |
| 1424 | 66 | | for (int i = 0; i < properties.Length; ++i) |
| | 67 | | { |
| 621 | 68 | | PropertyInfo property = properties[i]; |
| 621 | 69 | | if (property.PropertyType == type && property.CanRead && !property.CanWrite && Includes(property.Nam |
| | 70 | | { |
| 455 | 71 | | yield return property.GetValue(null); |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 91 | 75 | | FieldInfo[] fields = type.GetFields(bindingFlags); |
| 112 | 76 | | for (int i = 0; i < fields.Length; ++i) |
| | 77 | | { |
| 40 | 78 | | FieldInfo field = fields[i]; |
| 40 | 79 | | if (field.FieldType == type && (field.IsInitOnly || field.IsLiteral) && Includes(field.Name) && !Exc |
| | 80 | | { |
| 20 | 81 | | yield return field.GetValue(null); |
| | 82 | | } |
| | 83 | | } |
| 91 | 84 | | } |
| 93 | 85 | | } |
| | 86 | |
|
| | 87 | | private bool Includes(string name) |
| | 88 | | { |
| 645 | 89 | | if (_names is null || _names.Length == 0) |
| | 90 | | { |
| 461 | 91 | | return true; |
| | 92 | | } |
| | 93 | |
|
| 1204 | 94 | | for (int i = 0; i < _names.Length; ++i) |
| | 95 | | { |
| 484 | 96 | | if (string.Equals(_names[i], name, StringComparison.Ordinal)) |
| | 97 | | { |
| 66 | 98 | | return true; |
| | 99 | | } |
| | 100 | | } |
| | 101 | |
|
| 118 | 102 | | return false; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | private bool Excludes(string name) |
| | 106 | | { |
| 527 | 107 | | if (Exclude != null) |
| | 108 | | { |
| 992 | 109 | | for (int i = 0; i < Exclude.Length; ++i) |
| | 110 | | { |
| 316 | 111 | | if (string.Equals(Exclude[i], name, StringComparison.Ordinal)) |
| | 112 | | { |
| 52 | 113 | | return true; |
| | 114 | | } |
| | 115 | | } |
| | 116 | | } |
| | 117 | |
|
| 475 | 118 | | return false; |
| | 119 | | } |
| | 120 | | } |
| | 121 | | } |