|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using Castle.DynamicProxy; |
| 6 | +using FizzWare.NBuilder; |
| 7 | + |
| 8 | +namespace NTestDataBuilder.Lists |
| 9 | +{ |
| 10 | + public static class ListBuilderGenerator |
| 11 | + { |
| 12 | + static ListBuilderGenerator() |
| 13 | + { |
| 14 | + Generator = new ProxyGenerator(true); |
| 15 | + } |
| 16 | + |
| 17 | + public static ProxyGenerator Generator { get; private set; } |
| 18 | + } |
| 19 | + |
| 20 | + public static class ListBuilderExtensions |
| 21 | + { |
| 22 | + public static TBuilder TheFirst<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b, int howMany) |
| 23 | + where TObject : class |
| 24 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 25 | + { |
| 26 | + return b.ListBuilder.TheFirst(howMany); |
| 27 | + } |
| 28 | + |
| 29 | + public static TBuilder TheNext<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b, int howMany) |
| 30 | + where TObject : class |
| 31 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 32 | + { |
| 33 | + return b.ListBuilder.TheNext(howMany); |
| 34 | + } |
| 35 | + |
| 36 | + public static TBuilder TheLast<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b, int howMany) |
| 37 | + where TObject : class |
| 38 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 39 | + { |
| 40 | + return b.ListBuilder.TheLast(howMany); |
| 41 | + } |
| 42 | + |
| 43 | + public static TBuilder ThePrevious<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b, int howMany) |
| 44 | + where TObject : class |
| 45 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 46 | + { |
| 47 | + return b.ListBuilder.ThePrevious(howMany); |
| 48 | + } |
| 49 | + |
| 50 | + public static TBuilder All<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b) |
| 51 | + where TObject : class |
| 52 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 53 | + { |
| 54 | + return b.ListBuilder.All(); |
| 55 | + } |
| 56 | + |
| 57 | + public static ListBuilder<TObject, TBuilder> With<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b, Func<TBuilder, TBuilder> modifier) |
| 58 | + where TObject : class |
| 59 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 60 | + { |
| 61 | + return b.ListBuilder.With(modifier); |
| 62 | + } |
| 63 | + |
| 64 | + public static IList<TObject> BuildList<TObject, TBuilder>(this TestDataBuilder<TObject, TBuilder> b) |
| 65 | + where TObject : class |
| 66 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 67 | + { |
| 68 | + return b.ListBuilder.BuildList(); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + public class ListBuilderInterceptor<TObject, TBuilder> : IInterceptor |
| 73 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 74 | + where TObject : class |
| 75 | + { |
| 76 | + private readonly ListBuilder<TObject, TBuilder> _builder; |
| 77 | + |
| 78 | + public ListBuilderInterceptor(ListBuilder<TObject, TBuilder> builder) |
| 79 | + { |
| 80 | + _builder = builder; |
| 81 | + } |
| 82 | + |
| 83 | + public void Intercept(IInvocation invocation) |
| 84 | + { |
| 85 | + if (invocation.Method.ReturnType != typeof (TBuilder)) |
| 86 | + { |
| 87 | + throw new InvalidOperationException("Non-fluent builder method invoked while creating a list of builders: " + invocation.Method.Name); |
| 88 | + } |
| 89 | + |
| 90 | + _builder.Execute(invocation); |
| 91 | + invocation.ReturnValue = _builder.BuilderProxy; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public class ListBuilder<TObject, TBuilder> |
| 96 | + where TBuilder : TestDataBuilder<TObject, TBuilder>, new() |
| 97 | + where TObject : class |
| 98 | + { |
| 99 | + private int _start = 0; |
| 100 | + private int _count = 0; |
| 101 | + private readonly List<TBuilder> _list; |
| 102 | + |
| 103 | + public ListBuilder(int size) |
| 104 | + { |
| 105 | + BuilderProxy = (TBuilder) ListBuilderGenerator.Generator |
| 106 | + .CreateClassProxy(typeof (TBuilder), new ProxyGenerationOptions(new EnsureAllMethodsVirtual()), new ListBuilderInterceptor<TObject, TBuilder>(this)); |
| 107 | + BuilderProxy.ListBuilder = this; |
| 108 | + _list = new List<TBuilder>(); |
| 109 | + for (var i = 0; i < size; i++) |
| 110 | + _list.Add(new TBuilder()); |
| 111 | + } |
| 112 | + |
| 113 | + public TBuilder BuilderProxy { get; private set; } |
| 114 | + |
| 115 | + public TBuilder TheFirst(int howMany) |
| 116 | + { |
| 117 | + _start = 0; |
| 118 | + _count = howMany; |
| 119 | + return BuilderProxy; |
| 120 | + } |
| 121 | + |
| 122 | + public TBuilder TheNext(int howMany) |
| 123 | + { |
| 124 | + _start += _count; |
| 125 | + _count = howMany; |
| 126 | + return BuilderProxy; |
| 127 | + } |
| 128 | + |
| 129 | + public TBuilder ThePrevious(int howMany) |
| 130 | + { |
| 131 | + _start -= howMany; |
| 132 | + _count = howMany; |
| 133 | + return BuilderProxy; |
| 134 | + } |
| 135 | + |
| 136 | + public TBuilder TheLast(int howMany) |
| 137 | + { |
| 138 | + _start = _list.Count - howMany; |
| 139 | + _count = howMany; |
| 140 | + return BuilderProxy; |
| 141 | + } |
| 142 | + |
| 143 | + public TBuilder All() |
| 144 | + { |
| 145 | + _start = 0; |
| 146 | + _count = _list.Count; |
| 147 | + return BuilderProxy; |
| 148 | + } |
| 149 | + |
| 150 | + public ListBuilder<TObject, TBuilder> With(Func<TBuilder, TBuilder> modifier) |
| 151 | + { |
| 152 | + _list.Skip(_start) |
| 153 | + .Take(_count) |
| 154 | + .ToList() |
| 155 | + .ForEach(b => modifier(b)); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
| 159 | + public IList<TObject> BuildList() |
| 160 | + { |
| 161 | + return _list.Select(b => b.Build()).ToArray(); |
| 162 | + } |
| 163 | + |
| 164 | + internal void Execute(IInvocation invocation) |
| 165 | + { |
| 166 | + _list.Skip(_start) |
| 167 | + .Take(_count) |
| 168 | + .ToList() |
| 169 | + .ForEach(b => invocation.Method.Invoke(b, invocation.Arguments)); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + public class EnsureAllMethodsVirtual : IProxyGenerationHook |
| 174 | + { |
| 175 | + public void MethodsInspected() |
| 176 | + { |
| 177 | + } |
| 178 | + |
| 179 | + public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo) |
| 180 | + { |
| 181 | + if (new[]{"get_Any", "Build", "AsProxy", "Get", "GetOrDefault", "Set", "Has"}.Contains(memberInfo.Name)) |
| 182 | + return; |
| 183 | + throw new InvalidOperationException(string.Format("Tried to build a list with a builder who has non-virtual method. Please make {0} on type {1} virtual.", memberInfo.Name, type.Name)); |
| 184 | + } |
| 185 | + |
| 186 | + public bool ShouldInterceptMethod(Type type, MethodInfo methodInfo) |
| 187 | + { |
| 188 | + return true; |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments