PageRenderTime 2358ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Drawing/Test/System.Drawing/TestStringFormat.cs

https://github.com/rasmus-toftdahl-olesen/mono
C# | 414 lines | 344 code | 40 blank | 30 comment | 0 complexity | 9d90aa07c358379b86a609a0db7c17c2 MD5 | raw file
  1. //
  2. // StringFormat class testing unit
  3. //
  4. // Authors:
  5. // Jordi Mas i Hernàndez (jordi@ximian.com)
  6. // Sebastien Pouliot <sebastien@ximian.com>
  7. //
  8. // (C) 2004 Ximian, Inc. http://www.ximian.com
  9. // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.ComponentModel;
  32. using System.Drawing;
  33. using System.Drawing.Text;
  34. using System.Security.Permissions;
  35. using NUnit.Framework;
  36. namespace MonoTests.System.Drawing{
  37. [TestFixture]
  38. [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
  39. public class StringFormatTest {
  40. private void CheckDefaults (StringFormat sf)
  41. {
  42. Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
  43. Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
  44. Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
  45. Assert.AreEqual ((StringFormatFlags) 0, sf.FormatFlags, "FormatFlags");
  46. Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
  47. Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
  48. Assert.AreEqual (StringTrimming.Character, sf.Trimming, "Trimming");
  49. }
  50. [Test]
  51. public void Default ()
  52. {
  53. using (StringFormat sf = new StringFormat ()) {
  54. CheckDefaults (sf);
  55. Assert.AreEqual ("[StringFormat, FormatFlags=0]", sf.ToString (), "ToString");
  56. // check setters validations
  57. sf.FormatFlags = (StringFormatFlags) Int32.MinValue;
  58. Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "Min-FormatFlags");
  59. Assert.AreEqual ("[StringFormat, FormatFlags=-2147483648]", sf.ToString (), "ToString-2");
  60. }
  61. }
  62. [Test]
  63. [ExpectedException (typeof (ArgumentException))]
  64. #if TARGET_JVM
  65. [NUnit.Framework.Category ("NotWorking")]
  66. #endif
  67. public void Default_Dispose ()
  68. {
  69. StringFormat sf = new StringFormat ();
  70. sf.Dispose ();
  71. sf.ToString ();
  72. }
  73. [Test]
  74. [ExpectedException (typeof (ArgumentNullException))]
  75. public void ctor_StringFormat_Null ()
  76. {
  77. new StringFormat (null);
  78. }
  79. [Test]
  80. public void ctor_StringFormat ()
  81. {
  82. using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
  83. CheckTypographic (sf);
  84. }
  85. }
  86. [Test]
  87. #if TARGET_JVM
  88. [NUnit.Framework.Category ("NotWorking")]
  89. #endif
  90. public void ctor_StringFormatFlags ()
  91. {
  92. using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
  93. Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
  94. }
  95. }
  96. [Test]
  97. #if TARGET_JVM
  98. [NUnit.Framework.Category ("NotWorking")]
  99. #endif
  100. public void ctor_StringFormatFlags_Int32 ()
  101. {
  102. using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
  103. Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
  104. Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
  105. }
  106. }
  107. [Test]
  108. public void GenericDefault ()
  109. {
  110. CheckDefaults (StringFormat.GenericDefault);
  111. }
  112. [Test]
  113. public void GenericDefault_Dispose ()
  114. {
  115. StringFormat.GenericDefault.Dispose ();
  116. CheckDefaults (StringFormat.GenericDefault);
  117. }
  118. [Test]
  119. [ExpectedException (typeof (ArgumentException))]
  120. #if TARGET_JVM
  121. [NUnit.Framework.Category ("NotWorking")]
  122. #endif
  123. public void GenericDefault_Local_Dispose ()
  124. {
  125. StringFormat sf = StringFormat.GenericDefault;
  126. sf.Dispose (); // can't be cached
  127. CheckDefaults (sf);
  128. }
  129. private void CheckTypographic (StringFormat sf)
  130. {
  131. Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
  132. Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
  133. Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
  134. Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
  135. Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
  136. Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
  137. Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
  138. }
  139. [Test]
  140. public void GenericTypographic ()
  141. {
  142. StringFormat sf = StringFormat.GenericTypographic;
  143. CheckTypographic (sf);
  144. Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
  145. }
  146. [Test]
  147. public void GenericTypographic_Dispose ()
  148. {
  149. StringFormat.GenericTypographic.Dispose ();
  150. CheckTypographic (StringFormat.GenericTypographic);
  151. }
  152. [Test]
  153. [ExpectedException (typeof (ArgumentException))]
  154. #if TARGET_JVM
  155. [NUnit.Framework.Category ("NotWorking")]
  156. #endif
  157. public void GenericTypographic_Local_Dispose ()
  158. {
  159. StringFormat sf = StringFormat.GenericTypographic;
  160. sf.Dispose (); // can't be cached
  161. CheckTypographic (sf);
  162. }
  163. [Test]
  164. public void Alignment_All ()
  165. {
  166. using (StringFormat sf = new StringFormat ()) {
  167. foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
  168. sf.Alignment = sa;
  169. Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
  170. }
  171. }
  172. }
  173. [Test]
  174. [ExpectedException (typeof (InvalidEnumArgumentException))]
  175. #if TARGET_JVM
  176. [NUnit.Framework.Category ("NotWorking")]
  177. #endif
  178. public void Alignment_Invalid ()
  179. {
  180. using (StringFormat sf = new StringFormat ()) {
  181. sf.Alignment = (StringAlignment) Int32.MinValue;
  182. }
  183. }
  184. [Test]
  185. public void HotkeyPrefix_All ()
  186. {
  187. using (StringFormat sf = new StringFormat ()) {
  188. foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
  189. sf.HotkeyPrefix = hp;
  190. Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
  191. }
  192. }
  193. }
  194. [Test]
  195. [ExpectedException (typeof (InvalidEnumArgumentException))]
  196. #if TARGET_JVM
  197. [NUnit.Framework.Category ("NotWorking")]
  198. #endif
  199. public void HotkeyPrefix_Invalid ()
  200. {
  201. using (StringFormat sf = new StringFormat ()) {
  202. sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue;
  203. }
  204. }
  205. [Test]
  206. public void LineAlignment_All ()
  207. {
  208. using (StringFormat sf = new StringFormat ()) {
  209. foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
  210. sf.LineAlignment = sa;
  211. Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
  212. }
  213. }
  214. }
  215. [Test]
  216. [ExpectedException (typeof (InvalidEnumArgumentException))]
  217. #if TARGET_JVM
  218. [NUnit.Framework.Category ("NotWorking")]
  219. #endif
  220. public void LineAlignment_Invalid ()
  221. {
  222. using (StringFormat sf = new StringFormat ()) {
  223. sf.LineAlignment = (StringAlignment) Int32.MinValue;
  224. }
  225. }
  226. [Test]
  227. public void Trimming_All ()
  228. {
  229. using (StringFormat sf = new StringFormat ()) {
  230. foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
  231. sf.Trimming = st;
  232. Assert.AreEqual (st, sf.Trimming, st.ToString ());
  233. }
  234. }
  235. }
  236. [Test]
  237. [ExpectedException (typeof (InvalidEnumArgumentException))]
  238. #if TARGET_JVM
  239. [NUnit.Framework.Category ("NotWorking")]
  240. #endif
  241. public void Trimming_Invalid ()
  242. {
  243. using (StringFormat sf = new StringFormat ()) {
  244. sf.Trimming = (StringTrimming) Int32.MinValue;
  245. }
  246. }
  247. [Test]
  248. public void Clone()
  249. {
  250. using (StringFormat sf = new StringFormat ()) {
  251. using (StringFormat clone = (StringFormat) sf.Clone ()) {
  252. CheckDefaults (clone);
  253. }
  254. }
  255. }
  256. [Test]
  257. #if TARGET_JVM
  258. [NUnit.Framework.Category ("NotWorking")]
  259. #endif
  260. public void Clone_Complex ()
  261. {
  262. using (StringFormat sf = new StringFormat ()) {
  263. CharacterRange[] ranges = new CharacterRange [2];
  264. ranges[0].First = 1;
  265. ranges[0].Length = 2;
  266. ranges[1].First = 3;
  267. ranges[1].Length = 4;
  268. sf.SetMeasurableCharacterRanges (ranges);
  269. float[] stops = new float [2];
  270. stops [0] = 6.0f;
  271. stops [1] = 7.0f;
  272. sf.SetTabStops (5.0f, stops);
  273. using (StringFormat clone = (StringFormat) sf.Clone ()) {
  274. CheckDefaults (clone);
  275. float first;
  276. float[] cloned_stops = clone.GetTabStops (out first);
  277. Assert.AreEqual (5.0f, first, "first");
  278. Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
  279. Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
  280. }
  281. }
  282. }
  283. [Test]
  284. public void TestFormatFlags()
  285. {
  286. using (StringFormat smf = new StringFormat ()) {
  287. smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
  288. Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
  289. }
  290. }
  291. [Test]
  292. #if TARGET_JVM
  293. [NUnit.Framework.Category ("NotWorking")]
  294. #endif
  295. public void TabsStops()
  296. {
  297. using (StringFormat smf = new StringFormat ()) {
  298. float firstTabOffset;
  299. float[] tabsSrc = { 100, 200, 300, 400 };
  300. float[] tabStops;
  301. smf.SetTabStops (200, tabsSrc);
  302. tabStops = smf.GetTabStops (out firstTabOffset);
  303. Assert.AreEqual (200, firstTabOffset);
  304. Assert.AreEqual (tabsSrc.Length, tabStops.Length);
  305. Assert.AreEqual (tabsSrc[0], tabStops[0]);
  306. Assert.AreEqual (tabsSrc[1], tabStops[1]);
  307. Assert.AreEqual (tabsSrc[2], tabStops[2]);
  308. Assert.AreEqual (tabsSrc[3], tabStops[3]);
  309. }
  310. }
  311. [Test]
  312. [ExpectedException (typeof (NullReferenceException))]
  313. #if TARGET_JVM
  314. [NUnit.Framework.Category ("NotWorking")]
  315. #endif
  316. public void SetTabStops_Null ()
  317. {
  318. using (StringFormat sf = new StringFormat ()) {
  319. sf.SetTabStops (Single.NaN, null);
  320. }
  321. }
  322. [Test]
  323. #if TARGET_JVM
  324. [NUnit.Framework.Category ("NotWorking")]
  325. #endif
  326. public void SetDigitSubstitution ()
  327. {
  328. using (StringFormat sf = new StringFormat ()) {
  329. sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
  330. Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
  331. Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
  332. }
  333. }
  334. [Test]
  335. [ExpectedException (typeof (NullReferenceException))]
  336. #if TARGET_JVM
  337. [NUnit.Framework.Category ("NotWorking")]
  338. #endif
  339. public void SetMeasurableCharacterRanges_Null ()
  340. {
  341. using (StringFormat sf = new StringFormat ()) {
  342. sf.SetMeasurableCharacterRanges (null);
  343. }
  344. }
  345. [Test]
  346. public void SetMeasurableCharacterRanges_Empty ()
  347. {
  348. using (StringFormat sf = new StringFormat ()) {
  349. CharacterRange[] range = new CharacterRange[0];
  350. sf.SetMeasurableCharacterRanges (range);
  351. }
  352. }
  353. [Test]
  354. public void SetMeasurableCharacterRanges_Max ()
  355. {
  356. using (StringFormat sf = new StringFormat ()) {
  357. CharacterRange[] range = new CharacterRange[32];
  358. sf.SetMeasurableCharacterRanges (range);
  359. }
  360. }
  361. [Test]
  362. [ExpectedException (typeof (OverflowException))]
  363. #if TARGET_JVM
  364. [NUnit.Framework.Category ("NotWorking")]
  365. #endif
  366. public void SetMeasurableCharacterRanges_TooBig ()
  367. {
  368. using (StringFormat sf = new StringFormat ()) {
  369. CharacterRange[] range = new CharacterRange[33];
  370. sf.SetMeasurableCharacterRanges (range);
  371. }
  372. }
  373. }
  374. }