DataBinderTests.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. }
  48. static ClassInstance instance;
  49. public void TestEval1 ()
  50. {
  51. try {
  52. DataBinder.Eval (instance, "hello");
  53. Fail ("Eval1 #1 didn't throw exception");
  54. } catch (HttpException) {
  55. }
  56. object o = instance.Prop1;
  57. AssertEquals ("Eval1 #2", DataBinder.Eval (instance, "Prop1"), o);
  58. o = instance.Prop2;
  59. AssertEquals ("Eval1 #3", DataBinder.Eval (instance, "Prop2"), o);
  60. o = instance [0];
  61. AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[0]"), o);
  62. o = instance ["hi there!"];
  63. AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[\"hi there!\"]"), o);
  64. }
  65. public void TestEval2 ()
  66. {
  67. try {
  68. DataBinder.Eval (instance, "Another.hello");
  69. Fail ("Eval2 #1 didn't throw exception");
  70. } catch (HttpException) {
  71. }
  72. object o = instance.Another.Prop1;
  73. AssertEquals ("Eval2 #2", DataBinder.Eval (instance, "Another.Prop1"), o);
  74. o = instance.Another.Prop2;
  75. AssertEquals ("Eval2 #3", DataBinder.Eval (instance, "Another.Prop2"), o);
  76. o = instance.Another [0];
  77. AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[0]"), o);
  78. o = instance.Another ["hi there!"];
  79. AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi there!\"]"), o);
  80. AssertEquals ("Eval2 #5", DataBinder.Eval (instance,
  81. "Another[\"hi there!\"] MS ignores this]"), o);
  82. // MS gets fooled with this!!!
  83. //AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi] there!\"]"), o);
  84. }
  85. #if !NUNIT
  86. void Assert (string msg, bool result)
  87. {
  88. if (!result)
  89. Console.WriteLine (msg);
  90. }
  91. void AssertEquals (string msg, object expected, object real)
  92. {
  93. if (expected == null && real == null)
  94. return;
  95. if (expected != null && expected.Equals (real))
  96. return;
  97. Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
  98. }
  99. void Fail (string msg)
  100. {
  101. Console.WriteLine ("Failed: {0}", msg);
  102. }
  103. static void Main ()
  104. {
  105. DataBinderTests dbt = new DataBinderTests ();
  106. Type t = typeof (DataBinderTests);
  107. MethodInfo [] methods = t.GetMethods ();
  108. foreach (MethodInfo m in methods) {
  109. if (m.Name.Substring (0, 4) == "Test")
  110. m.Invoke (dbt, null);
  111. }
  112. }
  113. #endif
  114. }
  115. class ClassInstance
  116. {
  117. public string hello = "Hello";
  118. public ClassInstance another;
  119. string prefix;
  120. public ClassInstance (string prefix)
  121. {
  122. this.prefix = prefix;
  123. }
  124. public object Prop1
  125. {
  126. get {
  127. return prefix + "This is Prop1";
  128. }
  129. }
  130. public object Prop2
  131. {
  132. get {
  133. return prefix + "This is Prop2";
  134. }
  135. }
  136. public object this [int index]
  137. {
  138. get {
  139. return prefix + "This is the indexer for int. Index: " + index;
  140. }
  141. }
  142. public object this [string index]
  143. {
  144. get {
  145. return prefix + "This is the indexer for string. Index: " + index;
  146. }
  147. }
  148. public ClassInstance Another
  149. {
  150. get {
  151. return another;
  152. }
  153. }
  154. }
  155. }