2
0

UnitTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // Tests for System.Web.UI.WebControls.Unit.cs
  3. //
  4. // Author:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.Globalization;
  32. using System.Web;
  33. using System.Web.UI.WebControls;
  34. namespace MonoTests.System.Web.UI.WebControls
  35. {
  36. [TestFixture]
  37. public class UnitTest {
  38. [Test]
  39. public void UnitConstructors ()
  40. {
  41. Unit a1 = new Unit (1.0);
  42. Assert.AreEqual (a1.Type, UnitType.Pixel, "A1");
  43. Assert.AreEqual (a1.Value, 1.0, "A2");
  44. Assert.AreEqual ("1px", a1.ToString (), "A2.1");
  45. Unit a2 = new Unit (1);
  46. Assert.AreEqual (a2.Type, UnitType.Pixel, "A3");
  47. Assert.AreEqual (a1.Value, 1.0, "A4");
  48. Assert.AreEqual ("1px", a1.ToString (), "A4.1");
  49. Unit a3 = new Unit (32767);
  50. Assert.AreEqual (a3.Type, UnitType.Pixel, "A5");
  51. Assert.AreEqual (a3.Value, 32767.0, "A6");
  52. Assert.AreEqual ("32767px", a3.ToString (), "A6.1");
  53. a3 = new Unit (-32768);
  54. Assert.AreEqual (a3.Type, UnitType.Pixel, "A7");
  55. Assert.AreEqual (a3.Value, -32768.0, "A8");
  56. Assert.AreEqual ("-32768px", a3.ToString (), "A8.1");
  57. //
  58. // String constructor
  59. //
  60. Unit s1 = new Unit ("-45cm");
  61. Assert.AreEqual (s1.Type, UnitType.Cm, "A9");
  62. Assert.AreEqual (s1.Value, -45, "A10");
  63. Assert.AreEqual ("-45cm", s1.ToString (), "A10.1");
  64. s1 = new Unit ("\t-45cm");
  65. Assert.AreEqual (s1.Type, UnitType.Cm, "A11");
  66. Assert.AreEqual (s1.Value, -45, "A12");
  67. Assert.AreEqual ("-45cm", s1.ToString (), "A12.1");
  68. s1 = new Unit ("45\tcm");
  69. Assert.AreEqual (s1.Type, UnitType.Cm, "A13");
  70. Assert.AreEqual (s1.Value, 45, "A14");
  71. s1 = new Unit ("45\tcm");
  72. Assert.AreEqual (s1.Type, UnitType.Cm, "A15");
  73. Assert.AreEqual (s1.Value, 45, "A16");
  74. s1 = new Unit (null);
  75. Assert.AreEqual (s1.Type, UnitType.Pixel, "A17");
  76. Assert.AreEqual (s1.Value, 0, "A18");
  77. s1 = new Unit ("-45");
  78. Assert.AreEqual (s1.Type, UnitType.Pixel, "A19");
  79. Assert.AreEqual (s1.Value, -45, "A20");
  80. s1 = new Unit ("-45%");
  81. Assert.AreEqual (s1.Type, UnitType.Percentage, "A21");
  82. Assert.AreEqual (s1.Value, -45, "A22");
  83. Assert.AreEqual ("-45%", s1.ToString (), "A22.1");
  84. s1 = new Unit ("-45% \t ");
  85. Assert.AreEqual (s1.Type, UnitType.Percentage, "A23");
  86. Assert.AreEqual (s1.Value, -45, "A24");
  87. s1 = new Unit ("-45 % \t ");
  88. Assert.AreEqual (s1.Type, UnitType.Percentage, "A25");
  89. Assert.AreEqual (s1.Value, -45, "A26");
  90. s1 = new Unit ("45in");
  91. Assert.AreEqual (s1.Type, UnitType.Inch, "A27");
  92. Assert.AreEqual (s1.Value, 45, "A28");
  93. Assert.AreEqual ("45in", s1.ToString (), "A28.1");
  94. s1 = new Unit ("45cm");
  95. Assert.AreEqual (s1.Type, UnitType.Cm, "A29");
  96. Assert.AreEqual (s1.Value, 45, "A30");
  97. Assert.AreEqual ("45cm", s1.ToString (), "A30.1");
  98. s1 = new Unit ("45pt");
  99. Assert.AreEqual (s1.Type, UnitType.Point, "A31");
  100. Assert.AreEqual (s1.Value, 45, "A32");
  101. Assert.AreEqual ("45pt", s1.ToString (), "A32.1");
  102. s1 = new Unit ("45pc");
  103. Assert.AreEqual (s1.Type, UnitType.Pica, "A33");
  104. Assert.AreEqual (s1.Value, 45, "A34");
  105. Assert.AreEqual ("45pc", s1.ToString (), "A34.1");
  106. s1 = new Unit ("45mm");
  107. Assert.AreEqual (s1.Type, UnitType.Mm, "A35");
  108. Assert.AreEqual (s1.Value, 45, "A36");
  109. Assert.AreEqual ("45mm", s1.ToString (), "A36.1");
  110. s1 = new Unit ("45em");
  111. Assert.AreEqual (s1.Type, UnitType.Em, "A37");
  112. Assert.AreEqual (s1.Value, 45, "A38");
  113. Assert.AreEqual ("45em", s1.ToString (), "A38.1");
  114. s1 = new Unit ("45ex");
  115. Assert.AreEqual (s1.Type, UnitType.Ex, "A39");
  116. Assert.AreEqual (s1.Value, 45, "A40");
  117. Assert.AreEqual ("45ex", s1.ToString (), "A40.1");
  118. s1 = new Unit ("45px");
  119. Assert.AreEqual (s1.Type, UnitType.Pixel, "A41");
  120. Assert.AreEqual (s1.Value, 45, "A42");
  121. Assert.AreEqual ("45px", s1.ToString (), "A42.1");
  122. s1 = new Unit ("1.75cm");
  123. Assert.AreEqual (s1.Type, UnitType.Cm, "A43");
  124. Assert.AreEqual (s1.Value, 1.75, "A44");
  125. s1 = new Unit ("1.75%");
  126. Assert.AreEqual (s1.Type, UnitType.Percentage, "A45");
  127. Assert.AreEqual (s1.Value, 1.75, "A46");
  128. s1 = new Unit (null);
  129. Assert.AreEqual (s1.Type, UnitType.Pixel, "A47");
  130. Assert.AreEqual (s1.Value, 0, "A48");
  131. Assert.AreEqual (s1.IsEmpty, true, "A49");
  132. Assert.AreEqual (s1.ToString (), "", "A50");
  133. s1 = new Unit ("");
  134. Assert.AreEqual (s1.Type, UnitType.Pixel, "A51");
  135. Assert.AreEqual (s1.Value, 0, "A52");
  136. Assert.AreEqual (s1.IsEmpty, true, "A53");
  137. Assert.AreEqual (s1.ToString (), "", "A54");
  138. s1 = new Unit ("45.75cm");
  139. Assert.AreEqual (s1.Type, UnitType.Cm, "A55");
  140. Assert.AreEqual (s1.Value, 45.75, "A56");
  141. Assert.AreEqual ("45.75cm", s1.ToString (), "A57");
  142. a3 = new Unit (1.5);
  143. Assert.AreEqual (UnitType.Pixel, a3.Type, "A58");
  144. Assert.AreEqual (1.0, a3.Value, "A59");
  145. }
  146. [Test]
  147. public void ParseCultures ()
  148. {
  149. // Test cultures where the decimal separator is not "."
  150. CultureInfo ci = new CultureInfo ("es-ES", false);
  151. Unit s1 = new Unit ("1,5cm", ci);
  152. Assert.AreEqual (s1.Type, UnitType.Cm, "C1");
  153. Assert.AreEqual (s1.Value, 1.5, "C2");
  154. Assert.AreEqual (s1.ToString (), "1.5cm", "A54");
  155. Assert.AreEqual (s1.ToString (ci), "1,5cm", "A54");
  156. }
  157. [Test]
  158. public void UnitEquality ()
  159. {
  160. Unit u1 = new Unit ("1px");
  161. Unit u2 = new Unit ("2px");
  162. Unit t1 = new Unit ("1px");
  163. Unit c2 = new Unit ("2cm");
  164. Assert.AreEqual (u1 == t1, true, "U1");
  165. Assert.AreEqual (u1 != u2, true, "U2");
  166. Assert.AreEqual (u1 == u2, false, "U3");
  167. Assert.AreEqual (u1 != t1, false, "U4");
  168. // Test that its comparing the units and value
  169. Assert.AreEqual (u2 != c2, true, "U5");
  170. }
  171. [Test]
  172. public void UnitImplicit ()
  173. {
  174. Unit u = 1;
  175. Assert.AreEqual (u.Value, 1.0, "M1");
  176. Assert.AreEqual (u.Type, UnitType.Pixel, "M1");
  177. }
  178. [Test]
  179. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  180. public void IncorrectConstructor ()
  181. {
  182. Unit a = new Unit (32768.0);
  183. }
  184. [Test]
  185. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  186. public void IncorrectConstructor2 ()
  187. {
  188. Unit a = new Unit (32768);
  189. }
  190. [Test]
  191. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  192. public void IncorrectConstructor3 ()
  193. {
  194. Unit a = new Unit (-32769.0);
  195. }
  196. [Test]
  197. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  198. public void IncorrectConstructor4 ()
  199. {
  200. Unit a = new Unit (-32769);
  201. }
  202. [Test]
  203. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  204. public void IncorrectConstructor5 ()
  205. {
  206. // throws because of space between - and 45
  207. Unit a = new Unit ("- 45cm");
  208. }
  209. [Test]
  210. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  211. public void IncorrectConstructor6 ()
  212. {
  213. // Throws becaues "cma" is not a unit
  214. Unit a = new Unit ("-45cma");
  215. }
  216. [Test]
  217. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  218. public void IncorrectConstructor7 ()
  219. {
  220. // Throws because there is stuff after "%"
  221. Unit a = new Unit ("-45%cm");
  222. }
  223. [Test]
  224. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  225. public void IncorrectConstructor8 ()
  226. {
  227. // throws because there is stuff after cm (a)
  228. Unit a = new Unit ("-45cm a");
  229. }
  230. [Test]
  231. #if !NET_2_0
  232. [ExpectedException (typeof (FormatException))]
  233. #endif
  234. public void IncorrectConstructor9 ()
  235. {
  236. // throws because floating point values are not valid for Pixel.
  237. Unit a = new Unit ("34.4px");
  238. }
  239. [Test]
  240. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  241. public void IncorrectConstructor10 ()
  242. {
  243. // throws because there is a space before the dot
  244. Unit a = new Unit ("34 .4pt");
  245. }
  246. [Test]
  247. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  248. public void IncorrectConstructor11 ()
  249. {
  250. // throws because there is a space after the dot
  251. Unit a = new Unit ("34. 4pt");
  252. }
  253. #if NET_2_0
  254. class MyFormatProvider : IFormatProvider
  255. {
  256. public object GetFormat (Type format_type)
  257. {
  258. return Activator.CreateInstance (format_type);
  259. }
  260. }
  261. [Test]
  262. public void Unit_IFormatProviderToString ()
  263. {
  264. MyFormatProvider mfp = new MyFormatProvider ();
  265. Unit a1 = new Unit (1.0);
  266. Assert.AreEqual ("1px", a1.ToString (mfp), "A1");
  267. a1 = new Unit (1);
  268. Assert.AreEqual ("1px", a1.ToString (mfp), "A2");
  269. a1 = new Unit (32767);
  270. Assert.AreEqual ("32767px", a1.ToString (mfp), "A3");
  271. a1 = new Unit (-32768);
  272. Assert.AreEqual ("-32768px", a1.ToString (mfp), "A4");
  273. //
  274. // String constructor
  275. //
  276. Unit s1 = new Unit ("-45cm");
  277. Assert.AreEqual ("-45cm", s1.ToString (mfp), "A5");
  278. s1 = new Unit ("\t-45cm");
  279. Assert.AreEqual ("-45cm", s1.ToString (mfp), "A6");
  280. s1 = new Unit ("45\tcm");
  281. Assert.AreEqual ("45cm", s1.ToString (mfp), "A7");
  282. s1 = new Unit (null);
  283. Assert.AreEqual ("", s1.ToString (mfp), "A8");
  284. s1 = new Unit ("-45");
  285. Assert.AreEqual ("-45px", s1.ToString (mfp), "A9");
  286. s1 = new Unit ("-45%");
  287. Assert.AreEqual ("-45%", s1.ToString (mfp), "A10");
  288. s1 = new Unit ("-45% \t ");
  289. Assert.AreEqual ("-45%", s1.ToString (mfp), "A11");
  290. s1 = new Unit ("-45 % \t ");
  291. Assert.AreEqual ("-45%", s1.ToString (mfp), "A12");
  292. s1 = new Unit ("45in");
  293. Assert.AreEqual ("45in", s1.ToString (mfp), "A13");
  294. s1 = new Unit ("45cm");
  295. Assert.AreEqual ("45cm", s1.ToString (mfp), "A14");
  296. s1 = new Unit ("45pt");
  297. Assert.AreEqual ("45pt", s1.ToString (mfp), "A15");
  298. s1 = new Unit ("45pc");
  299. Assert.AreEqual ("45pc", s1.ToString (mfp), "A16");
  300. s1 = new Unit ("45mm");
  301. Assert.AreEqual ("45mm", s1.ToString (mfp), "A17");
  302. s1 = new Unit ("45em");
  303. Assert.AreEqual ("45em", s1.ToString (mfp), "A18");
  304. s1 = new Unit ("45ex");
  305. Assert.AreEqual ("45ex", s1.ToString (mfp), "A19");
  306. s1 = new Unit ("45px");
  307. Assert.AreEqual ("45px", s1.ToString (mfp), "A20");
  308. s1 = new Unit ("1.75cm");
  309. Assert.AreEqual ("1.75cm", s1.ToString (mfp), "A21");
  310. s1 = new Unit ("1.75%");
  311. Assert.AreEqual ("1.75%", s1.ToString (mfp), "A22");
  312. s1 = new Unit (null);
  313. Assert.AreEqual ("", s1.ToString (mfp), "A23");
  314. s1 = new Unit ("");
  315. Assert.AreEqual ("", s1.ToString (mfp), "A24");
  316. s1 = new Unit ("45.75cm");
  317. Assert.AreEqual ("45.75cm", s1.ToString (mfp), "A25");
  318. a1 = new Unit (1.5);
  319. Assert.AreEqual ("1px", a1.ToString (mfp), "A26");
  320. }
  321. #endif
  322. }
  323. }