Пример #1
0
        public void TestDefaultValue()
        {
            AttributeType type = new AttributeType("test", typeof(string));
            AttributeInfo test = new AttributeInfo("test", type);
            test.DefaultValue = "foo";
            Assert.AreEqual(test.DefaultValue, "foo");
            test.DefaultValue = null;
            Assert.AreEqual(test.DefaultValue, type.GetDefault());
            Assert.Throws<InvalidOperationException>(delegate { test.DefaultValue = 1; });

            AttributeType length2Type = new AttributeType("length2Type", typeof(int[]), 2);
            AttributeInfo length2Info = new AttributeInfo("length2", length2Type);
            Assert.AreEqual(length2Info.DefaultValue, length2Type.GetDefault());
            Assert.AreEqual(length2Info.DefaultValue, new int[] { default(int), default(int) });
            DomNodeType nodeType = new DomNodeType("testNodeType");
            nodeType.Define(length2Info);
            DomNode node = new DomNode(nodeType);
            Assert.AreEqual(node.GetAttribute(length2Info), length2Info.DefaultValue);
            node.SetAttribute(length2Info, new int[] { 1, 2 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1, 2 });
            node.SetAttribute(length2Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1 });

            AttributeType length1Type = new AttributeType("length1Type", typeof(int[]), 1);
            AttributeInfo length1Info = new AttributeInfo("length1", length1Type);
            Assert.AreEqual(length1Info.DefaultValue, length1Type.GetDefault());
            Assert.AreEqual(length1Info.DefaultValue, new int[] { default(int) });
            nodeType = new DomNodeType("testNodeType");
            nodeType.Define(length1Info);
            node = new DomNode(nodeType);
            Assert.AreEqual(node.GetAttribute(length1Info), length1Info.DefaultValue);
            node.SetAttribute(length1Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length1Info), new int[] { 1 });
        }
Пример #2
0
        private void TestArray <T>(AttributeTypes type)
        {
            AttributeType test = new AttributeType("test", typeof(T[]), 2);

            Assert.AreEqual(test.ClrType, typeof(T[]));
            Assert.AreEqual(test.Type, type);
            Assert.AreEqual(test.Length, 2);
            Assert.True(test.IsArray);
            //since length < int.MaxValue, default has 'length' elements
            Assert.AreEqual(test.GetDefault(), new T[] { default(T), default(T) });

            test = new AttributeType("testUnbounded", typeof(T[]), Int32.MaxValue);
            Assert.AreEqual(test.ClrType, typeof(T[]));
            Assert.AreEqual(test.Type, type);
            Assert.AreEqual(test.Length, Int32.MaxValue);
            Assert.True(test.IsArray);
            //since length == int.MaxValue, default has zero elements
            Assert.AreEqual(test.GetDefault(), new T[] { });

            test = new AttributeType("testDefaultLength", typeof(T[])); //default is length of 1
            Assert.AreEqual(test.ClrType, typeof(T[]));
            Assert.AreEqual(test.Type, type);
            Assert.AreEqual(test.Length, 1);
            Assert.True(test.IsArray);
            //since length == 1, default is T[] with 1 element
            Assert.AreEqual(test.GetDefault(), new T[] { default(T) });
        }
Пример #3
0
 public void TestConstructor()
 {
     AttributeType type = new AttributeType("test", typeof(string));
     AttributeInfo test = new AttributeInfo("test", type);
     Assert.AreEqual(test.Name, "test");
     Assert.AreEqual(test.Type, type);
     Assert.AreEqual(test.DefaultValue, type.GetDefault());
 }
Пример #4
0
        public void TestConstructor()
        {
            AttributeType type = new AttributeType("test", typeof(string));
            AttributeInfo test = new AttributeInfo("test", type);

            Assert.AreEqual(test.Name, "test");
            Assert.AreEqual(test.Type, type);
            Assert.AreEqual(test.DefaultValue, type.GetDefault());
        }
Пример #5
0
        private void TestScalar <T>(AttributeTypes type)
        {
            AttributeType test = new AttributeType("test", typeof(T));

            Assert.AreEqual(test.ClrType, typeof(T));
            Assert.AreEqual(test.Type, type);

            // test default for value types
            object typeDefault = default(T);

            if (typeDefault != null)
            {
                Assert.AreEqual(test.GetDefault(), typeDefault);
            }
        }
