2 // System.Configuration.IntegerValidatorTest.cs - Unit tests
3 // for System.Configuration.IntegerValidator.
6 // Chris Toshok <toshok@ximian.com>
7 // Andres G. Aragoneses <andres@7digital.com>
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Copyright (C) 2012 7digital Media, Ltd (http://www.7digital.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Configuration;
37 using NUnit.Framework;
39 namespace MonoTests.System.Configuration {
41 public class IntegerValidatorTest
44 public void CanValidate ()
46 IntegerValidator v = new IntegerValidator (1, 1);
48 Assert.IsFalse (v.CanValidate (typeof (TimeSpan)), "A1");
49 Assert.IsTrue (v.CanValidate (typeof (int)), "A2");
50 Assert.IsFalse (v.CanValidate (typeof (long)), "A3");
54 public void Validate_inRange ()
56 IntegerValidator v = new IntegerValidator (5000, 10000);
61 public void Validate_Inclusive ()
63 IntegerValidator v = new IntegerValidator (5000, 10000, false);
69 public void Validate_Exclusive ()
71 IntegerValidator v = new IntegerValidator (5000, 10000, true);
77 [ExpectedException (typeof (ArgumentException))]
78 public void Validate_Exclusive_fail1 ()
80 IntegerValidator v = new IntegerValidator (5000, 10000, true);
85 [ExpectedException (typeof (ArgumentException))]
86 public void Validate_Exclusive_fail2 ()
88 IntegerValidator v = new IntegerValidator (5000, 10000, true);
93 [ExpectedException (typeof (ArgumentException))]
94 public void Validate_Exclusive_fail3 ()
96 IntegerValidator v = new IntegerValidator (5000, 10000, true);
101 [ExpectedException (typeof (ArgumentException))]
102 public void Validate_Resolution ()
104 IntegerValidator v = new IntegerValidator (20000,
112 #region BNC654721 https://bugzilla.novell.com/show_bug.cgi?id=654721
113 public sealed class TestSection : ConfigurationSection
115 public void Load (string xml)
118 using (var sr = new StringReader (xml))
119 using (var reader = new XmlTextReader (sr))
121 DeserializeSection (reader);
125 [ConfigurationProperty ("integerValidatorMinValue")]
126 public IntegerValidatorMinValueChildElement IntegerValidatorMinValue
128 get { return (IntegerValidatorMinValueChildElement)base["integerValidatorMinValue"]; }
129 set { base["integerValidatorMinValue"] = value; }
132 [ConfigurationProperty ("integerValidatorMaxValue")]
133 public IntegerValidatorMaxValueChildElement IntegerValidatorMaxValue
135 get { return (IntegerValidatorMaxValueChildElement)base["integerValidatorMaxValue"]; }
136 set { base["integerValidatorMaxValue"] = value; }
140 public sealed class IntegerValidatorMaxValueChildElement : ConfigurationElement
142 [ConfigurationProperty ("theProperty"), IntegerValidator (MaxValue = 100)]
143 public int TheProperty
145 get { return (int)base["theProperty"]; }
146 set { base ["theProperty"] = value; }
150 public sealed class IntegerValidatorMinValueChildElement : ConfigurationElement
152 [ConfigurationProperty ("theProperty"), IntegerValidator (MinValue = 0)]
153 public int TheProperty
155 get { return (int)base["theProperty"]; }
156 set { base ["theProperty"] = value; }
161 public void IntegerValidatorMinValueTest ()
163 var section = new TestSection ();
164 section.Load (@"<someSection><integerValidatorMinValue theProperty=""15"" /></someSection>");
165 Assert.AreEqual (15, section.IntegerValidatorMinValue.TheProperty);
169 public void IntegerValidatorMaxValueTest ()
171 var section = new TestSection ();
172 section.Load (@"<someSection><integerValidatorMaxValue theProperty=""25"" /></someSection>");
173 Assert.AreEqual (25, section.IntegerValidatorMaxValue.TheProperty);