public void CursorConverter_GetStandardValuesSupported_Invoke_ReturnsTrue()
        {
            var converter = new CursorConverter();

            Assert.True(converter.GetStandardValuesSupported());
        }
        public void CursorConverter_ConvertTo_ValueNotCursor_ThrowsNotSupportedException(Type destinationType)
        {
            var converter = new CursorConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertTo(1, destinationType));
        }
        public void CursorConverter_CanConvertFrom_Invoke_ReturnsExpected(Type sourceType, bool expected)
        {
            var converter = new CursorConverter();

            Assert.Equal(expected, converter.CanConvertFrom(sourceType));
        }
        public void CursorConverter_ConvertTo_NullToByteArray_ReturnsExpected()
        {
            var converter = new CursorConverter();

            Assert.Empty(Assert.IsType <byte[]>(converter.ConvertTo(null, typeof(byte[]))));
        }
        public void CursorConverter_ConvertTo_NullDestinationType_ThrowsArgumentNullException()
        {
            var converter = new CursorConverter();

            Assert.Throws <ArgumentNullException>("destinationType", () => converter.ConvertTo(new object(), null));
        }
        public void CursorConverter_ConvertTo_NullToInstanceDescriptor_ThrowsNotSupportedException()
        {
            var converter = new CursorConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertTo(null, typeof(InstanceDescriptor)));
        }
        public void CursorConverter_ConvertTo_UnknownToByteArray_ThrowsFormatException()
        {
            var converter = new CursorConverter();

            Assert.Throws <InvalidOperationException>(() => converter.ConvertTo(new Cursor((IntPtr)2), typeof(byte[])));
        }
