ChtmlTextWriterTest.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // ChtmlTextWriterTest.cs: Unit tests for System.Web.UI.ChtmlTextWriter
  3. //
  4. // Author:
  5. // Cesar Lopez Nataren <[email protected]>
  6. //
  7. // Copyright (C) 2006 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.IO;
  31. using System.Web.UI;
  32. using NUnit.Framework;
  33. using System.Collections;
  34. namespace MonoTests.System.Web.UI {
  35. public class ChtmlTextWriterTester : ChtmlTextWriter {
  36. public ChtmlTextWriterTester (TextWriter writer)
  37. : this (writer, DefaultTabString)
  38. {
  39. }
  40. public ChtmlTextWriterTester (TextWriter writer, string tabString)
  41. : base (writer, tabString)
  42. {
  43. }
  44. public bool IsRecognizedAttribute (string elementName, string attributeName)
  45. {
  46. Hashtable elem_attrs = (Hashtable) RecognizedAttributes [elementName];
  47. if (elem_attrs == null)
  48. return false;
  49. return elem_attrs [attributeName] != null;
  50. }
  51. public string PublicGetAttributeName (HtmlTextWriterAttribute attrKey)
  52. {
  53. return GetAttributeName (attrKey);
  54. }
  55. public bool PublicOnAttributeRender (string name, string value, HtmlTextWriterAttribute attr)
  56. {
  57. return OnAttributeRender (name, value, attr);
  58. }
  59. public bool PublicOnStyleAttributeRender (string name, string value, HtmlTextWriterStyle key)
  60. {
  61. return OnStyleAttributeRender (name, value, key);
  62. }
  63. public bool PublicOnTagRender (string name, HtmlTextWriterTag tag)
  64. {
  65. return OnTagRender (name, tag);
  66. }
  67. public Hashtable PublicGlobalSuppressedAttributes {
  68. get { return GlobalSuppressedAttributes; }
  69. }
  70. public Hashtable PublicSuppressedAttributes {
  71. get { return SuppressedAttributes; }
  72. }
  73. }
  74. [TestFixture]
  75. public class ChtmlTextWriterTest {
  76. ChtmlTextWriterTester chtml;
  77. StringWriter writer;
  78. string absent_element = "absent-elem";
  79. string absent_attr = "absent-attr";
  80. [SetUp]
  81. public void SetupTests ()
  82. {
  83. writer = new StringWriter ();
  84. chtml = new ChtmlTextWriterTester (writer);
  85. }
  86. [Test]
  87. public void AddRecognizedAttributeTest ()
  88. {
  89. chtml.AddRecognizedAttribute (absent_element, absent_attr);
  90. Assertion.AssertEquals ("#A01", true, chtml.IsRecognizedAttribute (absent_element, absent_attr));
  91. }
  92. [Test]
  93. [ExpectedException (typeof (ArgumentException))]
  94. public void AddRecognizedAttribute2 ()
  95. {
  96. AddRecognizedAttributeTest ();
  97. AddRecognizedAttributeTest ();
  98. }
  99. [Test]
  100. public void RemoveRecognizedAttributeTest ()
  101. {
  102. AddRecognizedAttributeTest ();
  103. chtml.RemoveRecognizedAttribute (absent_element, absent_attr);
  104. Assertion.AssertEquals ("#B01", false, chtml.IsRecognizedAttribute (absent_element, absent_attr));
  105. string version_two = "v2";
  106. chtml.RemoveRecognizedAttribute (absent_element + version_two, absent_attr + version_two);
  107. }
  108. [Test]
  109. public void WriteBreakTest ()
  110. {
  111. string br = "<br>";
  112. chtml.WriteBreak ();
  113. Assertion.AssertEquals ("#C01", true, br == writer.ToString ());
  114. }
  115. [Test]
  116. public void WriteEncodedTest ()
  117. {
  118. string encoded_text = "<custID> & <invoice#>";
  119. string unencoded_text = "&lt;custID&gt; &amp; &lt;invoice#&gt;";
  120. chtml.WriteEncodedText (encoded_text);
  121. Assertion.AssertEquals ("#D01", true, unencoded_text == writer.ToString ());
  122. }
  123. [Test]
  124. public void OnAttributeRenderTest ()
  125. {
  126. HtmlTextWriterAttribute [] enum_values = (HtmlTextWriterAttribute []) Enum.GetValues (typeof (HtmlTextWriterAttribute));
  127. int i = 0;
  128. foreach (HtmlTextWriterAttribute attr in enum_values) {
  129. try {
  130. chtml.PublicOnAttributeRender (chtml.PublicGetAttributeName (attr), "accesskey", attr);
  131. } catch (ArgumentNullException e) {
  132. i++;
  133. }
  134. }
  135. Assertion.AssertEquals ("#E01", enum_values.Length, i);
  136. }
  137. [Test]
  138. public void OnStyleAttributeRenderTest ()
  139. {
  140. bool expected;
  141. int i = 0;
  142. foreach (HtmlTextWriterStyle tag in Enum.GetValues (typeof (HtmlTextWriterStyle))) {
  143. expected = (tag == HtmlTextWriterStyle.Display);
  144. Assertion.AssertEquals ("#F0" + i++, expected, chtml.PublicOnStyleAttributeRender ("foo", "foo", tag));
  145. }
  146. }
  147. [Test]
  148. public void OnTagRenderTest ()
  149. {
  150. int i = 0;
  151. bool expected;
  152. foreach (HtmlTextWriterTag tag in Enum.GetValues (typeof (HtmlTextWriterTag))) {
  153. expected = (tag != HtmlTextWriterTag.Span);
  154. Assertion.AssertEquals ("#G0" + i++, expected, chtml.PublicOnTagRender ("foo", tag));
  155. }
  156. }
  157. }
  158. }
  159. #endif