Ejemplo n.º 1
0
        public object Render(ObjectRenderingRequest req)
        {
            if (_cyclePreventer != null)
            {
                var res = _cyclePreventer.Render(req);
                if (res != req.RenderTarget && res != req)
                {
                    req = new ObjectRenderingRequest(req.Assertion, req.Renderer, res);
                }
            }

            var obj    = req.RenderTarget;
            var oldObj = obj;

            foreach (var mapping in _map)
            {
                if (obj != oldObj && obj != req)
                {
                    req    = new ObjectRenderingRequest(req.Assertion, req.Renderer, obj);
                    oldObj = obj;
                }

                obj = mapping.Render(req);
            }
            return(obj);
        }
        public object Render(ObjectRenderingRequest req)
        {
            IEnumerable obj = req?.RenderTarget as IEnumerable;

            if (obj == null)
            {
                return(req?.RenderTarget);
            }

            var prefix = "";
            var result = new StringBuilder();

            result.Append("[");
            result.Append(ObjectToCode.ToCSharpFriendlyTypeName(obj.GetType()));
            result.Append("]@(");
            foreach (var rec in obj)
            {
                var req2 = req.NewTarget(rec);
                result.Append(prefix);
                result.Append(req2.Render());
                prefix = ",";
            }
            result.Append(")");

            return(new AnnotatedFormattable(result.ToString()));
        }
        public object Render(ObjectRenderingRequest req)
        {
            if (req?.RenderTarget is AnnotatedFormattable)
            {
                return(req?.RenderTarget);
            }

            var target  = req.RenderTarget;
            var members = target.GetType().GetMembers().Where(m => m.MemberType == MemberTypes.Field || m.MemberType == MemberTypes.Property);
            var result  = new StringBuilder();

            result.Append("[");
            result.Append(ObjectToCode.ToCSharpFriendlyTypeName(target.GetType()));
            result.Append("]@{");

            foreach (var member in members)
            {
                result.Append('"');
                result.Append(member.Name);
                result.Append('"');
                result.Append('=');

                var value = Reflection.GetValue(member, target);
                var req2  = req.NewTarget(value);
                result.Append(req2.Render());
                result.Append(';');
            }
            result.Append("}");

            return(new AnnotatedFormattable(result.ToString()));
        }
 public object Render(ObjectRenderingRequest req)
 {
     if (!(req?.RenderTarget is Exception))
     {
         return(req?.RenderTarget);
     }
     return(new AnnotatedFormattable(req.RenderTarget.ToString(), null, "[" + ObjectToCode.ToCSharpFriendlyTypeName(req.RenderTarget.GetType()) + "]", null));
 }
 public object Render(ObjectRenderingRequest req)
 {
     if (!typeof(Enum).IsAssignableFrom(req.GetType()))
     {
         return(req.RenderTarget);
     }
     return(new AnnotatedFormattable(req.RenderTarget, "G", "[" + ObjectToCode.ToCSharpFriendlyTypeName(req.RenderTarget.GetType()) + "]", null));
 }
        public object Render(ObjectRenderingRequest req)
        {
            if (!(req?.RenderTarget is Type))
            {
                return(req?.RenderTarget);
            }

            return(new AnnotatedFormattable(string.Format("[{0}]", ((Type)req.RenderTarget).FullName)));
        }
        public object Render(ObjectRenderingRequest req)
        {
            if (!_test(req))
            {
                return(req.RenderTarget);
            }

            return(_renderer.Render(req));
        }
        object IObjectRenderer.Render(ObjectRenderingRequest req)
        {
            if (!Test(req))
            {
                return(req.RenderTarget);
            }

            return(Render(req));
        }
Ejemplo n.º 9
0
        public object Render(ObjectRenderingRequest req)
        {
            if (!(req?.RenderTarget is string))
            {
                return(req.RenderTarget);
            }
            var result = StringLiteralRenderer.RenderString(req?.RenderTarget?.ToString());

            return(new AnnotatedFormattable(result, null, "[string]", null));
        }
Ejemplo n.º 10
0
        public object Render(ObjectRenderingRequest req)
        {
            var del = req?.RenderTarget as Delegate;

            if (del == null)
            {
                return(req?.RenderTarget);
            }

            return(new AnnotatedFormattable(string.Format("[{0}]", del.Method.ToString())));
        }
Ejemplo n.º 11
0
        public object Render(ObjectRenderingRequest req)
        {
            if (!(req?.RenderTarget is char))
            {
                return(req.RenderTarget);
            }
            var c  = (char)req.RenderTarget;
            var sb = new StringBuilder(16);

            StringLiteralRenderer.RenderChar(c, sb);
            return(new AnnotatedFormattable(sb.ToString(), null, "[char]", null));
        }
