* CodeTypeReferenceTest.cs: Added tests for CodeTypeParameter ctor,
authorGert Driesen <drieseng@users.sourceforge.net>
Sun, 2 Oct 2005 14:57:07 +0000 (14:57 -0000)
committerGert Driesen <drieseng@users.sourceforge.net>
Sun, 2 Oct 2005 14:57:07 +0000 (14:57 -0000)
and string (basetype) ctor.
* CodeTypeReference.cs: Added default ctor for 2.0 profile. Set
Options to CodeReferenceOptions.GenericTypeParameter when instance
is constructed using ctor taking CodeTypeParameter argument. Modified
BaseType property to return BaseType of ArrayElementType if
available. Fixed some bugs in array and typeparameter support.

svn path=/trunk/mcs/; revision=51089

mcs/class/System/System.CodeDom/ChangeLog
mcs/class/System/System.CodeDom/CodeTypeReference.cs
mcs/class/System/Test/System.CodeDom/ChangeLog
mcs/class/System/Test/System.CodeDom/CodeTypeReferenceTest.cs

index 9c5de3a1b1e501d5167ca7f7ef210c75ff861deb..d1560b7013efedf9834d62fe023630494237eac5 100644 (file)
@@ -1,3 +1,11 @@
+2005-10-02 Gert Driesen <drieseng@users.sourceforge.net>
+
+       * CodeTypeReference.cs: Added default ctor for 2.0 profile. Set
+       Options to CodeReferenceOptions.GenericTypeParameter when instance
+       is constructed using ctor taking CodeTypeParameter argument. Modified
+       BaseType property to return BaseType of ArrayElementType if
+       available. Fixed some bugs in array and typeparameter support.
+
 2005-07-24 Gert Driesen <drieseng@users.sourceforge.net>
 
        * CodeTypeReference.cs: Added internal IsInterface property.
index f76e0c1751d3a756f36c1ade78192d6f365781fd..b31c221b7ce434121e7aee782294315579901e79 100644 (file)
@@ -41,7 +41,7 @@ namespace System.CodeDom
        {
                private string baseType;
                private CodeTypeReference arrayType;
-               private int rank;\r
+               private int rank;
                private bool isInterface;
 
 #if NET_2_0
@@ -52,7 +52,14 @@ namespace System.CodeDom
                //
                // Constructors
                //
-               public CodeTypeReference( string baseType )
+
+#if NET_2_0
+               public CodeTypeReference ()
+               {
+               }
+#endif
+
+               public CodeTypeReference (string baseType)
                {
                        if (baseType == null || baseType.Length == 0) {
                                this.baseType = typeof (void).FullName;
@@ -64,18 +71,42 @@ namespace System.CodeDom
                                this.baseType = baseType;
                                return;
                        }
-                       string[] args = baseType.Split (',');
 
-#if NET_2_0
                        int array_end = baseType.LastIndexOf (']');
+                       if (array_end < array_start) {
+                               this.baseType = baseType;
+                               return;
+                       }
+
+                       string[] args = baseType.Substring (array_start + 1, array_end - array_start - 1).Split (',');
 
+#if NET_2_0
                        if ((array_end - array_start) != args.Length) {
+                               this.baseType = baseType.Substring(0, array_start);
+                               foreach (string arg in args) {
+                                       if (arg.Length != 0) {
+                                               TypeArguments.Add (new CodeTypeReference (arg));
+                                       }
+                               }
+                       } else {
                                arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
-                               array_start++;
-                               TypeArguments.Add (new CodeTypeReference (baseType.Substring (array_start, array_end - array_start)));
-                       } else
+                               rank = args.Length;
+                       }
+#else
+                       bool isArray = true;
+                       foreach (string arg in args) {
+                               if (arg.Length != 0) {
+                                       isArray = false;
+                                       break;
+                               }
+                       }
+                       if (isArray) {
+                               arrayType = new CodeTypeReference (baseType.Substring (0, array_start));
+                               rank = args.Length;
+                       } else {
+                               this.baseType = baseType;
+                       }
 #endif
-                               arrayType = new CodeTypeReference (baseType.Substring (0, array_start), args.Length);
                }
                
                public CodeTypeReference( Type baseType )
@@ -84,14 +115,14 @@ namespace System.CodeDom
                        if (baseType == null) {
                                throw new ArgumentNullException ("baseType");
                        }
