public bool Matches(ConstruktionContext context) { var typeInfo = context.RequestType.GetTypeInfo(); return(typeInfo.IsGenericType && typeInfo.GetGenericTypeDefinition() == typeof(Dictionary <,>)); }
private object Construct(ConstruktionContext context, Blueprint blueprint) { if (depthReached() || recursionDetected()) { return(!depthReached() && _settings.ThrowOnRecursion ? throw new Exception($"Recursion Detected: {context.RequestType.FullName}") : default(object)); } _graph.Add(context.RequestType); var result = blueprint.Construct(context, this); _graph.Remove(context.RequestType); return(result); bool depthReached() => _graph.Count > _settings.MaxDepth; bool recursionDetected() { var depth = _graph.Count(x => context.RequestType == x); return(depth > _settings.RecursionDepth || (depth > 0 && _settings.ThrowOnRecursion)); } }
public override char Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var chars = "$%#@!*abcdefghijklmnopqrstuvwxyz1234567890?;:ABCDEFGHIJKLMNOPQRSTUVWXYZ^&"; var num = _random.Next(0, chars.Length); return(chars[num]); }
private string StartLog(ConstruktionContext requestContext, Blueprint blueprint, string buffer) { var requestName = requestContext.RequestType.FullName; var start = $"Start: {requestName}"; if (requestContext.PropertyInfo.IsNulloPropertyInfo() == false) { requestName = requestContext.PropertyInfo.Name; start = $"Start Property: {requestName}"; } else if (requestContext.ParameterInfo.IsNulloParameterInfo() == false) { requestName = requestContext.ParameterInfo.Name; start = $"Start Parameter: {requestName}"; } _underConstruction.Add(requestContext.RequestType); if (level != 0) { _log.Add(""); } _log.Add($"{buffer}{start}"); _log.Add($"{buffer}Blueprint: {blueprint}"); return(requestName); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var userName = Guid.NewGuid().ToString("N"); var domain = domains[random.Next(domains.Count - 1)]; return(userName + domain); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { int count = 4; Type key = context.RequestType.GetGenericArguments()[0]; Type value = context.RequestType.GetGenericArguments()[1]; // If dictionary key is an enum with unique number of items less than the available count, use that number for the dictionary generation instead. if (key.GetTypeInfo().IsEnum) { int max = Enum.GetNames(context.RequestType.GetGenericArguments()[0]).Length; count = max < count ? max : count; } var keys = UniqueKeys(count, key, pipeline, new HashSet <object>()).ToList(); var values = Values(count, value, pipeline).ToList(); var dictionary = (IDictionary)Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(key, value)); for (var i = 0; i <= count - 1; i++) { dictionary.Add(keys[i], values[i]); } return(dictionary); }
public object DebugSend(ConstruktionContext context, out List <string> debugLog) { var depth = _underConstruction.Count(x => context.RequestType == x); var blueprint = Settings.Blueprints.First(x => x.Matches(context)); level++; var indent = new string(' ', level * 5); if (depth > Settings.RecursionDepth) { _log.Add($"{indent}Recursion detected over the allowed limit. Omitting {context.RequestType.FullName}"); debugLog = _log; return(default(object)); } var requestName = StartLog(context, blueprint, indent); object result; try { result = blueprint.Construct(context, this); var exitBlueprint = _settings.ExitBlueprints.FirstOrDefault(x => x.Matches(result, context)); if (exitBlueprint != null) { _log.Add($"{indent}ExitBlueprint: {exitBlueprint}"); } result = exitBlueprint?.Construct(result, this) ?? result; } catch (OutOfMemoryException) { throw new Exception( "Oops you ran out of memory! It looks like this object graph is REALLY big. " + "It might be best to add a blueprint that constructs your object manually, " + "or revisit the design of your object. The current log can be found in the inner exception.", new Exception(string.Join("\n", _log))); } catch (Exception ex) { _log.Add( $"{indent}An exception occurred in blueprint {blueprint}, when constructing {requestName}"); _log.Add($"{indent}Exception is: {ex.Message}"); _log.Add(""); debugLog = _log; return(default(object)); } EndLog(context, indent, requestName); debugLog = _log; return(result); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var inject = new Foo(); pipeline.Inject(context.RequestType, inject); return(inject); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var arrayType = context.RequestType.GetElementType(); var results = construct(arrayType, pipeline); return(results); }
public override string Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var prefix = context.PropertyInfo.IsNulloPropertyInfo() ? "String" : context.PropertyInfo.Name; var result = prefix + "-" + _random.Next(1, 10000); return(result); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var implementation = _typeMap[context.RequestType]; var result = pipeline.Send(new ConstruktionContext(implementation)); return(result); }
public override DateTime Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var start = DateTime.Today.AddYears(-2); var range = (DateTime.Today - start).Days; return(start.AddDays(_random.Next(range))); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var closedType = context.RequestType.GenericTypeArguments[0]; var results = construct(closedType, pipeline); return(results); }
public void should_not_match_reference_types() { var blueprint = new NullableTypeBlueprint(); var context = new ConstruktionContext(typeof(Foo)); var matches = blueprint.Matches(context); matches.ShouldBe(false); }
public void should_build_property_context_correctly() { var context = new ConstruktionContext(typeof(Foo).GetProperty(nameof(Foo.Bar))); context.RequestType.ShouldBe(typeof(string)); context.PropertyContext.Name.ShouldBe("Bar"); context.PropertyContext.IsType(typeof(string)).ShouldBe(true); context.PropertyContext.GetAttributes(typeof(MaxLengthAttribute)).Count().ShouldBe(1); }
public void log_should_power_through_exceptions() { var context = new ConstruktionContext(typeof(WillThrowFoo)); var result = new Construktion().DebuggingConstruct(context, out string log); log.ShouldContain("Cannot construct the interface"); log.ShouldContain("End Construktion.Tests.DebuggingConstruktionTests+WillThrowFoo"); }
public void request_type_should_match_request() { var context = new ConstruktionContext(typeof(string)); context.RequestType.ShouldBe(typeof(string)); context.PropertyInfo.ShouldBeNulloPropertyInfo(); context.ParameterInfo.ShouldBeNulloParameterInfo(); }
public void should_set_property_info() { var context = new ConstruktionContext(typeof(Foo).GetProperty(nameof(Foo.Bar))); context.RequestType.ShouldBe(typeof(string)); context.PropertyInfo.ShouldNotBe(null); context.ParameterInfo.ShouldBeNulloParameterInfo(); }
public void should_not_match_non_nullable_type() { var blueprint = new NullableTypeBlueprint(); var context = new ConstruktionContext(typeof(Foo).GetProperty("NonNullableAge")); var matches = blueprint.Matches(context); matches.ShouldBe(false); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var items = pipeline.Settings.EnumerableCount; var key = context.RequestType.GetGenericArguments()[0]; var valueType = context.RequestType.GetGenericArguments()[1]; if (key.GetTypeInfo().IsEnum) { items = Enum.GetNames(key).Length; } var keys = uniqueKeys(); var values = itemValues(valueType); var dictionary = (IDictionary)typeof(Dictionary <,>).NewGeneric(key, valueType); for (var i = 0; i <= items - 1; i++) { dictionary.Add(keys[i], values[i]); } return(dictionary); List <object> uniqueKeys() { var builtKeys = new HashSet <object>(); while (true) { var newItem = pipeline.Send(new ConstruktionContext(key)); if (newItem != null) { builtKeys.Add(newItem); } if (builtKeys.Count == items) { return(builtKeys.ToList()); } } } List <object> itemValues(Type closedType) { var results = new List <object>(); for (var i = 0; i < items; i++) { results.Add(pipeline.Send(new ConstruktionContext(closedType))); } return(results); } }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var closedType = context.RequestType.GetGenericArguments()[0]; var useNull = _random.Next(1, 5); return(useNull == 1 ? null : pipeline.Send(new ConstruktionContext(closedType))); }
public void should_construct_from_attribute_value() { var blueprint = new SetBlueprint(); var property = typeof(Foo).GetProperty("WithSet"); var context = new ConstruktionContext(property); var result = (string)blueprint.Construct(context, Default.Pipeline); result.ShouldBe("Set"); }
public void property_without_attribute_should_not_match() { var blueprint = new SetBlueprint(); var property = typeof(Foo).GetProperty("WithoutSet"); var context = new ConstruktionContext(property); var matches = blueprint.Matches(context); matches.ShouldBe(false); }
public void should_match_property_with_attribute() { var blueprint = new SetBlueprint(); var property = typeof(Foo).GetProperty("WithSet"); var context = new ConstruktionContext(property); var matches = blueprint.Matches(context); matches.ShouldBe(true); }
public void should_log() { var context = new ConstruktionContext(typeof(Foo)); new Construktion().DebuggingConstruct(context, out string log); log = RemoveWhiteSpace(log); var expected = RemoveWhiteSpace(ExpectedLog); log.ShouldBe(expected); }
public bool Matches(ConstruktionContext context) { var typeInfo = context.RequestType.GetTypeInfo(); return((typeInfo.IsInterface && _typeMap.ContainsKey(context.RequestType)) || (!typeInfo.IsGenericType && typeInfo.IsClass && context.RequestType.HasNonDefaultCtor())); }
private void EndLog(ConstruktionContext context, string buffer, string requestName) { level--; _underConstruction.Remove(context.RequestType); if (_log.Last().Trim().StartsWith("End")) { _log.Add(""); } _log.Add($"{buffer}End {requestName}"); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { if (context.RequestType.GetTypeInfo().IsInterface) { throw new Exception($"Cannot construct the interface {context.RequestType.Name}. " + "You must register it or add a custom blueprint."); } throw new Exception($"No Blueprint could be found for {context.RequestType.FullName}. Please add " + $"a custom blueprint that can create it."); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var implementation = _typeMap.ContainsKey(context.RequestType) ? _typeMap[context.RequestType] : context.RequestType; var ctor = BuildCtor(implementation, pipeline); var instance = construct(ctor, pipeline); return(instance); }
public object Construct(ConstruktionContext context, ConstruktionPipeline pipeline) { var method = typeof(A) .GetMethod("Fake", Type.EmptyTypes) .MakeGenericMethod(context.ParameterInfo.ParameterType); var fake = method.Invoke(null, null); pipeline.Inject(context.RequestType, fake); return(fake); }