Hi, The attached patch fixes some minor differences between our and MS' Encoding.GetEncoding implementation.
* GetEncoding (int): do not allow codepage value below zero and above 0xffff. * GetEncoding (string): modified NotSupportedException to ArgumentException when encoding is not supported Both changes are covered by unit tests. Let me know if it's ok to commit. Gert
Index: ChangeLog =================================================================== --- ChangeLog (revision 71903) +++ ChangeLog (working copy) @@ -1,3 +1,7 @@ +2007-01-31 Gert Driesen <[EMAIL PROTECTED]> + + * corlib_test.dll.sources: Added EncodingTest.cs. + 2007-01-08 Sebastien Pouliot <[EMAIL PROTECTED]> * corlib_test.dll.sources: Added new unit tests for bug #80439 to the Index: corlib_test.dll.sources =================================================================== --- corlib_test.dll.sources (revision 71903) +++ corlib_test.dll.sources (working copy) @@ -299,6 +299,7 @@ System.Text/EncoderReplacementFallbackTest.cs System.Text/EncoderReplacementFallbackBufferTest.cs System.Text/EncoderTest.cs +System.Text/EncodingTest.cs System.Text/EncodingInfoTest.cs System.Text/StringBuilderTest.cs System.Text/TestEncoding.cs Index: System.Text/ChangeLog =================================================================== --- System.Text/ChangeLog (revision 71903) +++ System.Text/ChangeLog (working copy) @@ -1,3 +1,9 @@ +2007-01-31 Gert Driesen <[EMAIL PROTECTED]> + + * Encoding.cs: In GetEncoding (int) do not allow codepage value below zero + and above 0xffff. Modified NotSupportedException to ArgumentException in + GetEncoding (string). + 2006-12-18 Atsushi Enomoto <[EMAIL PROTECTED]> * EncoderFallbackBuffer.cs : implement Reset(). Index: System.Text/Encoding.cs =================================================================== --- System.Text/Encoding.cs (revision 71903) +++ System.Text/Encoding.cs (working copy) @@ -1,5 +1,5 @@ /* - * Encoding.cs - Implementation of the "System.Text.Encoding" class. + * Encoding.cs - Implementation of the "System.Text.Encoding" class. * * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd * Copyright (c) 2002, Ximian, Inc. @@ -454,6 +454,10 @@ #endif static Encoding GetEncoding (int codePage) { + if (codePage < 0 || codePage > 0xffff) + throw new ArgumentOutOfRangeException ("codepage", + "Valid values are between 0 and 65535, inclusive."); + // Check for the builtin code pages first. switch (codePage) { case 0: return Default; @@ -695,7 +699,7 @@ } // We have no idea how to handle this encoding name. - throw new NotSupportedException (String.Format ("Encoding name `{0}' not supported", name)); + throw new ArgumentException (String.Format ("Encoding name `{0}' not supported", name)); } #endif // !ECMA_COMPAT Index: Test/System.Text/ChangeLog =================================================================== --- Test/System.Text/ChangeLog (revision 71903) +++ Test/System.Text/ChangeLog (working copy) @@ -1,3 +1,7 @@ +2007-01-31 Gert Driesen <[EMAIL PROTECTED]> + + * EncodingTest.cs: Added tests for GetEncoding. + 2006-07-18 Kornél Pál <[EMAIL PROTECTED]> * ASCIIEncodingTest.cs: Added TestGetString3 test. Index: Test/System.Text/EncodingTest.cs =================================================================== --- Test/System.Text/EncodingTest.cs (revision 0) +++ Test/System.Text/EncodingTest.cs (revision 0) @@ -0,0 +1,85 @@ +// +// EncodingTest.cs - Unit Tests for System.Text.Encoding +// +// Author: +// Gert Driesen ([EMAIL PROTECTED]) +// +// Copyright (C) 2007 Gert Driesen +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// + +using System; +using System.Text; + +using NUnit.Framework; + +namespace MonoTests.System.Text +{ + [TestFixture] + public class EncodingTest + { + [Test] + public void GetEncoding_CodePage_Default () + { + Assert.AreEqual (Encoding.Default, Encoding.GetEncoding (0)); + } + + [Test] + public void GetEncoding_CodePage_Invalid () + { + try { + Encoding.GetEncoding (-1); + Assert.Fail ("#A1"); + } catch (ArgumentOutOfRangeException ex) { + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#A2"); + Assert.IsNull (ex.InnerException, "#A3"); + Assert.IsNotNull (ex.Message, "#A4"); + Assert.IsNotNull (ex.ParamName, "#A5"); + Assert.AreEqual ("codepage", ex.ParamName, "#A6"); + } + + try { + Encoding.GetEncoding (65536); + Assert.Fail ("#B1"); + } catch (ArgumentOutOfRangeException ex) { + Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2"); + Assert.IsNull (ex.InnerException, "#B3"); + Assert.IsNotNull (ex.Message, "#B4"); + Assert.IsNotNull (ex.ParamName, "#B5"); + Assert.AreEqual ("codepage", ex.ParamName, "#B6"); + } + } + + [Test] + [ExpectedException (typeof (ArgumentException))] + public void GetEncoding_Name_NotSupported () + { + Encoding.GetEncoding ("doesnotexist"); + } + + [Test] + [ExpectedException (typeof (ArgumentNullException))] + public void GetEncoding_Name_Null () + { + Encoding.GetEncoding ((string) null); + } + } +} Property changes on: Test/System.Text/EncodingTest.cs ___________________________________________________________________ Name: svn:eol-style + native
_______________________________________________ Mono-devel-list mailing list Mono-devel-list@lists.ximian.com http://lists.ximian.com/mailman/listinfo/mono-devel-list