1 //
2 // Authors:
3 //	Marek Habersack <mhabersack@novell.com>
4 //
5 // Copyright (C) 2010 Novell, Inc (http://novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining
8 // a copy of this software and associated documentation files (the
9 // "Software"), to deal in the Software without restriction, including
10 // without limitation the rights to use, copy, modify, merge, publish,
11 // distribute, sublicense, and/or sell copies of the Software, and to
12 // permit persons to whom the Software is furnished to do so, subject to
13 // the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be
16 // included in all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 //
26 using System;
27 using System.ComponentModel;
28 
29 using NUnit.Framework;
30 
31 namespace MonoTests.System.ComponentModel
32 {
33 	[TestFixture]
34 	public class MultilineStringConverterTest
35 	{
36 		[Test]
GetPropertiesSupported()37 		public void GetPropertiesSupported ()
38 		{
39 			var cvt = new MultilineStringConverter ();
40 
41 			Assert.IsFalse (cvt.GetPropertiesSupported (), "#A1-1");
42 			Assert.IsFalse (cvt.GetPropertiesSupported (null), "#A1-2");
43 		}
44 
45 		[Test]
GetProperties()46 		public void GetProperties ()
47 		{
48 			var cvt = new MultilineStringConverter ();
49 
50 			Assert.IsNull (cvt.GetProperties (null, null, null), "#A1-1");
51 			Assert.IsNull (cvt.GetProperties (null, "string", null), "#A1-2");
52 			Assert.IsNull (cvt.GetProperties (null, "string", new Attribute[] {}), "#A1-1");
53 		}
54 
55 		[Test]
ConvertTo()56 		public void ConvertTo ()
57 		{
58 			var cvt = new MultilineStringConverter ();
59 
60 			AssertThrows<ArgumentNullException> (() => {
61 				cvt.ConvertTo (null, null, "string", null);
62 			}, "#A1-1");
63 
64 			AssertThrows<NotSupportedException> (() => {
65 				cvt.ConvertTo (null, null, "string", typeof (int));
66 			}, "#A1-2");
67 
68 			AssertThrows<NotSupportedException> (() => {
69 				cvt.ConvertTo (null, null, "string", typeof (double));
70 			}, "#A1-3");
71 
72 			object result = cvt.ConvertTo (null, null, "string", typeof (string));
73 			Assert.IsNotNull (result, "#A2-1");
74 			Assert.IsTrue (result.GetType () == typeof (string), "#A2-2");
75 			Assert.AreEqual ("(Text)", (string) result, "#A2-3");
76 
77 			string orig = @"This
78 is
79 a
80 multiline
81 string";
82 			result = cvt.ConvertTo (null, null, orig, typeof (string));
83 			Assert.IsNotNull (result, "#A3-1");
84 			Assert.IsTrue (result.GetType () == typeof (string), "#A3-2");
85 			Assert.AreEqual ("(Text)", (string) result, "#A3-3");
86 
87 			result = cvt.ConvertTo (null, null, 1234, typeof (string));
88 			Assert.IsNotNull (result, "#A4-1");
89 			Assert.IsTrue (result.GetType () == typeof (string), "#A4-2");
90 			Assert.AreEqual ("1234", (string) result, "#A4-3");
91 		}
92 
93 		void AssertThrows <ExType> (Action code, string format, params object [] parms) where ExType : Exception
94 		{
95 			if (code == null)
96 				throw new ArgumentNullException ("code");
97 
98 			bool failed = false;
99 			try {
100 				code ();
101 				failed = true;
102 			} catch (ExType) {
103 			} catch (Exception) {
104 				failed = true;
105 			}
106 
107 			if (failed)
108 				Assert.Fail (format, parms);
109 		}
110 	}
111 }
112