Пример #6
0
        public void TestDefaultValue()
        {
            AttributeType type = new AttributeType("test", typeof(string));
            AttributeInfo test = new AttributeInfo("test", type);

            test.DefaultValue = "foo";
            Assert.AreEqual(test.DefaultValue, "foo");
            test.DefaultValue = null;
            Assert.AreEqual(test.DefaultValue, type.GetDefault());
            Assert.Throws <InvalidOperationException>(delegate { test.DefaultValue = 1; });

            AttributeType length2Type = new AttributeType("length2Type", typeof(int[]), 2);
            AttributeInfo length2Info = new AttributeInfo("length2", length2Type);

            Assert.AreEqual(length2Info.DefaultValue, length2Type.GetDefault());
            Assert.AreEqual(length2Info.DefaultValue, new int[] { default(int), default(int) });
            DomNodeType nodeType = new DomNodeType("testNodeType");

            nodeType.Define(length2Info);
            DomNode node = new DomNode(nodeType);

            Assert.AreEqual(node.GetAttribute(length2Info), length2Info.DefaultValue);
            node.SetAttribute(length2Info, new int[] { 1, 2 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1, 2 });
            node.SetAttribute(length2Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length2Info), new int[] { 1 });

            AttributeType length1Type = new AttributeType("length1Type", typeof(int[]), 1);
            AttributeInfo length1Info = new AttributeInfo("length1", length1Type);

            Assert.AreEqual(length1Info.DefaultValue, length1Type.GetDefault());
            Assert.AreEqual(length1Info.DefaultValue, new int[] { default(int) });
            nodeType = new DomNodeType("testNodeType");
            nodeType.Define(length1Info);
            node = new DomNode(nodeType);
            Assert.AreEqual(node.GetAttribute(length1Info), length1Info.DefaultValue);
            node.SetAttribute(length1Info, new int[] { 1 });
            Assert.AreEqual(node.GetAttribute(length1Info), new int[] { 1 });
        }
Пример #7
0
        public void TestScalarConstructor()
        {
            // fully test one type
            {
                AttributeType test = new AttributeType("test", typeof(SByte));
                Assert.AreEqual(test.Name, "test");
                Assert.AreEqual(test.ClrType, typeof(SByte));
                Assert.AreEqual(test.Type, AttributeTypes.Int8);
                Assert.AreEqual(test.Length, 1);
                Assert.False(test.IsArray);

                Assert.AreEqual((SByte)test.GetDefault(), (SByte)0);
                // test validation of type, without any custom rules
                Assert.True(test.Validate((SByte)1, null));
                Assert.False(test.Validate((Int32)1, null));

                Assert.AreEqual(test.GetDefault(), (SByte)0);
            }

            // test an unrecognized type
            {
                AttributeType test = new AttributeType("test", typeof(AttributeType[])); // unrecognized type is treated as string
                Assert.AreEqual(test.ClrType, typeof(String));
                Assert.AreEqual(test.Type, AttributeTypes.String);
                Assert.AreEqual(test.Length, 1);
                Assert.False(test.IsArray);
            }

            // test all other types
            TestScalar <Byte>(AttributeTypes.UInt8);
            TestScalar <Int16>(AttributeTypes.Int16);
            TestScalar <UInt16>(AttributeTypes.UInt16);
            TestScalar <Int32>(AttributeTypes.Int32);
            TestScalar <UInt32>(AttributeTypes.UInt32);
            TestScalar <Int64>(AttributeTypes.Int64);
            TestScalar <UInt64>(AttributeTypes.UInt64);
            TestScalar <Single>(AttributeTypes.Single);
            TestScalar <Double>(AttributeTypes.Double);
            TestScalar <Decimal>(AttributeTypes.Decimal);
            TestScalar <Boolean>(AttributeTypes.Boolean);
            TestScalar <DateTime>(AttributeTypes.DateTime);

            TestScalar <String>(AttributeTypes.String);
            Assert.AreEqual(new AttributeType("test", typeof(string)).GetDefault(), string.Empty);

            TestScalar <Uri>(AttributeTypes.Uri);
            Assert.Null(new AttributeType("test", typeof(Uri)).GetDefault());

            TestScalar <DomNode>(AttributeTypes.Reference);
            Assert.Null(new AttributeType("test", typeof(DomNode)).GetDefault());

            TestArray <SByte>(AttributeTypes.Int8Array);
            TestArray <Byte>(AttributeTypes.UInt8Array);
            TestArray <Int16>(AttributeTypes.Int16Array);
            TestArray <UInt16>(AttributeTypes.UInt16Array);
            TestArray <Int32>(AttributeTypes.Int32Array);
            TestArray <UInt32>(AttributeTypes.UInt32Array);
            TestArray <Int64>(AttributeTypes.Int64Array);
            TestArray <UInt64>(AttributeTypes.UInt64Array);
            TestArray <Single>(AttributeTypes.SingleArray);
            TestArray <Double>(AttributeTypes.DoubleArray);
            TestArray <Decimal>(AttributeTypes.DecimalArray);
            TestArray <Boolean>(AttributeTypes.BooleanArray);
            TestArray <String>(AttributeTypes.StringArray);
        }