Skip to content

Commit 417aeed

Browse files
committed
增加单元测试
1 parent 2369490 commit 417aeed

File tree

4 files changed

+39
-32
lines changed

4 files changed

+39
-32
lines changed

WebApiClientCore.Test/BuildinExtensions/AttributeExtensionsTest.cs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,16 @@ namespace WebApiClientCore.Test.BuildinExtensions
66
{
77
public class AttributeExtensionsTest
88
{
9-
interface IAttribute
10-
{
11-
}
129

13-
class MyAttribute : Attribute, IAttribute
10+
class MyAttribute : Attribute
1411
{
1512
}
1613

17-
class YourAttribute : Attribute, IAttribute
14+
class YourAttribute : Attribute
1815
{
1916
}
2017

21-
class MyClass
22-
{
23-
[My]
24-
public int Age { get; set; }
25-
26-
[My, Your]
27-
public string Name { get; set; }
28-
29-
public void Set(int age, [My, Your] string name)
30-
{
31-
}
32-
}
33-
34-
class D1
18+
class Class1
3519
{
3620
public void M1()
3721
{
@@ -42,16 +26,36 @@ public void M2()
4226
{
4327
}
4428
}
45-
29+
30+
[My]
31+
interface Inteface1
32+
{
33+
}
34+
35+
[My]
36+
interface Interface2 : Inteface1
37+
{
38+
}
39+
40+
[Your]
41+
[My]
42+
interface Interface3 : Interface2
43+
{
44+
}
45+
46+
4647

4748
[Fact]
4849
public void GetAttributesTest()
4950
{
50-
var m1 = typeof(D1).GetMethod("M1");
51-
var m2 = typeof(D1).GetMethod("M2");
51+
var m1 = typeof(Class1).GetMethod("M1");
52+
var m2 = typeof(Class1).GetMethod("M2");
5253

5354
Assert.True(m1.GetAttributes<MyAttribute>().Count() == 0);
5455
Assert.True(m2.GetAttributes<MyAttribute>().Count() == 1);
56+
57+
Assert.Equal(2, typeof(Interface2).GetAttributes<Attribute>(inclueBases: true).Count());
58+
Assert.Equal(4, typeof(Interface3).GetAttributes<Attribute>(inclueBases: true).Count());
5559
}
5660
}
5761
}

WebApiClientCore/ApiActionDescriptor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,14 @@ public ApiActionDescriptor(MethodInfo method, Type? interfaceType = default)
8787
// 接口特性优先于方法所在类型的特性
8888
var actionAttributes = method
8989
.GetAttributes<IApiActionAttribute>()
90-
.Concat(interfaceType.GetAttributes<IApiActionAttribute>())
90+
.Concat(interfaceType.GetAttributes<IApiActionAttribute>(inclueBases: true))
9191
.Distinct(MultiplableComparer<IApiActionAttribute>.Default)
9292
.OrderBy(item => item.OrderIndex)
9393
.ToReadOnlyList();
9494

9595
var filterAttributes = method
9696
.GetAttributes<IApiFilterAttribute>()
97-
.Concat(interfaceType.GetAttributes<IApiFilterAttribute>())
97+
.Concat(interfaceType.GetAttributes<IApiFilterAttribute>(inclueBases: true))
9898
.Distinct(MultiplableComparer<IApiFilterAttribute>.Default)
9999
.OrderBy(item => item.OrderIndex)
100100
.Where(item => item.Enable)

WebApiClientCore/ApiReturnDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public ApiReturnDescriptor(MethodInfo method, Type interfaceType)
6262
this.DataType = dataType;
6363
this.Attributes = method
6464
.GetAttributes<IApiReturnAttribute>()
65-
.Concat(interfaceType.GetAttributes<IApiReturnAttribute>())
65+
.Concat(interfaceType.GetAttributes<IApiReturnAttribute>(inclueBases: true))
6666
.Concat(GetDefaultAttributes(dataType))
6767
.Distinct(MultiplableComparer<IApiReturnAttribute>.Default)
6868
.OrderBy(item => item.OrderIndex)

WebApiClientCore/BuildinExtensions/AttributeExtensions.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,20 @@ public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this ParameterIn
4444
}
4545

4646
/// <summary>
47-
/// 获取接口以及其基础接口定义的特性
47+
/// 获取接口定义的特性
4848
/// </summary>
4949
/// <typeparam name="TAttribute"></typeparam>
50-
/// <param name="interfaceType">接口类型</param>
50+
/// <param name="interfaceType">接口类型</param>
51+
/// <param name="inclueBases">是否包括基础接口定义的特性</param>
5152
/// <returns></returns>
52-
public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this Type interfaceType) where TAttribute : class
53+
public static IEnumerable<TAttribute> GetAttributes<TAttribute>(this Type interfaceType, bool inclueBases) where TAttribute : class
5354
{
54-
return new[] { interfaceType }
55-
.Concat(interfaceType.GetInterfaces())
56-
.SelectMany(item => item.GetCustomAttributes()
57-
.OfType<TAttribute>());
55+
var types = Enumerable.Repeat(interfaceType, 1);
56+
if (inclueBases == true)
57+
{
58+
types = types.Concat(interfaceType.GetInterfaces());
59+
}
60+
return types.SelectMany(item => item.GetCustomAttributes().OfType<TAttribute>());
5861
}
5962
}
6063
}

0 commit comments

Comments
 (0)