DataBinderTests.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. //
  2. // System.Web.UI.DataBinderTests
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  8. //
  9. //#define NUNIT // Comment out this one if you wanna play with the test without using NUnit
  10. #if NUNIT
  11. using NUnit.Framework;
  12. #else
  13. using System.Reflection;
  14. #endif
  15. using System.IO;
  16. using System;
  17. using System.Text;
  18. using System.Web;
  19. using System.Web.UI;
  20. using System.Runtime.CompilerServices;
  21. namespace MonoTests.System.Web.UI
  22. {
  23. #if NUNIT
  24. public class DataBinderTests : TestCase
  25. {
  26. #else
  27. public class DataBinderTests
  28. {
  29. #endif
  30. #if NUNIT
  31. public static ITest Suite
  32. {
  33. get {
  34. return new TestSuite (typeof (PathTest));
  35. }
  36. }
  37. public DataBinderTests () : base ("MonoTests.System.Web.UI.DataBinderTests testcase") { }
  38. public DataBinderTests (string name) : base (name) { }
  39. protected override void SetUp ()
  40. {
  41. #else
  42. static DataBinderTests ()
  43. {
  44. #endif
  45. instance = new ClassInstance ("instance");
  46. instance.another = new ClassInstance ("another");
  47. echo = new StringEcho();
  48. }
  49. static ClassInstance instance;
  50. static StringEcho echo;
  51. public void TestEval1 ()
  52. {
  53. try {
  54. DataBinder.Eval (instance, "hello");
  55. Fail ("Eval1 #1 didn't throw exception");
  56. } catch (HttpException) {
  57. }
  58. object o = instance.Prop1;
  59. AssertEquals ("Eval1 #2", DataBinder.Eval (instance, "Prop1"), o);
  60. o = instance.Prop2;
  61. AssertEquals ("Eval1 #3", DataBinder.Eval (instance, "Prop2"), o);
  62. o = instance [0];
  63. AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[0]"), o);
  64. o = instance ["hi there!"];
  65. AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[\"hi there!\"]"), o);
  66. }
  67. public void TestEval2 ()
  68. {
  69. try {
  70. DataBinder.Eval (instance, "Another.hello");
  71. Fail ("Eval2 #1 didn't throw exception");
  72. } catch (HttpException) {
  73. }
  74. object o = instance.Another.Prop1;
  75. AssertEquals ("Eval2 #2", DataBinder.Eval (instance, "Another.Prop1"), o);
  76. o = instance.Another.Prop2;
  77. AssertEquals ("Eval2 #3", DataBinder.Eval (instance, "Another.Prop2"), o);
  78. o = instance.Another [0];
  79. AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[0]"), o);
  80. o = instance.Another ["hi there!"];
  81. AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi there!\"]"), o);
  82. AssertEquals ("Eval2 #5", DataBinder.Eval (instance,
  83. "Another[\"hi there!\"] MS ignores this]"), o);
  84. // MS gets fooled with this!!!
  85. //AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi] there!\"]"), o);
  86. }
  87. public void TestEval3 ()
  88. {
  89. try {
  90. DataBinder.Eval (echo, "[0]");
  91. Fail ("Eval3 #1 didn't throw exception");
  92. } catch (ArgumentException) {
  93. }
  94. AssertEquals ("Eval3 #2", DataBinder.Eval (echo, "[test]"), "test");
  95. AssertEquals ("Eval3 #3", DataBinder.Eval (echo, "[\"test\"]"), "test");
  96. AssertEquals ("Eval3 #4", DataBinder.Eval (echo, "['test']"), "test");
  97. AssertEquals ("Eval3 #5", DataBinder.Eval (echo, "['test\"]"), "'test\"");
  98. AssertEquals ("Eval3 #6", DataBinder.Eval (echo, "[\"test']"), "\"test'");
  99. }
  100. #if !NUNIT
  101. void Assert (string msg, bool result)
  102. {
  103. if (!result)
  104. Console.WriteLine (msg);
  105. }
  106. void AssertEquals (string msg, object expected, object real)
  107. {
  108. if (expected == null && real == null)
  109. return;
  110. if (expected != null && expected.Equals (real))
  111. return;
  112. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  113. }
  114. void Fail (string msg)
  115. {
  116. Console.WriteLine ("Failed: {0}", msg);
  117. }
  118. static void Main ()
  119. {
  120. DataBinderTests dbt = new DataBinderTests ();
  121. Type t = typeof (DataBinderTests);
  122. MethodInfo [] methods = t.GetMethods ();
  123. foreach (MethodInfo m in methods) {
  124. if (m.Name.Substring (0, 4) == "Test")
  125. m.Invoke (dbt, null);
  126. }
  127. }
  128. #endif
  129. }
  130. class ClassInstance
  131. {
  132. public string hello = "Hello";
  133. public ClassInstance another;
  134. string prefix;
  135. public ClassInstance (string prefix)
  136. {
  137. this.prefix = prefix;
  138. }
  139. public object Prop1
  140. {
  141. get {
  142. return prefix + "This is Prop1";
  143. }
  144. }
  145. public object Prop2
  146. {
  147. get {
  148. return prefix + "This is Prop2";
  149. }
  150. }
  151. public object this [int index]
  152. {
  153. get {
  154. return prefix + "This is the indexer for int. Index: " + index;
  155. }
  156. }
  157. public object this [string index]
  158. {
  159. get {
  160. return prefix + "This is the indexer for string. Index: " + index;
  161. }
  162. }
  163. public ClassInstance Another
  164. {
  165. get {
  166. return another;
  167. }
  168. }
  169. }
  170. class StringEcho
  171. {
  172. public object this [string msg] {
  173. get { return msg; }
  174. }
  175. }
  176. }