StringValueConverterTest.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // StringValueConverterTest.cs
  3. //
  4. // Author:
  5. // Gert Driesen ([email protected])
  6. //
  7. // Copyright (C) 2008 Gert Driesen
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.ComponentModel;
  30. using System.ComponentModel.Design.Serialization;
  31. using System.Diagnostics.Design;
  32. using System.Reflection;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Diagnostics.Design
  35. {
  36. [TestFixture]
  37. public class StringValueConverterTest
  38. {
  39. TypeConverter converter;
  40. [SetUp]
  41. public void SetUp ()
  42. {
  43. Assembly a = typeof (LogConverter).Assembly;
  44. Type type = a.GetType ("System.Diagnostics.Design.StringValueConverter");
  45. Assert.IsNotNull (type);
  46. converter = (TypeConverter) Activator.CreateInstance (type);
  47. }
  48. [Test]
  49. public void CanConvertFrom ()
  50. {
  51. Assert.IsTrue (converter.CanConvertFrom (typeof (string)), "#1");
  52. Assert.IsFalse (converter.CanConvertFrom (typeof (int)), "#2");
  53. Assert.IsTrue (converter.CanConvertFrom (typeof (InstanceDescriptor)), "#3");
  54. }
  55. [Test]
  56. public void CanConvertTo ()
  57. {
  58. Assert.IsTrue (converter.CanConvertTo (typeof (string)), "#1");
  59. Assert.IsFalse (converter.CanConvertTo (typeof (int)), "#2");
  60. Assert.IsFalse (converter.CanConvertTo (typeof (InstanceDescriptor)), "#3");
  61. }
  62. [Test]
  63. public void ConvertFrom ()
  64. {
  65. Assert.AreEqual ("AbC", converter.ConvertFrom ("AbC"), "#1");
  66. Assert.IsNull (converter.ConvertFrom (string.Empty), "#2");
  67. Assert.IsNull (converter.ConvertFrom (" \r "), "#3");
  68. Assert.AreEqual ("AbC", converter.ConvertFrom (" \nAbC\r "), "#4");
  69. }
  70. [Test]
  71. public void ConvertFrom_Value_Null ()
  72. {
  73. try {
  74. converter.ConvertFrom (null);
  75. Assert.Fail ("#1");
  76. } catch (NotSupportedException ex) {
  77. // StringValueConverter cannot convert from (null)
  78. Assert.AreEqual (typeof (NotSupportedException), ex.GetType (), "#2");
  79. Assert.IsNull (ex.InnerException, "#3");
  80. Assert.IsNotNull (ex.Message, "#4");
  81. Assert.IsTrue (ex.Message.IndexOf ("StringValueConverter") != -1, "#5");
  82. Assert.IsTrue (ex.Message.IndexOf ("(null)") != -1, "#6");
  83. }
  84. }
  85. [Test]
  86. public void ConvertTo ()
  87. {
  88. Assert.AreEqual ("AbC", converter.ConvertTo ("AbC", typeof (string)), "#1");
  89. Assert.AreEqual (string.Empty, converter.ConvertTo (string.Empty, typeof (string)), "#2");
  90. Assert.AreEqual (" \r ", converter.ConvertTo (" \r ", typeof (string)), "#3");
  91. Assert.AreEqual (" \nAbC\r ", converter.ConvertTo (" \nAbC\r ", typeof (string)), "#4");
  92. }
  93. }
  94. }