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