DecoderReplacementFallbackTest.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. Assert.AreEqual (String.Empty, f.DefaultString, "#6");
  32. Assert.AreEqual (0, f.MaxCharCount, "#7");
  33. // after beta2 this test became invalid.
  34. //f = new MyEncoding ().DecoderFallback as DecoderReplacementFallback;
  35. //Assert.IsNotNull (f, "#8");
  36. //Assert.AreEqual (String.Empty, f.DefaultString, "#9");
  37. //Assert.AreEqual (0, f.MaxCharCount, "#10");
  38. f = DecoderFallback.ReplacementFallback as DecoderReplacementFallback;
  39. Assert.AreEqual ("?", f.DefaultString, "#11");
  40. Assert.AreEqual (1, f.MaxCharCount, "#12");
  41. }
  42. [Test]
  43. [ExpectedException (typeof (InvalidOperationException))]
  44. public void DontChangeReadOnlyUTF8DecoderFallback ()
  45. {
  46. Encoding.UTF8.DecoderFallback =
  47. new DecoderReplacementFallback ();
  48. }
  49. [Test]
  50. public void DontChangeReadOnlyCodePageDecoderFallback ()
  51. {
  52. Encoding encoding = Encoding.GetEncoding (Encoding.Default.CodePage);
  53. try {
  54. encoding.DecoderFallback = new DecoderReplacementFallback ();
  55. Assert.Fail ("#1");
  56. } catch (InvalidOperationException ex) {
  57. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#2");
  58. Assert.IsNull (ex.InnerException, "#3");
  59. Assert.IsNotNull (ex.Message, "#4");
  60. }
  61. }
  62. [Test]
  63. [ExpectedException (typeof (InvalidOperationException))]
  64. public void CustomEncodingSetEncoderFallback ()
  65. {
  66. new MyEncoding ().DecoderFallback =
  67. new DecoderReplacementFallback ();
  68. }
  69. [Test]
  70. [ExpectedException (typeof (InvalidOperationException))]
  71. public void EncodingSetNullDecoderFallback ()
  72. {
  73. Encoding.Default.DecoderFallback = null;
  74. }
  75. [Test]
  76. // Don't throw an exception
  77. public void SetDecoderFallback ()
  78. {
  79. Encoding.Default.GetDecoder ().Fallback =
  80. new DecoderReplacementFallback ();
  81. }
  82. [Test]
  83. [ExpectedException (typeof (ArgumentNullException))]
  84. public void DecoderSetNullFallback ()
  85. {
  86. Encoding.Default.GetDecoder ().Fallback = null;
  87. }
  88. }
  89. }
  90. #endif