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 public void Default_Dispose ()
71 StringFormat sf = new StringFormat ();
73 Assert.Throws<ArgumentException> (() => sf.ToString ());
77 public void ctor_StringFormat_Null ()
79 Assert.Throws<ArgumentNullException> (() => new StringFormat (null));
83 public void ctor_StringFormat ()
85 using (StringFormat sf = new StringFormat (StringFormat.GenericTypographic)) {
86 CheckTypographic (sf);
91 public void ctor_StringFormatFlags ()
93 using (StringFormat sf = new StringFormat ((StringFormatFlags)Int32.MinValue)) {
94 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
99 public void ctor_StringFormatFlags_Int32 ()
101 using (StringFormat sf = new StringFormat ((StringFormatFlags) Int32.MinValue, Int32.MinValue)) {
102 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
103 Assert.AreEqual ((StringFormatFlags) Int32.MinValue, sf.FormatFlags, "FormatFlags");
108 public void GenericDefault ()
110 CheckDefaults (StringFormat.GenericDefault);
114 public void GenericDefault_Dispose ()
116 StringFormat.GenericDefault.Dispose ();
117 CheckDefaults (StringFormat.GenericDefault);
121 public void GenericDefault_Local_Dispose ()
123 StringFormat sf = StringFormat.GenericDefault;
124 sf.Dispose (); // can't be cached
125 Assert.Throws<ArgumentException> (() => CheckDefaults (sf));
128 private void CheckTypographic (StringFormat sf)
130 Assert.AreEqual (StringAlignment.Near, sf.Alignment, "Alignment");
131 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
132 Assert.AreEqual (StringDigitSubstitute.User, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
133 Assert.AreEqual (StringFormatFlags.FitBlackBox | StringFormatFlags.LineLimit | StringFormatFlags.NoClip, sf.FormatFlags, "FormatFlags");
134 Assert.AreEqual (HotkeyPrefix.None, sf.HotkeyPrefix, "HotkeyPrefix");
135 Assert.AreEqual (StringAlignment.Near, sf.LineAlignment, "LineAlignment");
136 Assert.AreEqual (StringTrimming.None, sf.Trimming, "Trimming");
140 public void GenericTypographic ()
142 StringFormat sf = StringFormat.GenericTypographic;
143 CheckTypographic (sf);
144 Assert.AreEqual ("[StringFormat, FormatFlags=FitBlackBox, LineLimit, NoClip]", sf.ToString (), "ToString");
148 public void GenericTypographic_Dispose ()
150 StringFormat.GenericTypographic.Dispose ();
151 CheckTypographic (StringFormat.GenericTypographic);
155 public void GenericTypographic_Local_Dispose ()
157 StringFormat sf = StringFormat.GenericTypographic;
158 sf.Dispose (); // can't be cached
159 Assert.Throws<ArgumentException> (() => CheckTypographic (sf));
163 public void Alignment_All ()
165 using (StringFormat sf = new StringFormat ()) {
166 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
168 Assert.AreEqual (sa, sf.Alignment, sa.ToString ());
174 public void Alignment_Invalid ()
176 using (StringFormat sf = new StringFormat ()) {
177 Assert.Throws<InvalidEnumArgumentException> (() => sf.Alignment = (StringAlignment) Int32.MinValue);
182 public void HotkeyPrefix_All ()
184 using (StringFormat sf = new StringFormat ()) {
185 foreach (HotkeyPrefix hp in Enum.GetValues (typeof (HotkeyPrefix))) {
186 sf.HotkeyPrefix = hp;
187 Assert.AreEqual (hp, sf.HotkeyPrefix, hp.ToString ());
193 public void HotkeyPrefix_Invalid ()
195 using (StringFormat sf = new StringFormat ()) {
196 Assert.Throws<InvalidEnumArgumentException> (() => sf.HotkeyPrefix = (HotkeyPrefix) Int32.MinValue);
201 public void LineAlignment_All ()
203 using (StringFormat sf = new StringFormat ()) {
204 foreach (StringAlignment sa in Enum.GetValues (typeof (StringAlignment))) {
205 sf.LineAlignment = sa;
206 Assert.AreEqual (sa, sf.LineAlignment, sa.ToString ());
212 public void LineAlignment_Invalid ()
214 using (StringFormat sf = new StringFormat ()) {
215 Assert.Throws<InvalidEnumArgumentException> (() => sf.LineAlignment = (StringAlignment) Int32.MinValue);
220 public void Trimming_All ()
222 using (StringFormat sf = new StringFormat ()) {
223 foreach (StringTrimming st in Enum.GetValues (typeof (StringTrimming))) {
225 Assert.AreEqual (st, sf.Trimming, st.ToString ());
231 public void Trimming_Invalid ()
233 using (StringFormat sf = new StringFormat ()) {
234 Assert.Throws<InvalidEnumArgumentException> (() => sf.Trimming = (StringTrimming) Int32.MinValue);
241 using (StringFormat sf = new StringFormat ()) {
242 using (StringFormat clone = (StringFormat) sf.Clone ()) {
243 CheckDefaults (clone);
249 public void Clone_Complex ()
251 using (StringFormat sf = new StringFormat ()) {
252 CharacterRange[] ranges = new CharacterRange [2];
254 ranges[0].Length = 2;
256 ranges[1].Length = 4;
257 sf.SetMeasurableCharacterRanges (ranges);
259 float[] stops = new float [2];
262 sf.SetTabStops (5.0f, stops);
264 using (StringFormat clone = (StringFormat) sf.Clone ()) {
265 CheckDefaults (clone);
268 float[] cloned_stops = clone.GetTabStops (out first);
269 Assert.AreEqual (5.0f, first, "first");
270 Assert.AreEqual (6.0f, cloned_stops[0], "cloned_stops[0]");
271 Assert.AreEqual (7.0f, cloned_stops[1], "cloned_stops[1]");
277 public void TestFormatFlags()
279 using (StringFormat smf = new StringFormat ()) {
280 smf.FormatFlags = StringFormatFlags.DisplayFormatControl;
281 Assert.AreEqual (StringFormatFlags.DisplayFormatControl, smf.FormatFlags);
286 public void TabsStops()
288 using (StringFormat smf = new StringFormat ()) {
289 float firstTabOffset;
290 float[] tabsSrc = { 100, 200, 300, 400 };
293 smf.SetTabStops (200, tabsSrc);
294 tabStops = smf.GetTabStops (out firstTabOffset);
296 Assert.AreEqual (200, firstTabOffset);
297 Assert.AreEqual (tabsSrc.Length, tabStops.Length);
298 Assert.AreEqual (tabsSrc[0], tabStops[0]);
299 Assert.AreEqual (tabsSrc[1], tabStops[1]);
300 Assert.AreEqual (tabsSrc[2], tabStops[2]);
301 Assert.AreEqual (tabsSrc[3], tabStops[3]);
306 public void SetTabStops_Null ()
308 using (StringFormat sf = new StringFormat ()) {
309 Assert.Throws<NullReferenceException> (() => sf.SetTabStops (Single.NaN, null));
314 public void SetDigitSubstitution ()
316 using (StringFormat sf = new StringFormat ()) {
317 sf.SetDigitSubstitution (Int32.MinValue, (StringDigitSubstitute) Int32.MinValue);
318 Assert.AreEqual (0, sf.DigitSubstitutionLanguage, "DigitSubstitutionLanguage");
319 Assert.AreEqual ((StringDigitSubstitute) Int32.MinValue, sf.DigitSubstitutionMethod, "DigitSubstitutionMethod");
324 public void SetMeasurableCharacterRanges_Null ()
326 using (StringFormat sf = new StringFormat ()) {
327 Assert.Throws<NullReferenceException> (() => sf.SetMeasurableCharacterRanges (null));
332 public void SetMeasurableCharacterRanges_Empty ()
334 using (StringFormat sf = new StringFormat ()) {
335 CharacterRange[] range = new CharacterRange[0];
336 sf.SetMeasurableCharacterRanges (range);
341 public void SetMeasurableCharacterRanges_Max ()
343 using (StringFormat sf = new StringFormat ()) {
344 CharacterRange[] range = new CharacterRange[32];
345 sf.SetMeasurableCharacterRanges (range);
350 public void SetMeasurableCharacterRanges_TooBig ()
352 using (StringFormat sf = new StringFormat ()) {
353 CharacterRange[] range = new CharacterRange[33];
354 Assert.Throws<OverflowException> (() => sf.SetMeasurableCharacterRanges (range));