~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/maccore/tests/bindings/ApiSelectorTest.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Test the generated API selectors against typos or non-existing cases
 
3
//
 
4
// Authors:
 
5
//      Sebastien Pouliot  <sebastien@xamarin.com>
 
6
//
 
7
// Copyright 2012-2013 Xamarin Inc.
 
8
//
 
9
// Licensed under the Apache License, Version 2.0 (the "License");
 
10
// you may not use this file except in compliance with the License.
 
11
// You may obtain a copy of the License at
 
12
//
 
13
//   http://www.apache.org/licenses/LICENSE-2.0
 
14
//
 
15
// Unless required by applicable law or agreed to in writing, software
 
16
// distributed under the License is distributed on an "AS IS" BASIS,
 
17
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
18
// See the License for the specific language governing permissions and
 
19
// limitations under the License.
 
20
//
 
21
 
 
22
using System;
 
23
using System.Reflection;
 
24
using NUnit.Framework;
 
25
 
 
26
#if MONOMAC
 
27
using MonoMac.Foundation;
 
28
using MonoMac.ObjCRuntime;
 
29
#else
 
30
using MonoTouch.Foundation;
 
31
using MonoTouch.ObjCRuntime;
 
32
#endif
 
33
 
 
34
namespace TouchUnit.Bindings {
 
35
 
 
36
        public abstract class ApiSelectorTest : ApiBaseTest {
 
37
                
 
38
                protected int Errors;
 
39
 
 
40
                // not everything should be even tried
 
41
                
 
42
                protected virtual bool Skip (Type type)
 
43
                {
 
44
                        // skip delegate (and other protocol references)
 
45
                        foreach (object ca in type.GetCustomAttributes (false)) {
 
46
                                if (ca is ModelAttribute)
 
47
                                        return true;
 
48
                        }
 
49
                        return false;
 
50
                }
 
51
 
 
52
                protected virtual bool Skip (Type type, string selectorName)
 
53
                {
 
54
                        return false;
 
55
                }
 
56
 
 
57
                protected virtual bool CheckResponse (bool value, Type actualType, Type declaredType, ref string name)
 
58
                {
 
59
                        if (value)
 
60
                                return true;
 
61
                        
 
62
                        name = actualType.FullName + " : " + name;
 
63
                        return false;
 
64
                }
 
65
                
 
66
                [Test]
 
67
                public void InstanceMethods ()
 
68
                {
 
69
                        int n = 0;
 
70
                        
 
71
                        IntPtr responds_handle = Selector.GetHandle ("instancesRespondToSelector:");
 
72
                        
 
73
                        foreach (Type t in Assembly.GetTypes ()) {
 
74
                                if (t.IsNested || !NSObjectType.IsAssignableFrom (t))
 
75
                                        continue;
 
76
 
 
77
                                if (Skip (t) || SkipDueToAttribute (t))
 
78
                                        continue;
 
79
                                
 
80
                                FieldInfo fi = t.GetField ("class_ptr", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
 
81
                                if (fi == null)
 
82
                                        continue; // e.g. *Delegate
 
83
                                IntPtr class_ptr = (IntPtr) fi.GetValue (null);
 
84
 
 
85
                                foreach (var m in t.GetMethods (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) {
 
86
                                        
 
87
                                        if (m.DeclaringType != t || SkipDueToAttribute (m))
 
88
                                                continue;
 
89
 
 
90
                                        foreach (object ca in m.GetCustomAttributes (true)) {
 
91
                                                ExportAttribute export = (ca as ExportAttribute);
 
92
                                                if (export == null)
 
93
                                                        continue;
 
94
 
 
95
                                                string name = export.Selector;
 
96
                                                if (Skip (t, name))
 
97
                                                        continue;
 
98
                                                
 
99
                                                bool result = Messaging.bool_objc_msgSend_IntPtr (class_ptr, responds_handle, Selector.GetHandle (name));
 
100
                                                bool response = CheckResponse (result, t, m.DeclaringType, ref name);
 
101
                                                if (!ContinueOnFailure)
 
102
                                                        Assert.IsTrue (response, name);
 
103
                                                else if (!response) {
 
104
                                                        CheckResponse (result, t, m.DeclaringType, ref name);
 
105
                                                        Console.WriteLine ("[FAIL] {0}", name);
 
106
                                                        Errors++;
 
107
                                                }
 
108
                                                n++;
 
109
                                        }
 
110
                                }
 
111
                        }
 
112
                        Assert.AreEqual (0, Errors, "{0} errors found in {1} instance selector validated", Errors, n);
 
113
                }
 
114
                
 
115
                protected virtual void Dispose (NSObject obj, Type type)
 
116
                {
 
117
                        obj.Dispose ();
 
118
                }
 
119
                
 
120
                // funny, this is how I envisioned the instance version... before hitting run :|
 
121
                protected virtual bool CheckStaticResponse (bool value, Type actualType, Type declaredType, ref string name)
 
122
                {
 
123
                        if (value)
 
124
                                return true;
 
125
                        
 
126
                        name = actualType.FullName + " : " + name;
 
127
                        return false;
 
128
                }
 
129
 
 
130
                [Test]
 
131
                public void StaticMethods ()
 
132
                {
 
133
                        Errors = 0;
 
134
                        int n = 0;
 
135
                        
 
136
                        IntPtr responds_handle = Selector.GetHandle ("respondsToSelector:");
 
137
                        
 
138
                        foreach (Type t in Assembly.GetTypes ()) {
 
139
                                if (t.IsNested || !NSObjectType.IsAssignableFrom (t))
 
140
                                        continue;
 
141
 
 
142
                                if (Skip (t) || SkipDueToAttribute (t))
 
143
                                        continue;
 
144
 
 
145
                                FieldInfo fi = t.GetField ("class_ptr", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
 
146
                                if (fi == null)
 
147
                                        continue; // e.g. *Delegate
 
148
                                IntPtr class_ptr = (IntPtr) fi.GetValue (null);
 
149
                                
 
150
                                foreach (var m in t.GetMethods (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static)) {
 
151
                                        if (SkipDueToAttribute (m))
 
152
                                                continue;
 
153
 
 
154
                                        foreach (object ca in m.GetCustomAttributes (true)) {
 
155
                                                if (ca is ExportAttribute) {
 
156
                                                        string name = (ca as ExportAttribute).Selector;
 
157
                                                        bool result = Messaging.bool_objc_msgSend_IntPtr (class_ptr, responds_handle, Selector.GetHandle (name));
 
158
                                                        bool response = CheckStaticResponse (result, t, m.DeclaringType, ref name);
 
159
                                                        if (!ContinueOnFailure)
 
160
                                                                Assert.IsTrue (response, name);
 
161
                                                        else if (!response) {
 
162
                                                                Console.WriteLine ("[FAIL] {0}", name);
 
163
                                                                Errors++;
 
164
                                                        }
 
165
                                                        n++;
 
166
                                                }
 
167
                                        }
 
168
                                }
 
169
                        }
 
170
                        Assert.AreEqual (0, Errors, "{0} errors found in {1} static selector validated", Errors, n);
 
171
                }
 
172
                
 
173
                protected virtual bool HasNoSetter (PropertyInfo p)
 
174
                {
 
175
                        return false;
 
176
                }
 
177
 
 
178
                [Test]
 
179
                public void MissingSetters ()
 
180
                {
 
181
                        Errors = 0;
 
182
                        int n = 0;
 
183
                        
 
184
                        IntPtr responds_handle = Selector.GetHandle ("instancesRespondToSelector:");
 
185
 
 
186
                        foreach (Type t in Assembly.GetTypes ()) {
 
187
                                if (t.IsNested || !NSObjectType.IsAssignableFrom (t))
 
188
                                        continue;
 
189
 
 
190
                                if (Skip (t))
 
191
                                        continue;
 
192
 
 
193
                                // static properties
 
194
                                
 
195
                                FieldInfo fi = t.GetField ("class_ptr", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
 
196
                                if (fi == null)
 
197
                                        continue; // e.g. *Delegate
 
198
                                IntPtr class_ptr = (IntPtr) fi.GetValue (null);
 
199
                                
 
200
                                foreach (var p in t.GetProperties (BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance)) {
 
201
                                        
 
202
                                        if (p.DeclaringType != t || SkipDueToAttribute (p))
 
203
                                                continue;
 
204
 
 
205
                                        var mg = p.GetGetMethod ();
 
206
                                        var ms = p.GetSetMethod ();
 
207
                                        if (HasNoSetter (p) || (mg == null) || (ms != null))
 
208
                                                continue;
 
209
 
 
210
                                        if (SkipDueToAttribute (mg) || SkipDueToAttribute (ms))
 
211
                                                continue;
 
212
 
 
213
                                        foreach (object ca in mg.GetCustomAttributes (true)) {
 
214
                                                if (ca is ExportAttribute) {
 
215
                                                        // if getter has [Export] assume it's valid (there's a test for this)
 
216
                                                        string name = (ca as ExportAttribute).Selector;
 
217
                                                        if (!Messaging.bool_objc_msgSend_IntPtr (class_ptr, responds_handle, Selector.GetHandle (name)))
 
218
                                                                continue;
 
219
 
 
220
                                                        n++;
 
221
                                                        string setter_selector = String.Format ("set{0}{1}:", Char.ToUpperInvariant (name [0]), name.Substring (1));
 
222
                                                        if (LogProgress)
 
223
                                                                Console.WriteLine ("{0} {1} '{2} {3}' selector: {4}", n, t.Name, mg.IsStatic ? "static" : "instance", p, setter_selector);
 
224
 
 
225
                                                        bool result = !Messaging.bool_objc_msgSend_IntPtr (class_ptr, responds_handle, Selector.GetHandle (setter_selector));
 
226
                                                        if (!ContinueOnFailure)
 
227
                                                                Assert.IsTrue (result, t.Name + " - " + setter_selector);
 
228
                                                        else if (!result) {
 
229
                                                                Console.WriteLine ("[FAIL] {0} {1}", t, setter_selector);
 
230
                                                                Errors++;
 
231
                                                        }
 
232
                                                }
 
233
                                        }
 
234
                                }
 
235
                        }
 
236
                        Assert.AreEqual (0, Errors, "{0} potential errors found in {1} setters validated", Errors, n);
 
237
                }
 
238
        }
 
239
}