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