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

« back to all changes in this revision

Viewing changes to external/maccore/tests/bindings/ApiCtorInitTest.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 `init` selectors are usable by the binding consumers
 
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
 
 
25
using NUnit.Framework;
 
26
 
 
27
#if MONOMAC
 
28
using MonoMac.Foundation;
 
29
#else
 
30
using MonoTouch.Foundation;
 
31
#endif
 
32
 
 
33
namespace TouchUnit.Bindings {
 
34
 
 
35
        public abstract class ApiCtorInitTest : ApiBaseTest {
 
36
 
 
37
                string instance_type_name;
 
38
                protected int Errors;
 
39
 
 
40
                /// <summary>
 
41
                /// Gets or sets a value indicating whether this test fixture will log untested types.
 
42
                /// </summary>
 
43
                /// <value><c>true</c> if log untested types; otherwise, <c>false</c>.</value>
 
44
                public bool LogUntestedTypes { get; set; }
 
45
 
 
46
                /// <summary>
 
47
                /// Override this method if you want the test to skip some specific types.
 
48
                /// By default types decorated with [Model] will be skipped.
 
49
                /// </summary>
 
50
                /// <param name="type">The Type to be tested</param>
 
51
                protected virtual bool Skip (Type type)
 
52
                {
 
53
                        // skip delegate (and other protocol references)
 
54
                        foreach (object ca in type.GetCustomAttributes (false)) {
 
55
                                if (ca is ModelAttribute)
 
56
                                        return true;
 
57
                        }
 
58
                        return false;
 
59
                }
 
60
 
 
61
                /// <summary>
 
62
                /// Checks that the Handle property of the specified NSObject instance is not null (not IntPtr.Zero).
 
63
                /// </summary>
 
64
                /// <param name="obj">NSObject instance to validate</param>
 
65
                protected virtual void CheckHandle (NSObject obj)
 
66
                {
 
67
                        bool result = obj.Handle != IntPtr.Zero;
 
68
                        if (!ContinueOnFailure)
 
69
                                Assert.IsTrue (result, instance_type_name + ".Handle");
 
70
                        else if (!result) {
 
71
                                Console.WriteLine ("[FAIL] {0} : Handle", instance_type_name);
 
72
                                Errors++;
 
73
                        }
 
74
                }
 
75
 
 
76
                /// <summary>
 
77
                /// Checks that ToString does not return null (not helpful for debugging) and that it does not crash.
 
78
                /// </summary>
 
79
                /// <param name="obj">NSObject instance to validate</param>
 
80
                protected virtual void CheckToString (NSObject obj)
 
81
                {
 
82
                        bool result = obj.ToString () != null;
 
83
                        if (!ContinueOnFailure)
 
84
                                Assert.IsTrue (result, instance_type_name + ".ToString");
 
85
                        else if (!result) {
 
86
                                Console.WriteLine ("[FAIL] {0} : ToString", instance_type_name);
 
87
                                Errors++;
 
88
                        }
 
89
                }
 
90
 
 
91
                /// <summary>
 
92
                /// Dispose the specified NSObject instance. In some cases objects cannot be disposed safely.
 
93
                /// Override this method to keep them alive while the remaining tests execute.
 
94
                /// </summary>
 
95
                /// <param name="obj">NSObject instance to dispose</param>
 
96
                /// <param name="type">Type of the object, to be used if special logic is required.</param>
 
97
                protected virtual void Dispose (NSObject obj, Type type)
 
98
                {
 
99
                        obj.Dispose ();
 
100
                }
 
101
 
 
102
                [Test]
 
103
                public void DefaultCtorAllowed ()
 
104
                {
 
105
                        Errors = 0;
 
106
                        int n = 0;
 
107
                        
 
108
                        foreach (Type t in Assembly.GetTypes ()) {
 
109
                                if (t.IsAbstract || !NSObjectType.IsAssignableFrom (t))
 
110
                                        continue;
 
111
                                
 
112
                                if (Skip (t) || SkipDueToAttribute (t))
 
113
                                        continue;
 
114
                                
 
115
                                var ctor = t.GetConstructor (Type.EmptyTypes);
 
116
                                if ((ctor == null) || ctor.IsAbstract) {
 
117
                                        if (LogUntestedTypes)
 
118
                                                Console.WriteLine ("[WARNING] {0} was skipped because it had no default constructor", t);
 
119
                                        continue;
 
120
                                }
 
121
                                
 
122
                                instance_type_name = t.FullName;
 
123
                                if (LogProgress)
 
124
                                                Console.WriteLine ("{0}. {1}", n, instance_type_name);
 
125
 
 
126
                                NSObject obj = null;
 
127
                                try {
 
128
                                        obj = ctor.Invoke (null) as NSObject;
 
129
                                        CheckHandle (obj);
 
130
                                        CheckToString (obj);
 
131
                                        Dispose (obj, t);
 
132
                                }
 
133
                                catch (TargetInvocationException tie) {
 
134
                                        // Objective-C exception thrown
 
135
                                        if (!ContinueOnFailure)
 
136
                                                throw;
 
137
 
 
138
                                        Console.WriteLine ("[FAIL] {0} : {1}", instance_type_name, tie.InnerException);
 
139
                                        Errors++;
 
140
                                }
 
141
                                n++;
 
142
                        }
 
143
                        Assert.AreEqual (0, Errors, "{0} potential errors found in {1} default ctor validated", Errors, n);
 
144
                }
 
145
        }
 
146
}