-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNamingConventions.doc.cs
84 lines (74 loc) · 2.32 KB
/
NamingConventions.doc.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentAssertions;
using Nest;
using Tests.Framework;
namespace Tests.CodeStandards
{
public class NamingConventions
{
/**
* Abstract class names should end with a `Base` suffix
*/
//[U]
public void AbstractClassNamesEndWithBase()
{
var abstractClasses = typeof(IRequest).Assembly().GetTypes()
.Where(t => t.IsClass() && t.IsAbstract() && !t.IsSealed())
.Select(t => t.Name.Split('`')[0])
.ToList();
foreach (var abstractClass in abstractClasses)
abstractClass.Should().EndWith("Base");
}
/**
* Request class names should end with "Request"
*/
//[U]
public void RequestClassNamesEndWithRequest()
{
var types = typeof(IRequest).Assembly().GetTypes();
var requests = types
.Where(t => typeof(IRequest).IsAssignableFrom(t))
.Select(t => t.Name.Split('`')[0])
.ToList();
foreach (var request in requests)
request.Should().EndWith("Request");
}
/**
* Response class names should end with "Response"
**/
//[U]
public void ResponseClassNamesEndWithResponse()
{
var types = typeof(IRequest).Assembly().GetTypes();
var responses = types
.Where(t => typeof(IResponse).IsAssignableFrom(t))
.Select(t => t.Name.Split('`')[0])
.ToList();
foreach (var response in responses)
response.Should().EndWith("Response");
}
/**
* Request and Response class names should be one to one in *most* cases.
* e.g. ValidateRequest => ValidateResponse, and not ValidateQueryRequest => ValidateResponse
*/
//[U]
public void ParityBetweenRequestsAndResponses()
{
var types = typeof(IRequest).Assembly().GetTypes();
var requests = new HashSet<string>(types
.Where(t => t.IsClass() && !t.IsAbstract() && typeof(IRequest).IsAssignableFrom(t) && !(t.Name.EndsWith("Descriptor")))
.Select(t => t.Name.Split('`')[0].Replace("Request", ""))
);
var responses = types
.Where(t => t.IsClass() && !t.IsAbstract() && typeof(IResponse).IsAssignableFrom(t))
.Select(t => t.Name.Split('`')[0].Replace("Response", ""));
// Add any exceptions to the rule here
var exceptions = new string[] { "Cat" };
responses = responses.Where(r => !exceptions.Contains(r)).ToList();
foreach (var response in responses)
requests.Should().Contain(response);
}
}
}