-#endif\r
-                       if (baseType.IsArray) {\r
-                               this.rank = baseType.GetArrayRank ();\r
-                               this.arrayType = new CodeTypeReference (baseType.GetElementType ());\r
-                               this.baseType = arrayType.BaseType;\r
-                       } else {\r
-                               this.baseType = baseType.FullName;\r
-                       }\r
+#endif
+                       if (baseType.IsArray) {
+                               this.rank = baseType.GetArrayRank ();
+                               this.arrayType = new CodeTypeReference (baseType.GetElementType ());
+                               this.baseType = arrayType.BaseType;
+                       } else {
+                               this.baseType = baseType.FullName;
+                       }
                        this.isInterface = baseType.IsInterface;
                }
 
@@ -106,11 +137,12 @@ namespace System.CodeDom
                        : this (new CodeTypeReference (baseType), rank)
                {
                }
-                       
+
 #if NET_2_0
                public CodeTypeReference( CodeTypeParameter typeParameter ) :
                        this (typeParameter.Name)
                {
+                       this.codeTypeReferenceOption = CodeTypeReferenceOptions.GenericTypeParameter;
                }
 
                public CodeTypeReference( string typeName, CodeTypeReferenceOptions codeTypeReferenceOption ) :
@@ -157,6 +189,10 @@ namespace System.CodeDom
 
                public string BaseType {
                        get {
+                               if (arrayType != null) {
+                                       return arrayType.BaseType;
+                               }
+
                                if (baseType == null)
                                        return String.Empty;
 
@@ -165,10 +201,10 @@ namespace System.CodeDom
                        set {
                                baseType = value;
                        }
-               }\r
-\r
-               internal bool IsInterface {\r
-                       get { return isInterface; }\r
+               }
+
+               internal bool IsInterface {
+                       get { return isInterface; }
                }
 
 #if NET_2_0
index 51f8242fce69914e9fa015e27d1e2706bf5ac1bd..c89cea41c1c85dcbd9244b32831bca14033b6f8e 100644 (file)
@@ -1,3 +1,8 @@
+2005-10-02  Gert Driesen  <drieseng@users.sourceforge.net>
+
+       * CodeTypeReferenceTest.cs: Added tests for CodeTypeParameter ctor,
+       and string (basetype) ctor.
+
 2005-07-24  Gert Driesen  <drieseng@users.sourceforge.net>
 
        * CodeTypeDelegateTest.cs: Added tests for BaseTypes and ReturnType.
index 3c61b51da5bd9e9489ebe39cb4278aceb12e7299..7ec4d218775631c0343bf780035e95108ac4dcca 100644 (file)
@@ -23,15 +23,25 @@ namespace MonoTests.System.CodeDom
                        Assert.AreEqual (typeof (void).FullName, reference.BaseType);
                        Assert.AreEqual (0, reference.ArrayRank);
                        Assert.IsNull (reference.ArrayElementType);
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+#endif
                }
 
                [Test]
                public void NullTypeName ()
                {
                        CodeTypeReference reference = new CodeTypeReference ((string) null);
-                       Assert.AreEqual (typeof (void).FullName, reference.BaseType);
-                       Assert.AreEqual (0, reference.ArrayRank);
-                       Assert.IsNull (reference.ArrayElementType);
+                       Assert.AreEqual (typeof (void).FullName, reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+#endif
                }
 
                [Test]
@@ -44,5 +54,239 @@ namespace MonoTests.System.CodeDom
                {
                        new CodeTypeReference ((Type) null);
                }
+
+               [Test]
+               public void BaseTypeTest1 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ("A[B]");
+#if NET_2_0
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (1, reference.TypeArguments.Count, "#6");
+
+                       CodeTypeReference typeArgument = reference.TypeArguments[0];
+                       Assert.AreEqual ("B", typeArgument.BaseType, "#7");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#8");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#9");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#10");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#11");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#12");
+#else
+                       Assert.AreEqual ("A[B]", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+#endif
+               }
+
+               [Test]
+               public void BaseTypeTest2 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ("A[]");
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (1, reference.ArrayRank, "#2");
+                       Assert.IsNotNull (reference.ArrayElementType, "#3");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+#endif
+
+                       CodeTypeReference arrayElementType = reference.ArrayElementType;
+                       Assert.AreEqual ("A", arrayElementType.BaseType, "#7");
+                       Assert.AreEqual (0, arrayElementType.ArrayRank, "#8");
+                       Assert.IsNull (arrayElementType.ArrayElementType, "#9");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, arrayElementType.Options, "#10");
+                       Assert.IsNotNull (arrayElementType.TypeArguments, "#11");
+                       Assert.AreEqual (0, arrayElementType.TypeArguments.Count, "#12");
+#endif
+               }
+
+               [Test]
+               public void BaseTypeTest3 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ("A[,]");
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (2, reference.ArrayRank, "#2");
+                       Assert.IsNotNull (reference.ArrayElementType, "#3");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+#endif
+
+                       CodeTypeReference arrayElementType = reference.ArrayElementType;
+                       Assert.AreEqual ("A", arrayElementType.BaseType, "#7");
+                       Assert.AreEqual (0, arrayElementType.ArrayRank, "#8");
+                       Assert.IsNull (arrayElementType.ArrayElementType, "#9");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, arrayElementType.Options, "#10");
+                       Assert.IsNotNull (arrayElementType.TypeArguments, "#11");
+                       Assert.AreEqual (0, arrayElementType.TypeArguments.Count, "#12");
+#endif
+               }
+
+               [Test]
+               public void BaseTypeTest4 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ("A[B,C]");
+#if NET_2_0
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (2, reference.TypeArguments.Count, "#6");
+
+                       CodeTypeReference typeArgument = reference.TypeArguments[0];
+                       Assert.AreEqual ("B", typeArgument.BaseType, "#7");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#8");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#9");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#10");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#11");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#12");
+
+                       typeArgument = reference.TypeArguments[1];
+                       Assert.AreEqual ("C", typeArgument.BaseType, "#13");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#14");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#15");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#16");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#17");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#18");
+#else
+                       Assert.AreEqual ("A[B,C]", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+#endif
+               }
+
+               [Test]
+               public void BaseTypeTest5 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ("A[");
+                       Assert.AreEqual ("A[", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+#if NET_2_0
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+#endif
+               }
+
+#if NET_2_0
+               [Test]
+               public void Defaults ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference ();
+
+                       Assert.IsNull (reference.ArrayElementType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.AreEqual (string.Empty, reference.BaseType, "#3");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+               }
+
+               [Test]
+               public void CodeTypeParameter1 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference(
+                               new CodeTypeParameter ("A"));
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+               }
+       
+               [Test]
+               public void CodeTypeParameter2 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference (
+                               new CodeTypeParameter ("A[B]"));
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (1, reference.TypeArguments.Count, "#6");
+
+                       CodeTypeReference typeArgument = reference.TypeArguments[0];
+                       Assert.AreEqual ("B", typeArgument.BaseType, "#7");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#8");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#9");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#10");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#11");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#12");
+               }
+
+               [Test]
+               public void CodeTypeParameter3 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference (
+                               new CodeTypeParameter ("A[B, C]"));
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (2, reference.TypeArguments.Count, "#6");
+
+                       CodeTypeReference typeArgument = reference.TypeArguments[0];
+                       Assert.AreEqual ("B", typeArgument.BaseType, "#7");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#8");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#9");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#10");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#11");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#12");
+
+                       typeArgument = reference.TypeArguments[1];
+                       Assert.AreEqual (" C", typeArgument.BaseType, "#13");
+                       Assert.AreEqual (0, typeArgument.ArrayRank, "#14");
+                       Assert.IsNull (typeArgument.ArrayElementType, "#15");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, typeArgument.Options, "#16");
+                       Assert.IsNotNull (typeArgument.TypeArguments, "#17");
+                       Assert.AreEqual (0, typeArgument.TypeArguments.Count, "#18");
+               }
+
+               [Test]
+               public void CodeTypeParameter4 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference (
+                               new CodeTypeParameter ("A[]"));
+                       Assert.AreEqual ("A", reference.BaseType, "#1");
+                       Assert.AreEqual (1, reference.ArrayRank, "#2");
+                       Assert.IsNotNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+
+                       CodeTypeReference arrayElementType = reference.ArrayElementType;
+                       Assert.AreEqual ("A", arrayElementType.BaseType, "#7");
+                       Assert.AreEqual (0, arrayElementType.ArrayRank, "#8");
+                       Assert.IsNull (arrayElementType.ArrayElementType, "#9");
+                       Assert.AreEqual ((CodeTypeReferenceOptions) 0, arrayElementType.Options, "#10");
+                       Assert.IsNotNull (arrayElementType.TypeArguments, "#11");
+                       Assert.AreEqual (0, arrayElementType.TypeArguments.Count, "#12");
+               }
+
+               [Test]
+               public void CodeTypeParameter5 ()
+               {
+                       CodeTypeReference reference = new CodeTypeReference (
+                               new CodeTypeParameter ("A[,"));
+                       Assert.AreEqual ("A[,", reference.BaseType, "#1");
+                       Assert.AreEqual (0, reference.ArrayRank, "#2");
+                       Assert.IsNull (reference.ArrayElementType, "#3");
+                       Assert.AreEqual (CodeTypeReferenceOptions.GenericTypeParameter, reference.Options, "#4");
+                       Assert.IsNotNull (reference.TypeArguments, "#5");
+                       Assert.AreEqual (0, reference.TypeArguments.Count, "#6");
+               }
+#endif
        }
 }