2
0

UnitTest.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. {
  39. [Test]
  40. public void UnitConstructors ()
  41. {
  42. Unit a1 = new Unit (1.0);
  43. Assert.AreEqual (a1.Type, UnitType.Pixel, "A1");
  44. Assert.AreEqual (a1.Value, 1.0, "A2");
  45. Assert.AreEqual ("1px", a1.ToString (), "A2.1");
  46. Unit a2 = new Unit (1);
  47. Assert.AreEqual (a2.Type, UnitType.Pixel, "A3");
  48. Assert.AreEqual (a1.Value, 1.0, "A4");
  49. Assert.AreEqual ("1px", a1.ToString (), "A4.1");
  50. Unit a3 = new Unit (32767);
  51. Assert.AreEqual (a3.Type, UnitType.Pixel, "A5");
  52. Assert.AreEqual (a3.Value, 32767.0, "A6");
  53. Assert.AreEqual ("32767px", a3.ToString (), "A6.1");
  54. a3 = new Unit (-32768);
  55. Assert.AreEqual (a3.Type, UnitType.Pixel, "A7");
  56. Assert.AreEqual (a3.Value, -32768.0, "A8");
  57. Assert.AreEqual ("-32768px", a3.ToString (), "A8.1");
  58. //
  59. // String constructor
  60. //
  61. Unit s1 = new Unit ("-45cm");
  62. Assert.AreEqual (s1.Type, UnitType.Cm, "A9");
  63. Assert.AreEqual (s1.Value, -45, "A10");
  64. Assert.AreEqual ("-45cm", s1.ToString (), "A10.1");
  65. s1 = new Unit ("\t-45cm");
  66. Assert.AreEqual (s1.Type, UnitType.Cm, "A11");
  67. Assert.AreEqual (s1.Value, -45, "A12");
  68. Assert.AreEqual ("-45cm", s1.ToString (), "A12.1");
  69. s1 = new Unit ("45\tcm");
  70. Assert.AreEqual (s1.Type, UnitType.Cm, "A13");
  71. Assert.AreEqual (s1.Value, 45, "A14");
  72. s1 = new Unit ("45\tcm");
  73. Assert.AreEqual (s1.Type, UnitType.Cm, "A15");
  74. Assert.AreEqual (s1.Value, 45, "A16");
  75. s1 = new Unit (null);
  76. Assert.AreEqual (s1.Type, UnitType.Pixel, "A17");
  77. Assert.AreEqual (s1.Value, 0, "A18");
  78. s1 = new Unit ("-45");
  79. Assert.AreEqual (s1.Type, UnitType.Pixel, "A19");
  80. Assert.AreEqual (s1.Value, -45, "A20");
  81. s1 = new Unit ("-45%");
  82. Assert.AreEqual (s1.Type, UnitType.Percentage, "A21");
  83. Assert.AreEqual (s1.Value, -45, "A22");
  84. Assert.AreEqual ("-45%", s1.ToString (), "A22.1");
  85. s1 = new Unit ("-45% \t ");
  86. Assert.AreEqual (s1.Type, UnitType.Percentage, "A23");
  87. Assert.AreEqual (s1.Value, -45, "A24");
  88. s1 = new Unit ("-45 % \t ");
  89. Assert.AreEqual (s1.Type, UnitType.Percentage, "A25");
  90. Assert.AreEqual (s1.Value, -45, "A26");
  91. s1 = new Unit ("45in");
  92. Assert.AreEqual (s1.Type, UnitType.Inch, "A27");
  93. Assert.AreEqual (s1.Value, 45, "A28");
  94. Assert.AreEqual ("45in", s1.ToString (), "A28.1");
  95. s1 = new Unit ("45cm");
  96. Assert.AreEqual (s1.Type, UnitType.Cm, "A29");
  97. Assert.AreEqual (s1.Value, 45, "A30");
  98. Assert.AreEqual ("45cm", s1.ToString (), "A30.1");
  99. s1 = new Unit ("45pt");
  100. Assert.AreEqual (s1.Type, UnitType.Point, "A31");
  101. Assert.AreEqual (s1.Value, 45, "A32");
  102. Assert.AreEqual ("45pt", s1.ToString (), "A32.1");
  103. s1 = new Unit ("45pc");
  104. Assert.AreEqual (s1.Type, UnitType.Pica, "A33");
  105. Assert.AreEqual (s1.Value, 45, "A34");
  106. Assert.AreEqual ("45pc", s1.ToString (), "A34.1");
  107. s1 = new Unit ("45mm");
  108. Assert.AreEqual (s1.Type, UnitType.Mm, "A35");
  109. Assert.AreEqual (s1.Value, 45, "A36");
  110. Assert.AreEqual ("45mm", s1.ToString (), "A36.1");
  111. s1 = new Unit ("45em");
  112. Assert.AreEqual (s1.Type, UnitType.Em, "A37");
  113. Assert.AreEqual (s1.Value, 45, "A38");
  114. Assert.AreEqual ("45em", s1.ToString (), "A38.1");
  115. s1 = new Unit ("45ex");
  116. Assert.AreEqual (s1.Type, UnitType.Ex, "A39");
  117. Assert.AreEqual (s1.Value, 45, "A40");
  118. Assert.AreEqual ("45ex", s1.ToString (), "A40.1");
  119. s1 = new Unit ("45px");
  120. Assert.AreEqual (s1.Type, UnitType.Pixel, "A41");
  121. Assert.AreEqual (s1.Value, 45, "A42");
  122. Assert.AreEqual ("45px", s1.ToString (), "A42.1");
  123. s1 = new Unit ("1.75cm");
  124. Assert.AreEqual (s1.Type, UnitType.Cm, "A43");
  125. Assert.AreEqual (s1.Value, 1.75, "A44");
  126. s1 = new Unit ("1.75%");
  127. Assert.AreEqual (s1.Type, UnitType.Percentage, "A45");
  128. Assert.AreEqual (s1.Value, 1.75, "A46");
  129. s1 = new Unit (null);
  130. Assert.AreEqual (s1.Type, UnitType.Pixel, "A47");
  131. Assert.AreEqual (s1.Value, 0, "A48");
  132. Assert.AreEqual (s1.IsEmpty, true, "A49");
  133. Assert.AreEqual (s1.ToString (), "", "A50");
  134. s1 = new Unit ("");
  135. Assert.AreEqual (s1.Type, UnitType.Pixel, "A51");
  136. Assert.AreEqual (s1.Value, 0, "A52");
  137. Assert.AreEqual (s1.IsEmpty, true, "A53");
  138. Assert.AreEqual (s1.ToString (), "", "A54");
  139. s1 = new Unit ("45.75cm");
  140. Assert.AreEqual (s1.Type, UnitType.Cm, "A55");
  141. Assert.AreEqual (s1.Value, 45.75, "A56");
  142. Assert.AreEqual ("45.75cm", s1.ToString (), "A57");
  143. a3 = new Unit (1.5);
  144. Assert.AreEqual (UnitType.Pixel, a3.Type, "A58");
  145. Assert.AreEqual (1.0, a3.Value, "A59");
  146. s1 = new Unit (".9em");
  147. Assert.AreEqual (s1.Type, UnitType.Em, "B1");
  148. Assert.AreEqual (s1.Value, 0.9, "B2");
  149. Assert.AreEqual ("0.9em", s1.ToString (), "B3");
  150. }
  151. [Test]
  152. public void ParseCultures ()
  153. {
  154. // Test cultures where the decimal separator is not "."
  155. CultureInfo ci = new CultureInfo ("es-ES", false);
  156. Unit s1 = new Unit ("1,5cm", ci);
  157. Assert.AreEqual (s1.Type, UnitType.Cm, "C1");
  158. Assert.AreEqual (s1.Value, 1.5, "C2");
  159. Assert.AreEqual (s1.ToString (), "1.5cm", "A54");
  160. Assert.AreEqual (s1.ToString (ci), "1,5cm", "A54");
  161. }
  162. [Test]
  163. public void UnitEquality ()
  164. {
  165. Unit u1 = new Unit ("1px");
  166. Unit u2 = new Unit ("2px");
  167. Unit t1 = new Unit ("1px");
  168. Unit c2 = new Unit ("2cm");
  169. Assert.AreEqual (u1 == t1, true, "U1");
  170. Assert.AreEqual (u1 != u2, true, "U2");
  171. Assert.AreEqual (u1 == u2, false, "U3");
  172. Assert.AreEqual (u1 != t1, false, "U4");
  173. // Test that its comparing the units and value
  174. Assert.AreEqual (u2 != c2, true, "U5");
  175. }
  176. [Test]
  177. public void UnitImplicit ()
  178. {
  179. Unit u = 1;
  180. Assert.AreEqual (u.Value, 1.0, "M1");
  181. Assert.AreEqual (u.Type, UnitType.Pixel, "M1");
  182. }
  183. [Test]
  184. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  185. public void IncorrectConstructor ()
  186. {
  187. Unit a = new Unit (32768.0);
  188. }
  189. [Test]
  190. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  191. public void IncorrectConstructor2 ()
  192. {
  193. Unit a = new Unit (32768);
  194. }
  195. [Test]
  196. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  197. public void IncorrectConstructor3 ()
  198. {
  199. Unit a = new Unit (-32769.0);
  200. }
  201. [Test]
  202. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  203. public void IncorrectConstructor4 ()
  204. {
  205. Unit a = new Unit (-32769);
  206. }
  207. [Test]
  208. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  209. public void IncorrectConstructor5 ()
  210. {
  211. // throws because of space between - and 45
  212. Unit a = new Unit ("- 45cm");
  213. }
  214. [Test]
  215. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  216. public void IncorrectConstructor6 ()
  217. {
  218. // Throws becaues "cma" is not a unit
  219. Unit a = new Unit ("-45cma");
  220. }
  221. [Test]
  222. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  223. public void IncorrectConstructor7 ()
  224. {
  225. // Throws because there is stuff after "%"
  226. Unit a = new Unit ("-45%cm");
  227. }
  228. [Test]
  229. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  230. public void IncorrectConstructor8 ()
  231. {
  232. // throws because there is stuff after cm (a)
  233. Unit a = new Unit ("-45cm a");
  234. }
  235. [Test]
  236. [ExpectedException (typeof (FormatException))]
  237. public void IncorrectConstructor9()
  238. {
  239. // throws because floating point values are not valid for Pixel.
  240. Unit a = new Unit("34.4px");
  241. }
  242. [Test]
  243. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  244. public void IncorrectConstructor10 ()
  245. {
  246. // throws because there is a space before the dot
  247. Unit a = new Unit ("34 .4pt");
  248. }
  249. [Test]
  250. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  251. public void IncorrectConstructor11 ()
  252. {
  253. // throws because there is a space after the dot
  254. Unit a = new Unit ("34. 4pt");
  255. }
  256. #if NET_2_0
  257. class MyFormatProvider : IFormatProvider
  258. {
  259. public object GetFormat (Type format_type)
  260. {
  261. return Activator.CreateInstance (format_type);
  262. }
  263. }
  264. [Test]
  265. [Category ("NotWorking")]
  266. public void Unit_IFormatProviderToString ()
  267. {
  268. MyFormatProvider mfp = new MyFormatProvider ();
  269. Unit a1 = new Unit (1.0);
  270. Assert.AreEqual ("1px", a1.ToString (mfp), "A1");
  271. a1 = new Unit (1);
  272. Assert.AreEqual ("1px", a1.ToString (mfp), "A2");
  273. a1 = new Unit (32767);
  274. Assert.AreEqual ("32767px", a1.ToString (mfp), "A3");
  275. a1 = new Unit (-32768);
  276. Assert.AreEqual ("-32768px", a1.ToString (mfp), "A4");
  277. //
  278. // String constructor
  279. //
  280. Unit s1 = new Unit ("-45cm");
  281. Assert.AreEqual ("-45cm", s1.ToString (mfp), "A5");
  282. s1 = new Unit ("\t-45cm");
  283. Assert.AreEqual ("-45cm", s1.ToString (mfp), "A6");
  284. s1 = new Unit ("45\tcm");
  285. Assert.AreEqual ("45cm", s1.ToString (mfp), "A7");
  286. s1 = new Unit (null);
  287. Assert.AreEqual ("", s1.ToString (mfp), "A8");
  288. s1 = new Unit ("-45");
  289. Assert.AreEqual ("-45px", s1.ToString (mfp), "A9");
  290. s1 = new Unit ("-45%");
  291. Assert.AreEqual ("-45%", s1.ToString (mfp), "A10");
  292. s1 = new Unit ("-45% \t ");
  293. Assert.AreEqual ("-45%", s1.ToString (mfp), "A11");
  294. s1 = new Unit ("-45 % \t ");
  295. Assert.AreEqual ("-45%", s1.ToString (mfp), "A12");
  296. s1 = new Unit ("45in");
  297. Assert.AreEqual ("45in", s1.ToString (mfp), "A13");
  298. s1 = new Unit ("45cm");
  299. Assert.AreEqual ("45cm", s1.ToString (mfp), "A14");
  300. s1 = new Unit ("45pt");
  301. Assert.AreEqual ("45pt", s1.ToString (mfp), "A15");
  302. s1 = new Unit ("45pc");
  303. Assert.AreEqual ("45pc", s1.ToString (mfp), "A16");
  304. s1 = new Unit ("45mm");
  305. Assert.AreEqual ("45mm", s1.ToString (mfp), "A17");
  306. s1 = new Unit ("45em");
  307. Assert.AreEqual ("45em", s1.ToString (mfp), "A18");
  308. s1 = new Unit ("45ex");
  309. Assert.AreEqual ("45ex", s1.ToString (mfp), "A19");
  310. s1 = new Unit ("45px");
  311. Assert.AreEqual ("45px", s1.ToString (mfp), "A20");
  312. s1 = new Unit ("1.75cm");
  313. Assert.AreEqual ("1.75cm", s1.ToString (mfp), "A21");
  314. s1 = new Unit ("1.75%");
  315. Assert.AreEqual ("1.75%", s1.ToString (mfp), "A22");
  316. s1 = new Unit (null);
  317. Assert.AreEqual ("", s1.ToString (mfp), "A23");
  318. s1 = new Unit ("");
  319. Assert.AreEqual ("", s1.ToString (mfp), "A24");
  320. s1 = new Unit ("45.75cm");
  321. Assert.AreEqual ("45.75cm", s1.ToString (mfp), "A25");
  322. a1 = new Unit (1.5);
  323. Assert.AreEqual ("1px", a1.ToString (mfp), "A26");
  324. }
  325. #endif
  326. }
  327. }