[Mono-bugs] [Bug 604134] New: Enum.TryParse() fails to throw ArgumentException when the out argument is not an enum

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Mon May 10 02:50:51 EDT 2010


http://bugzilla.novell.com/show_bug.cgi?id=604134

http://bugzilla.novell.com/show_bug.cgi?id=604134#c0


           Summary: Enum.TryParse() fails to throw ArgumentException when
                    the out argument is not an enum
    Classification: Mono
           Product: Mono: Class Libraries
           Version: SVN
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: CORLIB
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: aenomoto at novell.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: Community User
           Blocker: ---


As the summary tells.

The bug is reported at:
http://smdn.invisiblefulmoon.net/misc/forum/programming/#n16
via: http://irc.gimite.net/channel/mono-jp/archive/20100421

Below are copied from above.

repro:

using System;

class EnumTest {
  public static void Main()
  {
    Console.WriteLine("{0} {1}", Environment.Version, Environment.OSVersion);

    try {
      Guid parsed;

      var ret = Enum.TryParse("val", out parsed);
      Console.WriteLine("ArgumentException not thrown");
      Console.WriteLine("{0} {1}", ret, parsed);
    }
    catch (ArgumentException ex) {
      Console.WriteLine(ex);
    }
  }
}

actual result:
- returns false and no error.

expected result:
- ArgumentException.

fix:
Index: System/Enum.cs
===================================================================
--- System/Enum.cs    (revision 155838)
+++ System/Enum.cs    (working copy)
@@ -628,9 +628,13 @@
         public static bool TryParse<TEnum> (string value, bool ignoreCase, out
TEnum result) where TEnum : struct
         {
             Type tenum_type = typeof (TEnum);
+
+            if (!tenum_type.IsEnum)
+                throw new ArgumentException("TEnum is not an Enum type.",
"enumType");
+
             result = default (TEnum);

-            if (value == null || value.Trim ().Length == 0 ||
!tenum_type.IsEnum)
+            if (value == null || value.Trim ().Length == 0)
                 return false;

             return Parse (tenum_type, value, ignoreCase, out result);

fix:

Index: Test/System/EnumTest.cs
===================================================================
--- Test/System/EnumTest.cs    (revision 155838)
+++ Test/System/EnumTest.cs    (working copy)
@@ -705,11 +705,18 @@
             Assert.AreEqual (false, success, "#D1");
             Assert.AreEqual (TestingEnum.This, result, "#D2");

-            // TryParse can accept any struct derived type
-            int n;
-            success = Enum.TryParse<int> ("31416", out n);
-            Assert.AreEqual (false, success, "#E1");
-            Assert.AreEqual (0, n, "#E2");
+            // TryParse throws ArgumentException if TEnum is not an
enumeration type
+            try {
+                int n;
+                Enum.TryParse<int> ("31416", out n);
+                Assert.Fail ("#E1");
+            } catch (ArgumentException ex) {
+                Assert.AreEqual (typeof (ArgumentException), ex.GetType (),
"#E2");
+                Assert.IsNull (ex.InnerException, "#E3");
+                Assert.IsNotNull (ex.Message, "#E4");
+                Assert.IsNotNull (ex.ParamName, "#E5");
+                Assert.AreEqual ("enumType", ex.ParamName, "#E6");
+            }
         }

         [Test]

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list