public void GetNode_WithException()
        {
            var ex = new InvalidOperationException("Invalid Operation");

            Crestron.SimplSharp.Reflection.ExtensionMethods.ExceptionToThrowOnGetCType = ex;
            Crestron.SimplSharp.Reflection.ExtensionMethods.QuantityToThrowOnGetCType  = 1;
            var o      = new object();
            var actual = DumpFactory.GetNode(o);

            Assert.AreEqual(typeof(DumpObjectFailure), actual.ValueType);

            Crestron.SimplSharp.Reflection.ExtensionMethods.ExceptionToThrowOnGetCType = null;
            Crestron.SimplSharp.Reflection.ExtensionMethods.QuantityToThrowOnGetCType  = 0;
        }
        public void GetNode_WithName_WithType_WithException()
        {
            var ex = new InvalidOperationException("Invalid Operation");

            Crestron.SimplSharp.Reflection.ExtensionMethods.ExceptionToThrowOnGetCType = ex;
            Crestron.SimplSharp.Reflection.ExtensionMethods.QuantityToThrowOnGetCType  = 1;
            var o       = new object();
            var node    = DumpFactory.GetNode(o, "Type", o.GetType());
            var content = node.ToString();

            Assert.IsTrue(content.Contains("Invalid Operation"));

            Crestron.SimplSharp.Reflection.ExtensionMethods.ExceptionToThrowOnGetCType = null;
            Crestron.SimplSharp.Reflection.ExtensionMethods.QuantityToThrowOnGetCType  = 0;
        }
        /// <summary>
        /// Populates the items nodes from a standard enumerable object.
        /// </summary>
        private void PopulateEnumerable()
        {
            var ienum = this.Value as IEnumerable;

            if (ienum == null)
            {
                return;
            }

            foreach (var item in ienum)
            {
                this.items.Add(DumpFactory.GetNode(item));
            }

            this.count = this.items.Count;
        }
        /// <summary>
        /// Populates the item nodes from a dictionary.
        /// </summary>
        private void PopulateDictionary()
        {
            var idict = this.Value as IDictionary;

            if (idict.Count == 0)
            {
                return;
            }

            var index   = 0;
            var padding = idict.Count.ToString().Length + 5;

            foreach (var key in idict.Keys)
            {
                this.items.Add(DumpFactory.GetNode(key, string.Format("Key{0}", index).PadRight(padding, ' ')));
                this.items.Add(DumpFactory.GetNode(idict[key], string.Format("Value{0}", index).PadRight(padding, ' ')));
                index++;
            }

            this.count = index;
        }
Example #5
0
        /// <summary>
        /// Populates the node's children.
        /// </summary>
        private void PopulateChildren()
        {
            var failures = new List <DumpObjectFailure>();

            try
            {
                if (this.Value != null)
                {
                    PropertyInfo[][] props = new PropertyInfo[2][];

                    try
                    {
                        props[0] = this.Value.GetType().GetCType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                    }
                    catch (Exception ex)
                    {
                        failures.Add(
                            new DumpObjectFailure()
                        {
                            ErrorMessage     = "Exception while attempting to get the public instance properties of this object.",
                            ExceptionMessage = ex.Message,
                            ExceptionType    = ex.GetType().FullName
                        });
                    }

                    try
                    {
                        props[1] = this.Value.GetType().GetCType().GetProperties(BindingFlags.Static | BindingFlags.Public);
                    }
                    catch (Exception ex)
                    {
                        failures.Add(
                            new DumpObjectFailure()
                        {
                            ErrorMessage     = "Exception while attempting to get the public static properties of this object.",
                            ExceptionMessage = ex.Message,
                            ExceptionType    = ex.GetType().FullName
                        });
                    }

                    var max = 0;
                    if (props[0] != null && props[0].Length > 0)
                    {
                        max = props[0].Max(p => p.Name.Length);
                    }

                    if (props[1] != null && props[1].Length > 0)
                    {
                        max = Math.Max(max, props[1].Max(p => p.Name.Length));
                    }

                    this.children.Clear();

                    for (var i = 0; i < props.Length; i++)
                    {
                        if (props[i] != null)
                        {
                            foreach (var prop in props[i])
                            {
                                try
                                {
                                    object value;
                                    if (i == 1)
                                    {
                                        value = prop.GetValue(null, null);
                                    }
                                    else
                                    {
                                        value = prop.GetValue(this.Value, null);
                                    }

                                    if (this.Value != value)
                                    {
                                        this.children.Add(DumpFactory.GetNode(value, prop.Name.PadRight(max), prop.PropertyType));
                                    }
                                }
                                catch (Exception ex)
                                {
                                    failures.Add(
                                        new DumpObjectFailure()
                                    {
                                        PropertyName     = prop.Name,
                                        ErrorMessage     = "Exception encountered while dumping property value.",
                                        ExceptionMessage = ex.Message,
                                        ExceptionType    = ex.GetType().FullName
                                    });
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                failures.Add(
                    new DumpObjectFailure()
                {
                    ErrorMessage     = "Uncaught exception while dumping object properties.",
                    ExceptionMessage = ex.Message,
                    ExceptionType    = ex.GetType().FullName
                });
            }

            if (failures.Count > 0)
            {
                this.children.Add(DumpFactory.GetNode(failures, "DumpFailures"));
            }
        }
 public void GetNode_WithOtherObject_Returns_DumpObject()
 {
     Assert.IsInstanceOfType(DumpFactory.GetNode(new Object()), typeof(DumpObject));
 }
        public void GetNode_WithIEnumerable_Returns_DumpCollection()
        {
            IEnumerable <string> ienum = new List <string>().AsEnumerable <string>();

            Assert.IsInstanceOfType(DumpFactory.GetNode(ienum), typeof(DumpCollection));
        }
 public void GetNode_WithIList_Returns_DumpCollection()
 {
     Assert.IsInstanceOfType(DumpFactory.GetNode(new List <string>()), typeof(DumpCollection));
 }
 public void GetNode_WithStringType_Returns_DumpNode()
 {
     Assert.IsInstanceOfType(DumpFactory.GetNode("123"), typeof(DumpNode));
 }
 public void GetNode_WithValueType_Returns_DumpNode()
 {
     Assert.IsInstanceOfType(DumpFactory.GetNode(123), typeof(DumpNode));
 }
 public void GetNode_WithNull_Returns_DumpNode()
 {
     Assert.IsInstanceOfType(DumpFactory.GetNode(null), typeof(DumpNode));
 }