|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using Elastic.Xunit.XunitPlumbing; |
| 6 | +using Nest; |
| 7 | +using Tests.Analysis.TokenFilters; |
| 8 | +using Tests.Core.Client; |
| 9 | +using Tests.Search; |
| 10 | + |
| 11 | +namespace Tests.Analysis.Tokenizers |
| 12 | +{ |
| 13 | + public static class AnalysisUsageTests |
| 14 | + { |
| 15 | + public static IndexSettings TokenizersFluent => Fluent<TokenizersDescriptor, ITokenizerAssertion, ITokenizers>(i => i.Fluent, (a, v) => a.Tokenizers = v.Value); |
| 16 | + |
| 17 | + public static IndexSettings TokenFiltersFluent => Fluent<TokenFiltersDescriptor, ITokenFilterAssertion, ITokenFilters>(i => i.Fluent, (a, v) => a.TokenFilters = v.Value); |
| 18 | + |
| 19 | + public static IndexSettings TokenizersInitializer => Init<Nest.Tokenizers, ITokenizerAssertion, ITokenizer>(i => i.Initializer, (a, v) => a.Tokenizers = v); |
| 20 | + |
| 21 | + public static IndexSettings TokenFiltersInitializer => Init<Nest.TokenFilters, ITokenFilterAssertion, ITokenFilter>(i => i.Initializer, (a, v) => a.TokenFilters = v); |
| 22 | + |
| 23 | + private static IndexSettings Fluent<TContainer, TAssertion, TValue>(Func<TAssertion, Func<string, TContainer, IPromise<TValue>>> fluent, Action<Nest.Analysis, IPromise<TValue>> set) |
| 24 | + where TAssertion : IAnalysisAssertion |
| 25 | + where TContainer : IPromise<TValue>, new() |
| 26 | + where TValue : class => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => fluent(a)(a.Name, t)))); |
| 27 | + |
| 28 | + private static IndexSettings Init<TContainer, TAssertion, TInitializer>(Func<TAssertion, TInitializer> value, Action<Nest.Analysis, TContainer> set) |
| 29 | + where TAssertion : IAnalysisAssertion |
| 30 | + where TContainer : IDictionary<string, TInitializer>, new() => Wrap(an => set(an, Apply<TContainer, TAssertion>((t, a) => t[a.Name] = value(a)))); |
| 31 | + |
| 32 | + private static TContainer Apply<TContainer, TAssertion>(Action<TContainer, TAssertion> act) |
| 33 | + where TAssertion : IAnalysisAssertion |
| 34 | + where TContainer : new() => All<TAssertion>().Aggregate(new TContainer() , (t,a) => { act(t,a); return t; }, t=>t); |
| 35 | + |
| 36 | + private static IndexSettings Wrap(Action<Nest.Analysis> set) |
| 37 | + { |
| 38 | + var a = new Nest.Analysis(); |
| 39 | + var s =new IndexSettings { Analysis = a }; |
| 40 | + set(a); |
| 41 | + return s; |
| 42 | + } |
| 43 | + |
| 44 | + private static List<TAssertion> All<TAssertion>() |
| 45 | + where TAssertion : IAnalysisAssertion |
| 46 | + { |
| 47 | + var types = |
| 48 | + from t in typeof(TokenizerTests).GetNestedTypes() |
| 49 | + where typeof(TAssertion).IsAssignableFrom(t) && t.IsClass |
| 50 | + let a = t.GetCustomAttributes(typeof(SkipVersionAttribute)).FirstOrDefault() as SkipVersionAttribute |
| 51 | + where a != null && !a.Ranges.Any(r=>r.IsSatisfied(TestClient.Configuration.ElasticsearchVersion)) |
| 52 | + select (TAssertion) Activator.CreateInstance(t); |
| 53 | + return types.ToList(); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + } |
| 58 | +} |
0 commit comments