update MEF to preview 9
[mcs.git] / class / System.ComponentModel.Composition / Tests / ComponentModelUnitTest / System / ComponentModel / Composition / Expectations.cs
blob7dae8376ecfbe962ae5fd7255f81b6e63a8a2f0a
1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
4 using System;
5 using System.Linq;
6 using System.UnitTesting;
7 using System.Collections.Generic;
8 using Microsoft.Internal.Collections;
9 using System.Reflection;
10 using System.Globalization;
11 using System.Collections.ObjectModel;
12 using System.Collections;
13 using System.ComponentModel.Composition.Factories;
14 using System.ComponentModel.Composition.Hosting;
15 using System.ComponentModel.Composition.Primitives;
16 using System.ComponentModel.Composition.AttributedModel;
17 using System.ComponentModel.Composition.ReflectionModel;
18 using System.Reflection.Emit;
20 namespace System.ComponentModel.Composition
22 internal static class Expectations
24 public static IEnumerable<CultureInfo> GetCulturesForFormatting()
26 yield return new CultureInfo("en-US"); // English (US)
27 yield return new CultureInfo("en-AU"); // English (Australian)
28 yield return new CultureInfo("en-CA"); // English (Canada)
29 yield return new CultureInfo("en-NZ"); // English (Great Britain)
30 yield return new CultureInfo("en-JM"); // English (Jamaica)
31 yield return new CultureInfo("pt-BR"); // Portuguese (Brazil)
32 yield return new CultureInfo("es-AR"); // Spanish (Argentina)
33 yield return new CultureInfo("ja-JP"); // Japanese (Japan)
34 yield return new CultureInfo("fr-FR"); // French (France)
35 yield return new CultureInfo("it-IT"); // Italian (Italy)
36 yield return new CultureInfo("de-DE"); // German (Germany)
37 yield return new CultureInfo("es-ES"); // Spanish (Spain)
38 yield return new CultureInfo("ko-KR"); // Korean (Korea)
39 yield return new CultureInfo("zh-TW"); // Chinese (Taiwan)
40 yield return new CultureInfo("zh-CN"); // Chinese (People's Republic of China)
43 public static IEnumerable<Assembly> GetAssemblies()
45 yield return Assembly.GetExecutingAssembly();
46 yield return Assembly.Load("mscorlib");
49 public static IEnumerable<string> GetDisplayNames()
51 yield return "";
52 yield return " ";
53 yield return " ";
54 yield return "DisplayName";
55 yield return "displayname";
56 yield return "This is the display name.";
59 public static IEnumerable<ICompositionElement> GetCompositionElements()
61 yield return ElementFactory.CreateChain(1);
62 yield return ElementFactory.CreateChain(2);
63 yield return ElementFactory.CreateChain(3);
64 yield return ElementFactory.CreateChain(5);
65 yield return ElementFactory.CreateChain(10);
68 public static IEnumerable<ICompositionElement> GetCompositionElementsWithNull()
70 foreach (var element in GetCompositionElements())
72 yield return element;
75 yield return null;
78 public static IEnumerable<ComposablePartCatalog> GetCatalogs()
80 yield return CatalogFactory.Create();
81 yield return CatalogFactory.CreateDefaultAttributed();
84 public static IEnumerable<IEnumerable<CompositionError>> GetCompositionErrors()
86 foreach (var value in GetEmptyCollections<CompositionError>())
88 yield return value;
91 yield return new CompositionError[] { new CompositionError("") };
92 yield return new CompositionError[] { new CompositionError(""), new CompositionError("Description") };
93 yield return new CompositionError[] { new CompositionError(""), new CompositionError("Description"), ErrorFactory.Create(CompositionErrorId.InvalidExportMetadata, "Description", (Exception)null), ErrorFactory.Create(CompositionErrorId.Unknown, "Description", new Exception()) };
96 public static IEnumerable<string> GetContractNames()
98 yield return " ";
99 yield return " ";
100 yield return "ContractName";
101 yield return "contractName";
102 yield return "{ContractName}";
103 yield return "{ContractName}Name";
104 yield return "System.Windows.Forms.Control";
105 yield return "{System.Windows.Forms}Control";
106 yield return "{9}Control";
109 public static IEnumerable<string> GetContractNamesWithEmpty()
111 foreach (string contractName in GetContractNames())
113 yield return contractName;
116 yield return string.Empty;
119 public static IEnumerable<object> GetObjectsReferenceTypes()
121 yield return "Value";
122 yield return new Collection<string>();
123 yield return new IEnumerable[0];
126 public static IEnumerable<ValueType> GetObjectsValueTypes()
128 yield return 10;
129 yield return 10.0;
130 yield return DayOfWeek.Wednesday;
133 public static IEnumerable<string> GetMetadataNames()
135 return GetContractNamesWithEmpty();
138 public static IEnumerable<object> GetMetadataValues()
140 yield return null;
141 yield return string.Empty;
142 yield return "";
143 yield return " ";
144 yield return " ";
145 yield return (Int32)1;
146 yield return (Byte)1;
147 yield return (Single)1.1;
148 yield return (Double)1.1;
149 yield return DayOfWeek.Wednesday;
152 public static IEnumerable<Dictionary<string, Type>> GetRequiredMetadata()
154 yield return new Dictionary<string, Type> { { "", typeof(object) } };
155 yield return new Dictionary<string, Type> { { " ", typeof(object) } };
156 yield return new Dictionary<string, Type> { { " ", typeof(object) } };
157 yield return new Dictionary<string, Type> { { " ", typeof(object) } };
158 yield return new Dictionary<string, Type> { { "A", typeof(object) } };
159 yield return new Dictionary<string, Type> { { "A", typeof(object) }, { "B", typeof(object) } };
160 yield return new Dictionary<string, Type> { { "A", typeof(object) }, { "B", typeof(object) }, { "C", typeof(object) } };
161 yield return new Dictionary<string, Type> { { "a", typeof(object) } };
162 yield return new Dictionary<string, Type> { { "a", typeof(object) }, { "b", typeof(object) } };
163 yield return new Dictionary<string, Type> { { "a", typeof(object) }, { "b", typeof(object) }, { "c", typeof(object) } };
164 yield return new Dictionary<string, Type> { { "Metadata1", typeof(object) }, { "Metadata2", typeof(object) }, { "Metadata3", typeof(object) } };
167 public static IEnumerable<Dictionary<string, Type>> GetRequiredMetadataWithEmpty()
169 foreach (var requiredMetadata in GetRequiredMetadata())
171 yield return requiredMetadata;
174 yield return new Dictionary<string, Type>();
177 public static IEnumerable<IDictionary<string, object>> GetMetadata()
179 yield return new ReadOnlyDictionary<string, object>(null);
180 yield return new Dictionary<string, object>();
182 #if !SILVERLIGHT
184 yield return new SortedDictionary<string, object>();
185 yield return new SortedList<string, object>();
187 #endif
188 var metadata = new Dictionary<string, object>();
189 metadata.Add("One", "Value");
190 metadata.Add("Two", true);
191 metadata.Add("Three", 10);
192 metadata.Add("Four", 1.0);
193 metadata.Add("Five", null);
195 yield return metadata;
198 public static IEnumerable<string> GetExceptionMessages()
200 yield return "";
201 yield return " ";
202 yield return " ";
203 yield return "message";
204 yield return "This is an error message.";
205 yield return "Line One." + Environment.NewLine + "Line Two.";
208 public static IEnumerable<Exception> GetInnerExceptions()
210 yield return new Exception();
211 yield return new ArgumentException();
212 yield return new SystemException();
213 #if CLR40
214 yield return new CompositionException();
215 yield return new ImportCardinalityMismatchException();
216 #endif
219 public static IEnumerable<Exception> GetInnerExceptionsWithNull()
221 foreach (var exception in GetInnerExceptions())
223 yield return exception;
226 yield return null;
229 public static IEnumerable<bool> GetBooleans()
231 yield return false;
232 yield return true;
233 yield return false;
234 yield return true;
237 public static IEnumerable<ReflectionComposablePartDefinition> GetAttributedDefinitions()
239 foreach (var type in GetAttributedTypes())
241 yield return PartDefinitionFactory.CreateAttributed(type);
245 public static IEnumerable<Type> GetTypes()
247 yield return typeof(void);
248 yield return typeof(Type);
249 yield return typeof(double);
250 yield return typeof(string);
251 yield return typeof(int);
252 yield return typeof(CompositionServices);
253 yield return typeof(ICompositionService);
256 public static IEnumerable<Type> GetAttributedTypes()
258 foreach (Type type in typeof(Expectations).Assembly.GetTypes())
260 var definition = AttributedModelDiscovery.CreatePartDefinitionIfDiscoverable(type, (ICompositionElement)null);
261 if (definition != null)
263 yield return type;
269 public static IEnumerable<MemberInfo> GetMembers()
271 yield return typeof(String).GetSingleMember("Length");
272 yield return typeof(Int32).GetSingleMember("MaxValue");
275 public static IEnumerable<TEnum> GetEnumValues<TEnum>() where TEnum : struct
277 var values = TestServices.GetEnumValues<TEnum>();
279 foreach (TEnum value in values)
281 yield return value;
285 public static IEnumerable<TEnum> GetInvalidEnumValues<TEnum>() where TEnum : struct
287 var bounds = GetEnumBounds<TEnum>();
289 yield return AddEnum(bounds.Item1, -3);
290 yield return AddEnum(bounds.Item1, -2);
291 yield return AddEnum(bounds.Item1, -1);
292 yield return AddEnum(bounds.Item2, 1);
293 yield return AddEnum(bounds.Item2, 2);
294 yield return AddEnum(bounds.Item2, 3);
295 yield return (TEnum)(object)(int.MinValue + 1);
296 yield return (TEnum)(object)int.MinValue;
297 yield return (TEnum)(object)int.MaxValue;
298 yield return (TEnum)(object)(int.MaxValue - 1);
301 public static IEnumerable<object[]> GetObjectArraysWithNull()
303 yield return null;
304 yield return new object[0];
305 yield return new object[] { null };
306 yield return new string[] { null };
307 yield return new object[] { 1, "2", 3.0 };
308 yield return new string[] { "1" };
309 yield return new string[] { "1", "2" };
310 yield return new string[] { "1", "2", "3" };
313 private static TEnum AddEnum<TEnum>(TEnum left, int right) where TEnum : struct
315 int intRight = (int)(object)left;
317 return (TEnum)(object)(intRight + right);
320 private static Tuple<TEnum, TEnum> GetEnumBounds<TEnum>() where TEnum : struct
322 var values = TestServices.GetEnumValues<TEnum>();
324 return new Tuple<TEnum, TEnum>(values.First(), values.Last());
327 private static IEnumerable<IEnumerable<T>> GetEmptyCollections<T>()
329 yield return new T[0];
330 yield return Enumerable.Empty<T>();
331 yield return new List<T>();
332 yield return new Collection<T>();
333 yield return new Dictionary<T, object>().Keys;