DecoderReplacementFallbackTest.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // DecoderReplacementFallback.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. //
  9. #if NET_2_0
  10. using System;
  11. using System.IO;
  12. using System.Text;
  13. using NUnit.Framework;
  14. namespace MonoTests.System.Text
  15. {
  16. [TestFixture]
  17. public class DecoderReplacementFallbackTest
  18. {
  19. [Test]
  20. public void Defaults ()
  21. {
  22. DecoderReplacementFallback f =
  23. new DecoderReplacementFallback ();
  24. Assert.AreEqual ("?", f.DefaultString, "#1");
  25. Assert.AreEqual (1, f.MaxCharCount, "#2");
  26. f = new DecoderReplacementFallback (String.Empty);
  27. Assert.AreEqual (String.Empty, f.DefaultString, "#3");
  28. Assert.AreEqual (0, f.MaxCharCount, "#4");
  29. f = Encoding.UTF8.DecoderFallback as DecoderReplacementFallback;
  30. Assert.IsNotNull (f, "#5");
  31. // This behavior was introduced as
  32. // http://support.microsoft.com/kb/940521/
  33. Assert.AreEqual ("\uFFFD", f.DefaultString, "#6");
  34. Assert.AreEqual (1, f.MaxCharCount, "#7");
  35. // after beta2 this test became invalid.
  36. //f = new MyEncoding ().DecoderFallback as DecoderReplacementFallback;
  37. //Assert.IsNotNull (f, "#8");
  38. //Assert.AreEqual (String.Empty, f.DefaultString, "#9");
  39. //Assert.AreEqual (0, f.MaxCharCount, "#10");
  40. f = DecoderFallback.ReplacementFallback as DecoderReplacementFallback;
  41. Assert.AreEqual ("?", f.DefaultString, "#11");
  42. Assert.AreEqual (1, f.MaxCharCount, "#12");
  43. }
  44. [Test]
  45. [ExpectedException (typeof (InvalidOperationException))]
  46. public void DontChangeReadOnlyUTF8DecoderFallback ()
  47. {
  48. Encoding.UTF8.DecoderFallback =
  49. new DecoderReplacementFallback ();
  50. }
  51. [Test]
  52. public void DontChangeReadOnlyCodePageDecoderFallback ()
  53. {
  54. Encoding encoding = Encoding.GetEncoding (Encoding.Default.CodePage);
  55. try {
  56. encoding.DecoderFallback = new DecoderReplacementFallback ();
  57. Assert.Fail ("#1");
  58. } catch (InvalidOperationException ex) {
  59. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  60. Assert.IsNull (ex.InnerException, "#3");
  61. Assert.IsNotNull (ex.Message, "#4");
  62. }
  63. }
  64. [Test]
  65. [ExpectedException (typeof (InvalidOperationException))]
  66. public void CustomEncodingSetEncoderFallback ()
  67. {
  68. new MyEncoding ().DecoderFallback =
  69. new DecoderReplacementFallback ();
  70. }
  71. [Test]
  72. [ExpectedException (typeof (InvalidOperationException))]
  73. public void EncodingSetNullDecoderFallback ()
  74. {
  75. Encoding.Default.DecoderFallback = null;
  76. }
  77. [Test]
  78. // Don't throw an exception
  79. public void SetDecoderFallback ()
  80. {
  81. Encoding.Default.GetDecoder ().Fallback =
  82. new DecoderReplacementFallback ();
  83. }
  84. [Test]
  85. [ExpectedException (typeof (ArgumentNullException))]
  86. public void DecoderSetNullFallback ()
  87. {
  88. Encoding.Default.GetDecoder ().Fallback = null;
  89. }
  90. }
  91. }
  92. #endif