stub out things to get the beatles xbox site up and running
[moon.git] / class / System.Windows / scripts / checks.cs
bloba3fffbe8f2dff73c3a01a128dd917389c7d13b9d
1 using System;
2 using System.Reflection;
3 using System.Collections.Generic;
4 using System.Text;
5 using System.Windows;
6 using System.Windows.Media;
7 using System.Windows.Shapes;
8 using System.Windows.Media.Animation;
9 using System.Windows.Controls;
10 using System.Windows.Input;
12 class t
14 static int Main (string [] args)
16 Type DO = typeof (System.Windows.DependencyObject);
17 List<Type> types = new List<Type> (DO.Assembly.GetTypes ());
19 for (int i = types.Count - 1; i >= 0; i--) {
20 if (types [i] == DO)
21 types.RemoveAt (i);
22 else if (!types [i].IsSubclassOf (DO))
23 types.RemoveAt (i);
24 else if (types [i].IsNotPublic)
25 types.RemoveAt (i);
28 if (args.Length >= 1 && args [0] == "generate-test-code") {
29 Console.WriteLine (GenerateTestCode (types));
30 } else {
31 return DoChecks (types);
34 return 0;
37 static int DoChecks (List <Type> types)
39 int result = 0;
40 int normal_ctor_size = 12;
41 int normal_ctor_intptr_size = 8;
43 foreach (Type tp in types) {
44 //Console.WriteLine ("Checking: " + tp.FullName);
45 MethodInfo mi;
46 mi = tp.GetMethod ("GetKind", BindingFlags.NonPublic | BindingFlags.Instance);
47 if (mi == null || mi.DeclaringType != tp) {
48 Console.WriteLine ("Error: 1. The class '{0}' does not implement 'GetKind'.", tp.FullName);
49 result = 1;
52 ConstructorInfo ci = tp.GetConstructor (Type.EmptyTypes);
54 if (ci == null) {
55 if (tp.FullName.IndexOf ("Internal") < 0) {
56 Console.WriteLine ("Error: 2a. The class '{0}' does not have an empty constructor.", tp.FullName);
57 result = 1;
59 } else if (!ci.IsPublic) {
60 Console.WriteLine ("Error: 2b. The class' '{0}' empty constructor is not public.", tp.FullName);
61 result = 1;
62 } else if (result == 0) {
63 MethodBody body = ci.GetMethodBody ();
64 byte [] il = body.GetILAsByteArray ();
65 if (il.Length != normal_ctor_size)
66 Console.WriteLine ("Warning: 2c. {1,3} bytes is the size of the empty constructor of the class '{0}' (normal size = {2}).", tp.FullName, il.Length, normal_ctor_size);
70 foreach (Type tp in types) {
71 ConstructorInfo ci = tp.GetConstructor (BindingFlags.Instance | BindingFlags.NonPublic, null, new Type [] {typeof (IntPtr)}, null);
73 if (ci == null) {
74 Console.WriteLine ("Error: 3a. The class '{0}' does not have an IntPtr constructor.", tp.FullName);
75 result = 1;
76 } else if (!ci.IsAssembly || ci.IsPublic || ci.IsFamily) {
77 Console.WriteLine ("Error: 3b. The class' '{0}' IntPtr constructor is not internal.", tp.FullName);
78 result = 1;
79 } else if (result == 0) {
80 MethodBody body = ci.GetMethodBody ();
81 byte [] il = body.GetILAsByteArray ();
82 if (il.Length != normal_ctor_intptr_size)
83 Console.WriteLine ("Warning: 3c. {1,3} bytes is the size of the IntPtr constructor of the class '{0}' (normal size = {2}).", tp.FullName, il.Length, normal_ctor_intptr_size);
86 return result;
89 static string GenerateTestCode (List<Type> types)
91 StringBuilder result = new StringBuilder ();
93 result.AppendLine (@"
94 using System;
95 using System.Reflection;
96 using System.Collections.Generic;
97 using System.Text;
98 using System.Windows;
99 using System.Windows.Media;
100 using System.Windows.Shapes;
101 using System.Windows.Media.Animation;
102 using System.Windows.Controls;
103 using System.Windows.Input;
104 using System.Windows.Ink;
106 result.AppendLine ("class tester {");
107 result.AppendLine ("static void Main () {");
108 result.AppendLine ("string a,b;");
109 foreach (Type tp in types) {
110 if (tp.IsAbstract) {
111 continue;
114 if (tp.GetConstructor (Type.EmptyTypes) == null) {
115 result.AppendLine ("// Don't know how to create a " + tp.FullName);
116 continue;
119 result.AppendLine ("try {");
120 //result.AppendLine (string.Format (" Console.WriteLine (\"Testing {0}\");", tp.FullName));
121 result.AppendLine (string.Format (" {1} var_{0} = new {1} ();", tp.Name, tp.FullName));
122 foreach (PropertyInfo field in tp.GetProperties ()) {
123 Type ftp = field.PropertyType;
124 string var = string.Empty;
125 string nullvar = string.Empty;
126 string vartp = ftp.FullName;
128 if ((field.CanRead && field.GetGetMethod (true).IsStatic) || (field.CanWrite && field.GetSetMethod (true).IsStatic))
129 continue;
131 if (ftp == typeof(int)) {
132 var = "1";
133 } else if (ftp == typeof(double)) {
134 var = "1.1";
135 } else if (ftp == typeof (string)) {
136 var = "\"abc\"";
137 } else if (ftp == typeof (bool)) {
138 var = "true";
139 } else if (ftp == typeof (object)) {
140 var = "null";
141 } else if (ftp == typeof (Uri)) {
142 var = "new Uri (\"http://www.mono-project.com\");";
143 } else if (ftp == typeof (Point)) {
144 var = "new Point (2, 3)";
145 } else if (ftp == typeof (Transform)) {
146 var = "new TranslateTransform ()";
147 } else if (ftp.IsSubclassOf (typeof (DependencyObject)) && !ftp.IsAbstract) {
148 var = "new " + ftp.Name + "()";
149 } else if (ftp.IsEnum) {
150 var = "(" + ftp.Name + ") 1";
151 } else if (ftp == typeof (double[])) {
152 var = "new double [] {1, 2, 3}";
153 } else if (ftp == typeof (Point [])) {
154 var = "new Point [] {new Point (1, 2), new Point (3, 4)}";
155 } else if (ftp == typeof (Color)) {
156 var = "Color.FromArgb (0, 25, 25, 25)";
157 } else if (ftp == typeof (Geometry)) {
158 var = "new PathGeometry ()";
159 } else if (ftp == typeof (Rect)) {
160 var = "new Rect (1, 2, 3, 4)";
161 } else if (ftp == typeof (Matrix)) {
162 var = "new Matrix (1, 2, 3, 4, 5, 6)";
163 } else if (ftp == typeof (Timeline)) {
164 var = "new Storyboard ()";
165 } else if (ftp == typeof (Duration)) {
166 var = "new Duration (new TimeSpan ())";
167 } else if (ftp == typeof (RepeatBehavior)) {
168 var = "new RepeatBehavior (12.34)";
169 } else if (ftp == typeof (Nullable<TimeSpan>)) {
170 nullvar = "null";
171 var = "new Nullable<TimeSpan> (new TimeSpan ())";
172 vartp = "Nullable<TimeSpan>";
173 } else if (ftp == typeof (Nullable<double>)) {
174 nullvar = "null";
175 var = "new Nullable<double> (1.2)";
176 vartp = "Nullable<double>";
177 } else if (ftp == typeof (Nullable<Color>)) {
178 nullvar = "null";
179 var = "new Nullable<Color> (Color.FromArgb (0, 25, 50, 75))";
180 vartp = "Nullable<Color>";
181 } else if (ftp == typeof (Nullable<Point>)) {
182 nullvar = "null";
183 var = "new Nullable<Point> (new Point (25, 75))";
184 vartp = "Nullable<Point>";
185 } else if (ftp == typeof (Brush)) {
186 var = "new SolidColorBrush (Color.FromArgb (0, 25, 50, 75))";
187 } else if (ftp == typeof (KeyTime)) {
188 var = "KeyTime.Uniform";
189 } else if (ftp == typeof (TimeSpan)) {
190 var = "new TimeSpan (123456)";
191 } else {
192 Console.WriteLine ("//Don't know how to test a '" + ftp.FullName + "'.");
195 if (var == string.Empty)
196 continue;
198 if (field.Name == "Item")
199 continue;
201 if (!field.CanWrite && !field.CanRead) {
202 Console.WriteLine ("Can't read nor write to: " + field.Name);
203 continue;
206 if (field.CanWrite || field.CanRead) {
207 //result.AppendLine (string.Format (" Console.WriteLine (\"Testing {0}.{1} <{2}>\");", tp.FullName, field.Name, ftp.FullName));
210 if (field.CanWrite)
211 result.AppendLine (string.Format (" {2} field_{0}_{1}_a;", tp.Name, field.Name, vartp));
213 if (field.CanRead) {
214 result.AppendLine (string.Format (" {2} field_{0}_{1}_b;", tp.Name, field.Name, vartp));
217 foreach (string code in new string [] {var, nullvar}) {
218 if (code == string.Empty || code == null)
219 continue;
220 bool cw = field.CanWrite && field.GetSetMethod (true).IsPublic; //!field.GetSetMethod (true).IsPrivate && !field.GetSetMethod (true).IsAssembly && !field.;
221 bool cr = field.CanRead && field.GetGetMethod (true).IsPublic; //!field.GetGetMethod (true).IsPrivate && !field.GetGetMethod (true).IsAssembly;
223 if (cw) {
224 result.AppendLine (string.Format (" field_{0}_{1}_a = {3};", tp.Name, field.Name, vartp, code));
225 result.AppendLine (string.Format (" var_{0}.{1} = field_{0}_{1}_a;", tp.Name, field.Name));
227 if (cr) {
228 result.AppendLine (string.Format (" field_{0}_{1}_b = var_{0}.{1};", tp.Name, field.Name, vartp));
230 if (cr && cw) {
231 if (ftp.IsClass) {
232 result.AppendLine (string.Format (" a = field_{0}_{1}_a == null ? \"<null>\" : field_{0}_{1}_a.ToString ();", tp.Name, field.Name));
233 result.AppendLine (string.Format (" b = field_{0}_{1}_b == null ? \"<null>\" : field_{0}_{1}_b.ToString ();", tp.Name, field.Name));
234 } else {
235 result.AppendLine (string.Format (" a = field_{0}_{1}_a.ToString ();", tp.Name, field.Name));
236 result.AppendLine (string.Format (" b = field_{0}_{1}_b.ToString ();", tp.Name, field.Name));
238 result.AppendLine (string.Format (" if (string.CompareOrdinal (a, b) != 0) {{"));
239 result.AppendLine (string.Format (" Console.WriteLine (\"While testing {0}.{1} got:\");", tp.FullName, field.Name));
240 result.AppendLine (string.Format (" Console.WriteLine (\" Wrote '{{0}}' and got '{{1}}'\", a, b);", tp.Name, field.Name));
241 result.AppendLine (" }");
245 result.AppendLine ("} catch (TypeInitializationException ex) {");
246 result.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got:\");", tp.FullName));
247 result.AppendLine (" Console.WriteLine (\" \" + ex.InnerException.Message);");
248 result.AppendLine ("} catch (EntryPointNotFoundException ex) {");
249 result.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got EntryPointNotFoundException:\");", tp.FullName));
250 result.AppendLine (" Console.WriteLine (\" \" + ex.Message);");
251 result.AppendLine ("} catch (Exception ex) {");
252 result.AppendLine (string.Format (" Console.WriteLine (\"While testing {0} got:\");", tp.FullName));
253 result.AppendLine (" Console.WriteLine (\" \" + ex.Message);");
254 result.AppendLine (" Console.WriteLine (ex.StackTrace);");
255 result.AppendLine ("}");
256 result.AppendLine ("");
258 result.AppendLine ("}");
259 result.AppendLine ("}");
260 return result.ToString ();