DecoderReplacementFallbackTest.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. [ExpectedException (typeof (InvalidOperationException))]
  51. public void DontChangeReadOnlyCodePageDecoderFallback ()
  52. {
  53. Encoding.GetEncoding (932).DecoderFallback =
  54. new DecoderReplacementFallback ();
  55. }
  56. [Test]
  57. [ExpectedException (typeof (InvalidOperationException))]
  58. public void CustomEncodingSetEncoderFallback ()
  59. {
  60. new MyEncoding ().DecoderFallback =
  61. new DecoderReplacementFallback ();
  62. }
  63. [Test]
  64. [ExpectedException (typeof (InvalidOperationException))]
  65. public void EncodingSetNullDecoderFallback ()
  66. {
  67. Encoding.Default.DecoderFallback = null;
  68. }
  69. [Test]
  70. // Don't throw an exception
  71. public void SetDecoderFallback ()
  72. {
  73. Encoding.Default.GetDecoder ().Fallback =
  74. new DecoderReplacementFallback ();
  75. }
  76. [Test]
  77. [ExpectedException (typeof (ArgumentNullException))]
  78. public void DecoderSetNullFallback ()
  79. {
  80. Encoding.Default.GetDecoder ().Fallback = null;
  81. }
  82. }
  83. }
  84. #endif