Example #8
0
        /// <summary>
        /// create a CodeExpression to generate an object
        /// </summary>
        /// <param name="v">the object to be generated</param>
        /// <returns>the CodeExpression generating the object</returns>
        public static CodeExpression ObjectCreationCode(object v)
        {
            //===simple values===
            if (v == null)
            {
                return(new CodePrimitiveExpression(null));
            }
            if (v == System.DBNull.Value)
            {
                return(new CodePropertyReferenceExpression(new CodeTypeReferenceExpression(typeof(System.DBNull)), "Value"));
            }

            Type t = v.GetType();

            if (t.Equals(typeof(EventArgs)))
            {
                return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EventArgs)), "Empty"));
            }
            if (t.IsPrimitive)
            {
                if (t.Equals(typeof(IntPtr)) || t.Equals(typeof(UIntPtr)))
                {
                    CodeObjectCreateExpression coce;
                    if (t.Equals(typeof(IntPtr)))
                    {
                        IntPtr val = (IntPtr)v;
                        if (IntPtr.Size == 4)
                        {
                            coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToInt32()));
                        }
                        else
                        {
                            coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToInt64()));
                        }
                    }
                    else
                    {
                        UIntPtr val = (UIntPtr)v;
                        if (IntPtr.Size == 4)
                        {
                            coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToUInt32()));
                        }
                        else
                        {
                            coce = new CodeObjectCreateExpression(t, new CodePrimitiveExpression(val.ToUInt64()));
                        }
                    }
                    return(coce);
                }
                else
                {
                    CodePrimitiveExpression cpi = new CodePrimitiveExpression(v);
                    if (VPLUtil.IsNumber(t))
                    {
                        if (!typeof(int).Equals(t))
                        {
                            return(new CodeCastExpression(t, cpi));
                        }
                    }
                    return(cpi);
                }
            }
            if (t.Equals(typeof(string)))
            {
                return(new CodePrimitiveExpression(v));
            }
            if (t.IsEnum)
            {
                return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(t), v.ToString()));
            }
            if (t.IsArray)
            {
                Type  te = t.GetElementType();
                Array a  = (Array)v;
                if (a.Rank == 1)
                {
                    CodeExpression[] inits = new CodeExpression[a.Length];
                    for (int i = 0; i < inits.Length; i++)
                    {
                        object val = a.GetValue(i);
                        if (val == null)
                        {
                            inits[i] = GetDefaultValueExpression(te);
                        }
                        else
                        {
                            inits[i] = ObjectCreationCodeGen.ObjectCreationCode(val);
                        }
                    }
                    CodeArrayCreateExpression ac = new CodeArrayCreateExpression(t, inits);
                    return(ac);
                }
                else
                {
                    StringBuilder sb = new StringBuilder(CreateArrayCreationCodeString(a.Rank, te.Name));
                    sb.Append("{");
                    for (int i = 0; i < a.Rank; i++)
                    {
                        if (i > 0)
                        {
                            sb.Append(",");
                        }
                        sb.Append("{");
                        for (int j = a.GetLowerBound(i); j <= a.GetUpperBound(i); j++)
                        {
                            if (j > 0)
                            {
                                sb.Append(",");
                            }
                            sb.Append(ObjectCreationCodeGen.GetObjectCreationCodeSnippet(a.GetValue(i, j)));
                        }
                        sb.Append("}");
                    }
                    sb.Append("}");
                    return(new CodeSnippetExpression(sb.ToString()));
                }
            }
            //===use plug-ins to handle it===
            if (creators != null)
            {
                foreach (IGetObjectCreationCode occ in creators.Values)
                {
                    CodeExpression ce = occ.ObjectCreationCode(v);
                    if (ce != null)
                    {
                        return(ce);
                    }
                }
            }
            //===use default handlers===
            if (t.Equals(typeof(Size)))
            {
                Size v1 = (Size)v;
                return(new CodeObjectCreateExpression(t,
                                                      new CodePrimitiveExpression(v1.Width),
                                                      new CodePrimitiveExpression(v1.Height)));
            }
            if (t.Equals(typeof(Point)))
            {
                Point v1 = (Point)v;
                return(new CodeObjectCreateExpression(t,
                                                      new CodePrimitiveExpression(v1.X),
                                                      new CodePrimitiveExpression(v1.Y)));
            }
            if (t.Equals(typeof(Color)))
            {
                Color v1 = (Color)v;
                return(new CodeMethodInvokeExpression(new CodeMethodReferenceExpression(new CodeTypeReferenceExpression(typeof(Color)), "FromArgb"),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.A)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.R)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.G)),
                                                      new CodeCastExpression(typeof(int), new CodePrimitiveExpression(v1.B))
                                                      ));
            }
            if (t.Equals(typeof(Cursor)))
            {
                //System.Windows.Forms.Cursors.
                Cursor v1 = (Cursor)v;
                if (v1 == Cursors.AppStarting)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "AppStarting"));
                }
                else if (v1 == Cursors.Arrow)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Arrow"));
                }
                else if (v1 == Cursors.Cross)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Cross"));
                }
                else if (v1 == Cursors.Default)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Default"));
                }
                else if (v1 == Cursors.Hand)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Hand"));
                }
                else if (v1 == Cursors.Help)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "Help"));
                }
                else if (v1 == Cursors.HSplit)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "HSplit"));
                }
                else if (v1 == Cursors.IBeam)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "IBeam"));
                }
                else if (v1 == Cursors.No)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "No"));
                }
                else if (v1 == Cursors.NoMove2D)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMove2D"));
                }
                else if (v1 == Cursors.NoMoveHoriz)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMoveHoriz"));
                }
                else if (v1 == Cursors.NoMoveVert)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "NoMoveVert"));
                }
                else if (v1 == Cursors.PanEast)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanEast"));
                }
                else if (v1 == Cursors.PanNE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNE"));
                }
                else if (v1 == Cursors.PanNorth)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNorth"));
                }
                else if (v1 == Cursors.PanNW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanNW"));
                }
                else if (v1 == Cursors.PanSE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSE"));
                }
                else if (v1 == Cursors.PanSouth)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSouth"));
                }
                else if (v1 == Cursors.PanSW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanSW"));
                }
                else if (v1 == Cursors.PanWest)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "PanWest"));
                }
                else if (v1 == Cursors.SizeAll)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeAll"));
                }
                else if (v1 == Cursors.SizeNESW)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNESW"));
                }
                else if (v1 == Cursors.SizeNS)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNS"));
                }
                else if (v1 == Cursors.SizeNWSE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeNWSE"));
                }
                else if (v1 == Cursors.SizeWE)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "SizeWE"));
                }
                else if (v1 == Cursors.UpArrow)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "UpArrow"));
                }
                else if (v1 == Cursors.VSplit)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "VSplit"));
                }
                else if (v1 == Cursors.WaitCursor)
                {
                    return(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(Cursors)), "WaitCursor"));
                }
                else
                {
                    CursorConverter cc = new CursorConverter();
                    string          s  = cc.ConvertToString(v);
                    return(new CodeMethodInvokeExpression(
                               new CodeObjectCreateExpression(typeof(CursorConverter), null),
                               "ConvertFromString",
                               new CodePrimitiveExpression(s)
                               ));
                }
            }
            if (t.Equals(typeof(Font)))
            {
                Font f = (Font)v;
                return(new CodeObjectCreateExpression(typeof(Font), new CodePrimitiveExpression(f.FontFamily.ToString()), new CodePrimitiveExpression(f.Size)));
            }
            //use string converter ===================================
            TypeConverter sc = TypeDescriptor.GetConverter(t);

            if (sc != null)
            {
                if (sc.CanConvertTo(typeof(string)) && sc.CanConvertFrom(typeof(string)))
                {
                    string svalue = sc.ConvertToInvariantString(v);
                    CodeMethodInvokeExpression mgc = new CodeMethodInvokeExpression(new CodeTypeReferenceExpression(typeof(TypeDescriptor)), "GetConverter", new CodeTypeOfExpression(t));
                    CodeMethodInvokeExpression fs  = new CodeMethodInvokeExpression(mgc, "ConvertFromInvariantString", new CodePrimitiveExpression(svalue));
                    //
                    return(new CodeCastExpression(t, fs));
                }
            }
            //===use TypeConverter as the last resort===
            PropertyDescriptorCollection pifs = TypeDescriptor.GetProperties(v);

            if (pifs == null || pifs.Count == 0)
            {
                return(new CodeObjectCreateExpression(t));
            }
            int nCount = 0;

            for (int i = 0; i < pifs.Count; i++)
            {
                if (pifs[i].IsBrowsable && !pifs[i].IsReadOnly)
                {
                    nCount++;
                }
            }
            if (nCount == 0)
            {
                return(new CodeObjectCreateExpression(t));
            }
            string sCodeSnippet = GetObjectCreationCodeSnippet(v);

            return(new CodeSnippetExpression(sCodeSnippet));
        }
        public void CursorConverter_ConvertTo_ValueNotCursorToString_ReturnsExpected(object value, string expected)
        {
            var converter = new CursorConverter();

            Assert.Equal(expected, converter.ConvertTo(value, typeof(string)));
        }
