2 // System.Web.UI.DataBinderTests
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
10 //#define NUNIT // Comment out this one if you wanna play with the test without using NUnit
13 using NUnit.Framework;
15 using System.Reflection;
23 using System.Runtime.CompilerServices;
25 namespace MonoTests.System.Web.UI
28 public class DataBinderTests : TestCase
31 public class DataBinderTests
35 public static ITest Suite
38 return new TestSuite (typeof (PathTest));
42 public DataBinderTests () : base ("MonoTests.System.Web.UI.DataBinderTests testcase") { }
43 public DataBinderTests (string name) : base (name) { }
45 protected override void SetUp ()
48 static DataBinderTests ()
51 instance = new ClassInstance ("instance");
52 instance.another = new ClassInstance ("another");
53 echo = new StringEcho();
56 static ClassInstance instance;
57 static StringEcho echo;
59 public void TestEval1 ()
62 DataBinder.Eval (instance, "hello");
63 Fail ("Eval1 #1 didn't throw exception");
64 } catch (HttpException) {
67 object o = instance.Prop1;
68 AssertEquals ("Eval1 #2", DataBinder.Eval (instance, "Prop1"), o);
70 AssertEquals ("Eval1 #3", DataBinder.Eval (instance, "Prop2"), o);
72 AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[0]"), o);
73 o = instance ["hi there!"];
74 AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[\"hi there!\"]"), o);
77 public void TestEval2 ()
80 DataBinder.Eval (instance, "Another.hello");
81 Fail ("Eval2 #1 didn't throw exception");
82 } catch (HttpException) {
85 object o = instance.Another.Prop1;
86 AssertEquals ("Eval2 #2", DataBinder.Eval (instance, "Another.Prop1"), o);
87 o = instance.Another.Prop2;
88 AssertEquals ("Eval2 #3", DataBinder.Eval (instance, "Another.Prop2"), o);
89 o = instance.Another [0];
90 AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[0]"), o);
91 o = instance.Another ["hi there!"];
92 AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi there!\"]"), o);
93 AssertEquals ("Eval2 #5", DataBinder.Eval (instance,
94 "Another[\"hi there!\"] MS ignores this]"), o);
96 // MS gets fooled with this!!!
97 //AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi] there!\"]"), o);
100 public void TestEval3 ()
103 DataBinder.Eval (echo, "[0]");
104 Fail ("Eval3 #1 didn't throw exception");
105 } catch (ArgumentException) {
108 AssertEquals ("Eval3 #2", DataBinder.Eval (echo, "[test]"), "test");
109 AssertEquals ("Eval3 #3", DataBinder.Eval (echo, "[\"test\"]"), "test");
110 AssertEquals ("Eval3 #4", DataBinder.Eval (echo, "['test']"), "test");
111 AssertEquals ("Eval3 #5", DataBinder.Eval (echo, "['test\"]"), "'test\"");
112 AssertEquals ("Eval3 #6", DataBinder.Eval (echo, "[\"test']"), "\"test'");
117 void Assert (string msg, bool result)
120 Console.WriteLine (msg);
123 void AssertEquals (string msg, object expected, object real)
125 if (expected == null && real == null)
128 if (expected != null && expected.Equals (real))
131 Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
134 void Fail (string msg)
136 Console.WriteLine ("Failed: {0}", msg);
141 DataBinderTests dbt = new DataBinderTests ();
142 Type t = typeof (DataBinderTests);
143 MethodInfo [] methods = t.GetMethods ();
144 foreach (MethodInfo m in methods) {
145 if (m.Name.Substring (0, 4) == "Test")
146 m.Invoke (dbt, null);
154 public string hello = "Hello";
155 public ClassInstance another;
158 public ClassInstance (string prefix)
160 this.prefix = prefix;
166 return prefix + "This is Prop1";
173 return prefix + "This is Prop2";
177 public object this [int index]
180 return prefix + "This is the indexer for int. Index: " + index;
184 public object this [string index]
187 return prefix + "This is the indexer for string. Index: " + index;
191 public ClassInstance Another
201 public object this [string msg] {