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))]
70 public void Default_Dispose ()
72 StringFormat sf = new StringFormat ();
78 [ExpectedException (typeof (ArgumentNullException))]
79 public void ctor_StringFormat_Null ()
81 new StringFormat (null);
85 public void ctor_StringFormat ()
87 using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
88 CheckTypographic (sf);
93 public void ctor_StringFormatFlags ()
95 using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
96 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
101 public void ctor_StringFormatFlags_Int32 ()
103 using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
104 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
105 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
110 public void GenericDefault ()
112 CheckDefaults (StringFormat.GenericDefault);
116 public void GenericDefault_Dispose ()
118 StringFormat.GenericDefault.Dispose ();
119 CheckDefaults (StringFormat.GenericDefault);
123 [ExpectedException (typeof (ArgumentException))]
124 public void GenericDefault_Local_Dispose ()
126 StringFormat sf = StringFormat.GenericDefault;
127 sf.Dispose (); // can't be cached
131 private void CheckTypographic (StringFormat sf)
133 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
134 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
135 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
136 Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
137 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
138 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
139 Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
143 public void GenericTypographic ()
145 StringFormat sf = StringFormat.GenericTypographic;
146 CheckTypographic (sf);
147 Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
151 public void GenericTypographic_Dispose ()
153 StringFormat.GenericTypographic.Dispose ();
154 CheckTypographic (StringFormat.GenericTypographic);
158 [ExpectedException (typeof (ArgumentException))]
159 public void GenericTypographic_Local_Dispose ()
161 StringFormat sf = StringFormat.GenericTypographic;
162 sf.Dispose (); // can't be cached
163 CheckTypographic (sf);
167 public void Alignment_All ()
169 using (StringFormat sf = new StringFormat ()) {
170 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
172 Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
178 [ExpectedException (typeof (InvalidEnumArgumentException))]
179 public void Alignment_Invalid ()
181 using (StringFormat sf = new StringFormat ()) {
182 sf.Alignment = (StringAlignment) Int32.MinValue;
187 public void HotkeyPrefix_All ()
189 using (StringFormat sf = new StringFormat ()) {
190 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
191 sf.HotkeyPrefix = hp;
192 Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
198 [ExpectedException (typeof (InvalidEnumArgumentException))]
199 public void HotkeyPrefix_Invalid ()
201 using (StringFormat sf = new StringFormat ()) {
202 sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue;
207 public void LineAlignment_All ()
209 using (StringFormat sf = new StringFormat ()) {
210 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
211 sf.LineAlignment = sa;
212 Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
218 [ExpectedException (typeof (InvalidEnumArgumentException))]
219 public void LineAlignment_Invalid ()
221 using (StringFormat sf = new StringFormat ()) {
222 sf.LineAlignment = (StringAlignment) Int32.MinValue;
227 public void Trimming_All ()
229 using (StringFormat sf = new StringFormat ()) {
230 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
232 Assert.AreEqual (st, sf.Trimming, st.ToString ());
238 [ExpectedException (typeof (InvalidEnumArgumentException))]
239 public void Trimming_Invalid ()
241 using (StringFormat sf = new StringFormat ()) {
242 sf.Trimming = (StringTrimming) Int32.MinValue;
249 using (StringFormat sf = new StringFormat ()) {
250 using (StringFormat clone = (StringFormat) sf.Clone ()) {
251 CheckDefaults (clone);
257 public void Clone_Complex ()
259 using (StringFormat sf = new StringFormat ()) {
260 CharacterRange[] ranges = new CharacterRange [2];
262 ranges[0].Length = 2;
264 ranges[1].Length = 4;
265 sf.SetMeasurableCharacterRanges (ranges);
267 float[] stops = new float [2];
270 sf.SetTabStops (5.0f, stops);
272 using (StringFormat clone = (StringFormat) sf.Clone ()) {
273 CheckDefaults (clone);
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]");
285 public void TestFormatFlags()
287 using (StringFormat smf = new StringFormat ()) {
288 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
289 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
294 public void TabsStops()
296 using (StringFormat smf = new StringFormat ()) {
297 float firstTabOffset;
298 float[] tabsSrc = { 100, 200, 300, 400 };
301 smf.SetTabStops (200, tabsSrc);
302 tabStops = smf.GetTabStops (out firstTabOffset);
304 Assert.AreEqual (200, firstTabOffset);
305 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
306 Assert.AreEqual (tabsSrc[0], tabStops[0]);
307 Assert.AreEqual (tabsSrc[1], tabStops[1]);
308 Assert.AreEqual (tabsSrc[2], tabStops[2]);
309 Assert.AreEqual (tabsSrc[3], tabStops[3]);
314 [ExpectedException (typeof (NullReferenceException))]
315 public void SetTabStops_Null ()
317 using (StringFormat sf = new StringFormat ()) {
318 sf.SetTabStops (Single.NaN, null);
323 public void SetDigitSubstitution ()
325 using (StringFormat sf = new StringFormat ()) {
326 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
327 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
328 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
333 [ExpectedException (typeof (NullReferenceException))]
334 public void SetMeasurableCharacterRanges_Null ()
336 using (StringFormat sf = new StringFormat ()) {
337 sf.SetMeasurableCharacterRanges (null);
342 public void SetMeasurableCharacterRanges_Empty ()
344 using (StringFormat sf = new StringFormat ()) {
345 CharacterRange[] range = new CharacterRange[0];
346 sf.SetMeasurableCharacterRanges (range);
351 public void SetMeasurableCharacterRanges_Max ()
353 using (StringFormat sf = new StringFormat ()) {
354 CharacterRange[] range = new CharacterRange[32];
355 sf.SetMeasurableCharacterRanges (range);
360 [ExpectedException (typeof (OverflowException))]
361 public void SetMeasurableCharacterRanges_TooBig ()
363 using (StringFormat sf = new StringFormat ()) {
364 CharacterRange[] range = new CharacterRange[33];
365 sf.SetMeasurableCharacterRanges (range);