Example #10
0
        public void CursorConverter_ConvertTo_UnknownToString_ThrowsFormatException()
        {
            var converter = new CursorConverter();

            Assert.Throws <FormatException>(() => converter.ConvertTo(new Cursor((IntPtr)2), typeof(string)));
        }
Example #11
0
        public void CursorConverter_CanConvertTo_Invoke_ReturnsExpected(Type destinationType, bool expected)
        {
            var converter = new CursorConverter();

            Assert.Equal(expected, converter.CanConvertTo(destinationType));
        }
Example #12
0
        public void CursorConverter_ConvertFrom_InvalidByteArray_ThrowsArgumentException()
        {
            var converter = new CursorConverter();

            Assert.Throws <ArgumentException>("stream", () => converter.ConvertFrom(new byte[0]));
        }
Example #13
0
        public void CursorConverter_ConvertFrom_InvalidValue_ThrowsNotSupportedException(object value)
        {
            var converter = new CursorConverter();

            Assert.Throws <NotSupportedException>(() => converter.ConvertFrom(value));
        }
Example #14
0
        public void CursorConverter_ConvertFrom_KnownCursor_ReturnsExpected(string value)
        {
            var converter = new CursorConverter();

            Assert.Same(Cursors.AppStarting, converter.ConvertFrom(value));
        }