2 // StringFormat class testing unit
5 // Jordi Mas i Hernàndez (jordi@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
9 // Copyright (C) 2004-2006 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.ComponentModel;
34 using System.Drawing.Text;
35 using System.Security.Permissions;
36 using NUnit.Framework;
38 namespace MonoTests.System.Drawing{
41 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
42 public class StringFormatTest {
44 private void CheckDefaults (StringFormat sf)
46 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
47 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
48 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
49 Assert.AreEqual ((StringFormatFlags) 0, sf.FormatFlags, "FormatFlags");
50 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
51 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
52 Assert.AreEqual (StringTrimming.Character, sf.Trimming, "Trimming");
56 public void Default ()
58 using (StringFormat sf = new StringFormat ()) {
60 Assert.AreEqual ("[StringFormat, FormatFlags=0]", sf.ToString (), "ToString");
61 // check setters validations
62 sf.FormatFlags = (StringFormatFlags) Int32.MinValue;
63 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "Min-FormatFlags");
64 Assert.AreEqual ("[StringFormat, FormatFlags=-2147483648]", sf.ToString (), "ToString-2");
69 [ExpectedException (typeof (ArgumentException))]
71 [NUnit.Framework.Category ("NotWorking")]
73 public void Default_Dispose ()
75 StringFormat sf = new StringFormat ();
81 [ExpectedException (typeof (ArgumentNullException))]
82 public void ctor_StringFormat_Null ()
84 new StringFormat (null);
88 public void ctor_StringFormat ()
90 using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
91 CheckTypographic (sf);
97 [NUnit.Framework.Category ("NotWorking")]
99 public void ctor_StringFormatFlags ()
101 using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
102 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
108 [NUnit.Framework.Category ("NotWorking")]
110 public void ctor_StringFormatFlags_Int32 ()
112 using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
113 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
114 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
119 public void GenericDefault ()
121 CheckDefaults (StringFormat.GenericDefault);
125 public void GenericDefault_Dispose ()
127 StringFormat.GenericDefault.Dispose ();
128 CheckDefaults (StringFormat.GenericDefault);
132 [ExpectedException (typeof (ArgumentException))]
134 [NUnit.Framework.Category ("NotWorking")]
136 public void GenericDefault_Local_Dispose ()
138 StringFormat sf = StringFormat.GenericDefault;
139 sf.Dispose (); // can't be cached
143 private void CheckTypographic (StringFormat sf)
145 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
146 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
147 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
148 Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
149 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
150 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
151 Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
155 public void GenericTypographic ()
157 StringFormat sf = StringFormat.GenericTypographic;
158 CheckTypographic (sf);
159 Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
163 public void GenericTypographic_Dispose ()
165 StringFormat.GenericTypographic.Dispose ();
166 CheckTypographic (StringFormat.GenericTypographic);
170 [ExpectedException (typeof (ArgumentException))]
172 [NUnit.Framework.Category ("NotWorking")]
174 public void GenericTypographic_Local_Dispose ()
176 StringFormat sf = StringFormat.GenericTypographic;
177 sf.Dispose (); // can't be cached
178 CheckTypographic (sf);
182 public void Alignment_All ()
184 using (StringFormat sf = new StringFormat ()) {
185 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
187 Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
193 [ExpectedException (typeof (InvalidEnumArgumentException))]
195 [NUnit.Framework.Category ("NotWorking")]
197 public void Alignment_Invalid ()
199 using (StringFormat sf = new StringFormat ()) {
200 sf.Alignment = (StringAlignment) Int32.MinValue;
205 public void HotkeyPrefix_All ()
207 using (StringFormat sf = new StringFormat ()) {
208 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
209 sf.HotkeyPrefix = hp;
210 Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
216 [ExpectedException (typeof (InvalidEnumArgumentException))]
218 [NUnit.Framework.Category ("NotWorking")]
220 public void HotkeyPrefix_Invalid ()
222 using (StringFormat sf = new StringFormat ()) {
223 sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue;
228 public void LineAlignment_All ()
230 using (StringFormat sf = new StringFormat ()) {
231 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
232 sf.LineAlignment = sa;
233 Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
239 [ExpectedException (typeof (InvalidEnumArgumentException))]
241 [NUnit.Framework.Category ("NotWorking")]
243 public void LineAlignment_Invalid ()
245 using (StringFormat sf = new StringFormat ()) {
246 sf.LineAlignment = (StringAlignment) Int32.MinValue;
251 public void Trimming_All ()
253 using (StringFormat sf = new StringFormat ()) {
254 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
256 Assert.AreEqual (st, sf.Trimming, st.ToString ());
262 [ExpectedException (typeof (InvalidEnumArgumentException))]
264 [NUnit.Framework.Category ("NotWorking")]
266 public void Trimming_Invalid ()
268 using (StringFormat sf = new StringFormat ()) {
269 sf.Trimming = (StringTrimming) Int32.MinValue;
276 using (StringFormat sf = new StringFormat ()) {
277 using (StringFormat clone = (StringFormat) sf.Clone ()) {
278 CheckDefaults (clone);
285 [NUnit.Framework.Category ("NotWorking")]
287 public void Clone_Complex ()
289 using (StringFormat sf = new StringFormat ()) {
290 CharacterRange[] ranges = new CharacterRange [2];
292 ranges[0].Length = 2;
294 ranges[1].Length = 4;
295 sf.SetMeasurableCharacterRanges (ranges);
297 float[] stops = new float [2];
300 sf.SetTabStops (5.0f, stops);
302 using (StringFormat clone = (StringFormat) sf.Clone ()) {
303 CheckDefaults (clone);
306 float[] cloned_stops = clone.GetTabStops (out first);
307 Assert.AreEqual (5.0f, first, "first");
308 Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
309 Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
315 public void TestFormatFlags()
317 using (StringFormat smf = new StringFormat ()) {
318 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
319 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
325 [NUnit.Framework.Category ("NotWorking")]
327 public void TabsStops()
329 using (StringFormat smf = new StringFormat ()) {
330 float firstTabOffset;
331 float[] tabsSrc = { 100, 200, 300, 400 };
334 smf.SetTabStops (200, tabsSrc);
335 tabStops = smf.GetTabStops (out firstTabOffset);
337 Assert.AreEqual (200, firstTabOffset);
338 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
339 Assert.AreEqual (tabsSrc[0], tabStops[0]);
340 Assert.AreEqual (tabsSrc[1], tabStops[1]);
341 Assert.AreEqual (tabsSrc[2], tabStops[2]);
342 Assert.AreEqual (tabsSrc[3], tabStops[3]);
347 [ExpectedException (typeof (NullReferenceException))]
349 [NUnit.Framework.Category ("NotWorking")]
351 public void SetTabStops_Null ()
353 using (StringFormat sf = new StringFormat ()) {
354 sf.SetTabStops (Single.NaN, null);
360 [NUnit.Framework.Category ("NotWorking")]
362 public void SetDigitSubstitution ()
364 using (StringFormat sf = new StringFormat ()) {
365 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
366 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
367 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
372 [ExpectedException (typeof (NullReferenceException))]
374 [NUnit.Framework.Category ("NotWorking")]
376 public void SetMeasurableCharacterRanges_Null ()
378 using (StringFormat sf = new StringFormat ()) {
379 sf.SetMeasurableCharacterRanges (null);
384 public void SetMeasurableCharacterRanges_Empty ()
386 using (StringFormat sf = new StringFormat ()) {
387 CharacterRange[] range = new CharacterRange[0];
388 sf.SetMeasurableCharacterRanges (range);
393 public void SetMeasurableCharacterRanges_Max ()
395 using (StringFormat sf = new StringFormat ()) {
396 CharacterRange[] range = new CharacterRange[32];
397 sf.SetMeasurableCharacterRanges (range);
402 [ExpectedException (typeof (OverflowException))]
404 [NUnit.Framework.Category ("NotWorking")]
406 public void SetMeasurableCharacterRanges_TooBig ()
408 using (StringFormat sf = new StringFormat ()) {
409 CharacterRange[] range = new CharacterRange[33];
410 sf.SetMeasurableCharacterRanges (range);