This test is for 4.0+ only
[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                         echo = new StringEcho();
54                 }
55
56                 static ClassInstance instance;
57                 static StringEcho echo;
58
59                 public void TestEval1 ()
60                 {
61                         try {
62                                 DataBinder.Eval (instance, "hello");
63                                 Fail ("Eval1 #1 didn't throw exception");
64                         } catch (HttpException) {
65                         }
66
67                         object o = instance.Prop1;
68                         AssertEquals ("Eval1 #2", DataBinder.Eval (instance, "Prop1"), o);
69                         o = instance.Prop2;
70                         AssertEquals ("Eval1 #3", DataBinder.Eval (instance, "Prop2"), o);
71                         o = instance [0];
72                         AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[0]"), o);
73                         o = instance ["hi there!"];
74                         AssertEquals ("Eval1 #4", DataBinder.Eval (instance, "[\"hi there!\"]"), o);
75                 }
76
77                 public void TestEval2 ()
78                 {
79                         try {
80                                 DataBinder.Eval (instance, "Another.hello");
81                                 Fail ("Eval2 #1 didn't throw exception");
82                         } catch (HttpException) {
83                         }
84
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);
95
96                         // MS gets fooled with this!!!
97                         //AssertEquals ("Eval2 #4", DataBinder.Eval (instance, "Another[\"hi] there!\"]"), o);
98                 }
99
100                 public void TestEval3 ()
101                 {
102                         try {
103                                 DataBinder.Eval (echo, "[0]");
104                                 Fail ("Eval3 #1 didn't throw exception");
105                         } catch (ArgumentException) {
106                         }
107
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'");
113                 }
114
115
116 #if !NUNIT
117                 void Assert (string msg, bool result)
118                 {
119                         if (!result)
120                                 Console.WriteLine (msg);
121                 }
122
123                 void AssertEquals (string msg, object expected, object real)
124                 {
125                         if (expected == null && real == null)
126                                 return;
127
128                         if (expected != null && expected.Equals (real))
129                                 return;
130
131                         Console.WriteLine ("{0}: expected: '{1}', got: '{2}'", msg, expected, real);
132                 }
133
134                 void Fail (string msg)
135                 {
136                         Console.WriteLine ("Failed: {0}", msg);
137                 }
138
139                 static void Main ()
140                 {
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);
147                         }
148                 }
149 #endif
150         }
151
152         class ClassInstance
153         {
154                 public string hello = "Hello";
155                 public ClassInstance another;
156                 string prefix;
157
158                 public ClassInstance (string prefix)
159                 {
160                         this.prefix = prefix;
161                 }
162                 
163                 public object Prop1
164                 {
165                         get {
166                                 return prefix + "This is Prop1";
167                         }
168                 }
169
170                 public object Prop2
171                 {
172                         get {
173                                 return prefix + "This is Prop2";
174                         }
175                 }
176
177                 public object this [int index]
178                 {
179                         get {
180                                 return prefix + "This is the indexer for int. Index: " + index;
181                         }
182                 }
183
184                 public object this [string index]
185                 {
186                         get {
187                                 return prefix + "This is the indexer for string. Index: " + index;
188                         }
189                 }
190
191                 public ClassInstance Another
192                 {
193                         get {
194                                 return another;
195                         }
196                 }
197         }
198
199         class StringEcho
200         {
201                 public object this [string msg] {
202                         get { return msg; }
203                 }
204         }
205 }
206