Ejemplo n.º 12
0
        static object RenderObject(AssertionData data, object obj, IObjectRenderer renderer)
        {
            var req = new ObjectRenderingRequest(data, renderer, obj);

            // we pretty much are only doing this to clear out the cycle detector betweeen rendering different components
            var state = renderer.SaveState(req);

            try {
                return(renderer.Render(req));
            } finally {
                renderer.RestoreState(req, state);
            }
        }
Ejemplo n.º 13
0
        public object Render(ObjectRenderingRequest req)
        {
            var hash = _cycleDetector.GetOrCreateValue(req.Assertion);

            if (hash.Contains(req.RenderTarget))
            {
                return(new AnnotatedFormattable("~cycle detected~"));
            }
            else
            {
                hash.Add(req.RenderTarget);
                return(req.RenderTarget);
            }
        }
Ejemplo n.º 14
0
        public object Render(ObjectRenderingRequest req)
        {
            var exp = req?.RenderTarget as Expression;

            if (exp == null)
            {
                return(req?.RenderTarget);
            }

            return(new RenderedExpression(
                       exp,
                       ExpressionToCodeLib.ExpressionToCode.ToCode((Expression)req.RenderTarget)
                       ));
        }
Ejemplo n.º 15
0
        public object Render(ObjectRenderingRequest req)
        {
            var af = req?.RenderTarget as AnnotatedFormattable;

            if (af != null)
            {
                return(af);
            }
            var fm = req.RenderTarget as IFormattable;

            if (fm != null)
            {
                return(new AnnotatedFormattable(fm));
            }
            return(req.RenderTarget);
        }
Ejemplo n.º 16
0
        public object Render(ObjectRenderingRequest req)
        {
            var exp = req?.RenderTarget as Expression;

            if (exp == null)
            {
                return(req?.RenderTarget);
            }

            var data     = req.Assertion;
            var replacer = new ExpressionShortener(data.DataMappings.Select(kvp => kvp.Key.Name));

            exp = replacer.Replace(exp);
            foreach (var mapping in replacer.Parameters.Zip(replacer.Values, (p, v) => new { p, v }))
            {
                data.DataMappings.Add(new KeyValuePair <ParameterExpression, object>(mapping.p, mapping.v));
            }
            return(exp);
        }
Ejemplo n.º 17
0
 public object Render(ObjectRenderingRequest req)
 {
     return(new AnnotatedFormattable(RenderString(req?.RenderTarget?.ToString())));
 }
Ejemplo n.º 18
0
        public object Render(ObjectRenderingRequest req)
        {
            var data = req?.RenderTarget as AssertionData;

            if (data == null)
            {
                return(req?.RenderTarget);
            }

            var objectRenderer = req.Renderer;

            var s = new StringBuilder();

            if (data.Assertion != null)
            {
                s.AppendLine();
                s.Append(data.GetType().Name);
                s.Append(' ');
                s.Append(RenderObject(data, data.Assertion, objectRenderer));

                string prefix   = Environment.NewLine + " with ";
                var    mappings = data.DataMappings.ToArray();
                foreach (var @ref in mappings)
                {
                    var type = @ref.Value?.GetType() ?? typeof(UnknownType);
                    s.Append(prefix);
                    s.Append(RenderObject(data, Expression.Parameter(type, @ref.Key.Name), objectRenderer));
                    s.Append(" = ");
                    s.Append(RenderObject(data, @ref.Value, objectRenderer));
                    prefix = Environment.NewLine + " and ";
                }

                s.Append(".");
            }

            if (!string.IsNullOrWhiteSpace(data.FormatMessage))
            {
                s.AppendLine();
                s.Append(" Message: ");
                s.AppendFormat(data.FormatMessage, data.FormatArgs.Select(f => RenderObject(data, f, objectRenderer)).ToArray());
            }

            if (data.ContextData != null)
            {
                string prefix = Environment.NewLine + " With context data ";

                foreach (var d in data.ContextData)
                {
                    s.Append(prefix);
                    s.AppendFormat("[Depth={0}] ", d.Depth);
                    s.Append(d.Key);
                    s.Append(" = ");
                    s.Append(RenderObject(data, d.Value, objectRenderer));
                    s.Append(".");

                    prefix = Environment.NewLine + " And context data ";
                }
            }

            if (data.CombinedException != null)
            {
                s.AppendLine();
                s.Append(" One or more exceptions were included. ");
                s.Append(RenderObject(data, data.CombinedException, objectRenderer));
            }

            s.AppendLine();

            return(new AnnotatedFormattable(s.ToString()));
        }