|
1 |
| -using System.Collections.Generic; |
| 1 | +using System; |
| 2 | +using System.CodeDom.Compiler; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Globalization; |
2 | 5 | using System.Linq;
|
3 | 6 | using System.Reflection;
|
| 7 | +using Elasticsearch.Net; |
4 | 8 | using FluentAssertions;
|
5 | 9 | using Nest;
|
| 10 | +using Nest.Aggregations.Visitor; |
6 | 11 | using Tests.Framework;
|
7 | 12 |
|
8 | 13 | namespace Tests.CodeStandards
|
9 | 14 | {
|
10 | 15 | /** == Naming Conventions
|
11 |
| - * |
| 16 | + * |
12 | 17 | * NEST uses the following naming conventions (with _some_ exceptions).
|
13 | 18 | */
|
14 | 19 | public class NamingConventions
|
@@ -136,5 +141,142 @@ public void ParityBetweenRequestsAndResponses()
|
136 | 141 |
|
137 | 142 | requests.Except(responses).Should().BeEmpty();
|
138 | 143 | }
|
| 144 | + |
| 145 | + [U] |
| 146 | + public void AllNestTypesAreInNestNamespace() |
| 147 | + { |
| 148 | + var nestAssembly = typeof(IElasticClient).Assembly(); |
| 149 | + |
| 150 | + var exceptions = new List<Type> |
| 151 | + { |
| 152 | + nestAssembly.GetType("Elasticsearch.Net.DotNetCoreTypeExtensions"), |
| 153 | + typeof(AggregationWalker), |
| 154 | + typeof(AggregationVisitor), |
| 155 | + typeof(IAggregationVisitor), |
| 156 | + nestAssembly.GetType("System.AssemblyVersionInformation"), |
| 157 | +#if DOTNETCORE |
| 158 | + typeof(SynchronizedCollection<>), |
| 159 | + nestAssembly.GetType("System.ComponentModel.Browsable") |
| 160 | +#endif |
| 161 | + }; |
| 162 | + |
| 163 | + var types = nestAssembly.GetTypes(); |
| 164 | + var typesNotInNestNamespace = types |
| 165 | + .Where(t => !exceptions.Contains(t)) |
| 166 | + .Where(t => t.Namespace != "Nest") |
| 167 | + .Where(t => !t.Name.StartsWith("<")) |
| 168 | + .Where(t => IsValidTypeNameOrIdentifier(t.Name, true)) |
| 169 | + .ToList(); |
| 170 | + |
| 171 | + typesNotInNestNamespace.Should().BeEmpty(); |
| 172 | + } |
| 173 | + |
| 174 | + [U] |
| 175 | + public void AllElasticsearchNetTypesAreInElasticsearchNetNamespace() |
| 176 | + { |
| 177 | + var elasticsearchNetAssembly = typeof(IElasticLowLevelClient).Assembly(); |
| 178 | + |
| 179 | + var exceptions = new List<Type> |
| 180 | + { |
| 181 | + elasticsearchNetAssembly.GetType("System.AssemblyVersionInformation"), |
| 182 | + elasticsearchNetAssembly.GetType("System.FormattableString"), |
| 183 | + elasticsearchNetAssembly.GetType("System.Runtime.CompilerServices.FormattableStringFactory"), |
| 184 | + elasticsearchNetAssembly.GetType("System.Runtime.CompilerServices.FormattableStringFactory"), |
| 185 | + elasticsearchNetAssembly.GetType("Purify.Purifier"), |
| 186 | + elasticsearchNetAssembly.GetType("Purify.Purifier+IPurifier"), |
| 187 | + elasticsearchNetAssembly.GetType("Purify.Purifier+PurifierDotNet"), |
| 188 | + elasticsearchNetAssembly.GetType("Purify.Purifier+PurifierMono"), |
| 189 | + elasticsearchNetAssembly.GetType("Purify.Purifier+UriInfo"), |
| 190 | +#if DOTNETCORE |
| 191 | + elasticsearchNetAssembly.GetType("System.ComponentModel.Browsable") |
| 192 | +#endif |
| 193 | + }; |
| 194 | + |
| 195 | + var types = elasticsearchNetAssembly.GetTypes(); |
| 196 | + var typesNotIElasticsearchNetNamespace = types |
| 197 | + .Where(t => !exceptions.Contains(t)) |
| 198 | + .Where(t => t.Namespace != "Elasticsearch.Net") |
| 199 | + .Where(t => !t.Name.StartsWith("<")) |
| 200 | + .Where(t => IsValidTypeNameOrIdentifier(t.Name, true)) |
| 201 | + .ToList(); |
| 202 | + |
| 203 | + typesNotIElasticsearchNetNamespace.Should().BeEmpty(); |
| 204 | + } |
| 205 | + |
| 206 | + /// implementation from System.CodeDom.Compiler.CodeGenerator.IsValidLanguageIndependentIdentifier(string value) |
| 207 | + private static bool IsValidTypeNameOrIdentifier(string value, bool isTypeName) |
| 208 | + { |
| 209 | + bool nextMustBeStartChar = true; |
| 210 | + if (value.Length == 0) |
| 211 | + return false; |
| 212 | + for (int index = 0; index < value.Length; ++index) |
| 213 | + { |
| 214 | + var character = value[index]; |
| 215 | +#if DOTNETCORE |
| 216 | + var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(character); |
| 217 | +#else |
| 218 | + var unicodeCategory = char.GetUnicodeCategory(character); |
| 219 | +#endif |
| 220 | + switch (unicodeCategory) |
| 221 | + { |
| 222 | + case UnicodeCategory.UppercaseLetter: |
| 223 | + case UnicodeCategory.LowercaseLetter: |
| 224 | + case UnicodeCategory.TitlecaseLetter: |
| 225 | + case UnicodeCategory.ModifierLetter: |
| 226 | + case UnicodeCategory.OtherLetter: |
| 227 | + case UnicodeCategory.LetterNumber: |
| 228 | + nextMustBeStartChar = false; |
| 229 | + break; |
| 230 | + case UnicodeCategory.NonSpacingMark: |
| 231 | + case UnicodeCategory.SpacingCombiningMark: |
| 232 | + case UnicodeCategory.DecimalDigitNumber: |
| 233 | + case UnicodeCategory.ConnectorPunctuation: |
| 234 | + if (nextMustBeStartChar && (int)character != 95) |
| 235 | + return false; |
| 236 | + nextMustBeStartChar = false; |
| 237 | + break; |
| 238 | + default: |
| 239 | + if (!isTypeName || !IsSpecialTypeChar(character, ref nextMustBeStartChar)) |
| 240 | + return false; |
| 241 | + break; |
| 242 | + } |
| 243 | + } |
| 244 | + return true; |
| 245 | + } |
| 246 | + |
| 247 | + private static bool IsSpecialTypeChar(char ch, ref bool nextMustBeStartChar) |
| 248 | + { |
| 249 | + if ((uint)ch <= 62U) |
| 250 | + { |
| 251 | + switch (ch) |
| 252 | + { |
| 253 | + case '$': |
| 254 | + case '&': |
| 255 | + case '*': |
| 256 | + case '+': |
| 257 | + case ',': |
| 258 | + case '-': |
| 259 | + case '.': |
| 260 | + case ':': |
| 261 | + case '<': |
| 262 | + case '>': |
| 263 | + break; |
| 264 | + default: |
| 265 | + goto label_6; |
| 266 | + } |
| 267 | + } |
| 268 | + else if ((int)ch != 91 && (int)ch != 93) |
| 269 | + { |
| 270 | + if ((int)ch == 96) |
| 271 | + return true; |
| 272 | + goto label_6; |
| 273 | + } |
| 274 | + nextMustBeStartChar = true; |
| 275 | + return true; |
| 276 | + label_6: |
| 277 | + return false; |
| 278 | + } |
139 | 279 | }
|
140 | 280 | }
|
| 281 | + |
| 282 | + |
0 commit comments