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 public class StringFormatTest
{
43 private void CheckDefaults (StringFormat sf
)
45 Assert
.AreEqual (StringAlignment
.Near
, sf
.Alignment
, "Alignment");
46 Assert
.AreEqual (0, sf
.DigitSubstitutionLanguage
, "DigitSubstitutionLanguage");
47 Assert
.AreEqual (StringDigitSubstitute
.User
, sf
.DigitSubstitutionMethod
, "DigitSubstitutionMethod");
48 Assert
.AreEqual ((StringFormatFlags
) 0, sf
.FormatFlags
, "FormatFlags");
49 Assert
.AreEqual (HotkeyPrefix
.None
, sf
.HotkeyPrefix
, "HotkeyPrefix");
50 Assert
.AreEqual (StringAlignment
.Near
, sf
.LineAlignment
, "LineAlignment");
51 Assert
.AreEqual (StringTrimming
.Character
, sf
.Trimming
, "Trimming");
55 public void Default ()
57 using (StringFormat sf
= new StringFormat ()) {
59 Assert
.AreEqual ("[StringFormat, FormatFlags=0]", sf
.ToString (), "ToString");
60 // check setters validations
61 sf
.FormatFlags
= (StringFormatFlags
) Int32
.MinValue
;
62 Assert
.AreEqual ((StringFormatFlags
) Int32
.MinValue
, sf
.FormatFlags
, "Min-FormatFlags");
63 Assert
.AreEqual ("[StringFormat, FormatFlags=-2147483648]", sf
.ToString (), "ToString-2");
68 public void Default_Dispose ()
70 StringFormat sf
= new StringFormat ();
72 Assert
.Throws
<ArgumentException
> (() => sf
.ToString ());
76 public void ctor_StringFormat_Null ()
78 Assert
.Throws
<ArgumentNullException
> (() => new StringFormat (null));
82 public void ctor_StringFormat ()
84 using (StringFormat sf
= new StringFormat (StringFormat
.GenericTypographic
)) {
85 CheckTypographic (sf
);
90 public void ctor_StringFormatFlags ()
92 using (StringFormat sf
= new StringFormat ((StringFormatFlags
)Int32
.MinValue
)) {
93 Assert
.AreEqual ((StringFormatFlags
) Int32
.MinValue
, sf
.FormatFlags
, "FormatFlags");
98 public void ctor_StringFormatFlags_Int32 ()
100 using (StringFormat sf
= new StringFormat ((StringFormatFlags
) Int32
.MinValue
, Int32
.MinValue
)) {
101 Assert
.AreEqual (0, sf
.DigitSubstitutionLanguage
, "DigitSubstitutionLanguage");
102 Assert
.AreEqual ((StringFormatFlags
) Int32
.MinValue
, sf
.FormatFlags
, "FormatFlags");
107 public void GenericDefault ()
109 CheckDefaults (StringFormat
.GenericDefault
);
113 public void GenericDefault_Dispose ()
115 StringFormat
.GenericDefault
.Dispose ();
116 CheckDefaults (StringFormat
.GenericDefault
);
120 public void GenericDefault_Local_Dispose ()
122 StringFormat sf
= StringFormat
.GenericDefault
;
123 sf
.Dispose (); // can't be cached
124 Assert
.Throws
<ArgumentException
> (() => CheckDefaults (sf
));
127 private void CheckTypographic (StringFormat sf
)
129 Assert
.AreEqual (StringAlignment
.Near
, sf
.Alignment
, "Alignment");
130 Assert
.AreEqual (0, sf
.DigitSubstitutionLanguage
, "DigitSubstitutionLanguage");
131 Assert
.AreEqual (StringDigitSubstitute
.User
, sf
.DigitSubstitutionMethod
, "DigitSubstitutionMethod");
132 Assert
.AreEqual (StringFormatFlags
.FitBlackBox
| StringFormatFlags
.LineLimit
| StringFormatFlags
.NoClip
, sf
.FormatFlags
, "FormatFlags");
133 Assert
.AreEqual (HotkeyPrefix
.None
, sf
.HotkeyPrefix
, "HotkeyPrefix");
134 Assert
.AreEqual (StringAlignment
.Near
, sf
.LineAlignment
, "LineAlignment");
135 Assert
.AreEqual (StringTrimming
.None
, sf
.Trimming
, "Trimming");
139 public void GenericTypographic ()
141 StringFormat sf
= StringFormat
.GenericTypographic
;
142 CheckTypographic (sf
);
143 Assert
.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf
.ToString (), "ToString");
147 public void GenericTypographic_Dispose ()
149 StringFormat
.GenericTypographic
.Dispose ();
150 CheckTypographic (StringFormat
.GenericTypographic
);
154 public void GenericTypographic_Local_Dispose ()
156 StringFormat sf
= StringFormat
.GenericTypographic
;
157 sf
.Dispose (); // can't be cached
158 Assert
.Throws
<ArgumentException
> (() => CheckTypographic (sf
));
162 public void Alignment_All ()
164 using (StringFormat sf
= new StringFormat ()) {
165 foreach (StringAlignment sa
in Enum
.GetValues (typeof (StringAlignment
))) {
167 Assert
.AreEqual (sa
, sf
.Alignment
, sa
.ToString ());
173 public void Alignment_Invalid ()
175 using (StringFormat sf
= new StringFormat ()) {
176 Assert
.Throws
<InvalidEnumArgumentException
> (() => sf
.Alignment
= (StringAlignment
) Int32
.MinValue
);
181 public void HotkeyPrefix_All ()
183 using (StringFormat sf
= new StringFormat ()) {
184 foreach (HotkeyPrefix hp
in Enum
.GetValues (typeof (HotkeyPrefix
))) {
185 sf
.HotkeyPrefix
= hp
;
186 Assert
.AreEqual (hp
, sf
.HotkeyPrefix
, hp
.ToString ());
192 public void HotkeyPrefix_Invalid ()
194 using (StringFormat sf
= new StringFormat ()) {
195 Assert
.Throws
<InvalidEnumArgumentException
> (() => sf
.HotkeyPrefix
= (HotkeyPrefix
) Int32
.MinValue
);
200 public void LineAlignment_All ()
202 using (StringFormat sf
= new StringFormat ()) {
203 foreach (StringAlignment sa
in Enum
.GetValues (typeof (StringAlignment
))) {
204 sf
.LineAlignment
= sa
;
205 Assert
.AreEqual (sa
, sf
.LineAlignment
, sa
.ToString ());
211 public void LineAlignment_Invalid ()
213 using (StringFormat sf
= new StringFormat ()) {
214 Assert
.Throws
<InvalidEnumArgumentException
> (() => sf
.LineAlignment
= (StringAlignment
) Int32
.MinValue
);
219 public void Trimming_All ()
221 using (StringFormat sf
= new StringFormat ()) {
222 foreach (StringTrimming st
in Enum
.GetValues (typeof (StringTrimming
))) {
224 Assert
.AreEqual (st
, sf
.Trimming
, st
.ToString ());
230 public void Trimming_Invalid ()
232 using (StringFormat sf
= new StringFormat ()) {
233 Assert
.Throws
<InvalidEnumArgumentException
> (() => sf
.Trimming
= (StringTrimming
) Int32
.MinValue
);
240 using (StringFormat sf
= new StringFormat ()) {
241 using (StringFormat clone
= (StringFormat
) sf
.Clone ()) {
242 CheckDefaults (clone
);
248 public void Clone_Complex ()
250 using (StringFormat sf
= new StringFormat ()) {
251 CharacterRange
[] ranges
= new CharacterRange
[2];
253 ranges
[0].Length
= 2;
255 ranges
[1].Length
= 4;
256 sf
.SetMeasurableCharacterRanges (ranges
);
258 float[] stops
= new float [2];
261 sf
.SetTabStops (5.0f
, stops
);
263 using (StringFormat clone
= (StringFormat
) sf
.Clone ()) {
264 CheckDefaults (clone
);
267 float[] cloned_stops
= clone
.GetTabStops (out first
);
268 Assert
.AreEqual (5.0f
, first
, "first");
269 Assert
.AreEqual (6.0f
, cloned_stops
[0], "cloned_stops[0]");
270 Assert
.AreEqual (7.0f
, cloned_stops
[1], "cloned_stops[1]");
276 public void TestFormatFlags()
278 using (StringFormat smf
= new StringFormat ()) {
279 smf
.FormatFlags
= StringFormatFlags
.DisplayFormatControl
;
280 Assert
.AreEqual (StringFormatFlags
.DisplayFormatControl
, smf
.FormatFlags
);
285 public void TabsStops()
287 using (StringFormat smf
= new StringFormat ()) {
288 float firstTabOffset
;
289 float[] tabsSrc
= { 100, 200, 300, 400 }
;
292 smf
.SetTabStops (200, tabsSrc
);
293 tabStops
= smf
.GetTabStops (out firstTabOffset
);
295 Assert
.AreEqual (200, firstTabOffset
);
296 Assert
.AreEqual (tabsSrc
.Length
, tabStops
.Length
);
297 Assert
.AreEqual (tabsSrc
[0], tabStops
[0]);
298 Assert
.AreEqual (tabsSrc
[1], tabStops
[1]);
299 Assert
.AreEqual (tabsSrc
[2], tabStops
[2]);
300 Assert
.AreEqual (tabsSrc
[3], tabStops
[3]);
305 public void SetTabStops_Null ()
307 using (StringFormat sf
= new StringFormat ()) {
308 Assert
.Throws
<NullReferenceException
> (() => sf
.SetTabStops (Single
.NaN
, null));
313 public void SetDigitSubstitution ()
315 using (StringFormat sf
= new StringFormat ()) {
316 sf
.SetDigitSubstitution (Int32
.MinValue
, (StringDigitSubstitute
) Int32
.MinValue
);
317 Assert
.AreEqual (0, sf
.DigitSubstitutionLanguage
, "DigitSubstitutionLanguage");
318 Assert
.AreEqual ((StringDigitSubstitute
) Int32
.MinValue
, sf
.DigitSubstitutionMethod
, "DigitSubstitutionMethod");
323 public void SetMeasurableCharacterRanges_Null ()
325 using (StringFormat sf
= new StringFormat ()) {
326 Assert
.Throws
<NullReferenceException
> (() => sf
.SetMeasurableCharacterRanges (null));
331 public void SetMeasurableCharacterRanges_Empty ()
333 using (StringFormat sf
= new StringFormat ()) {
334 CharacterRange
[] range
= new CharacterRange
[0];
335 sf
.SetMeasurableCharacterRanges (range
);
340 public void SetMeasurableCharacterRanges_Max ()
342 using (StringFormat sf
= new StringFormat ()) {
343 CharacterRange
[] range
= new CharacterRange
[32];
344 sf
.SetMeasurableCharacterRanges (range
);
349 public void SetMeasurableCharacterRanges_TooBig ()
351 using (StringFormat sf
= new StringFormat ()) {
352 CharacterRange
[] range
= new CharacterRange
[33];
353 Assert
.Throws
<OverflowException
> (() => sf
.SetMeasurableCharacterRanges (range
));