Debian Codegraph @

external/rx/Ix/NET/Tests/Tests.Qbservable.cs

raw
1 // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
2 #if !SILVERLIGHTM7 && !PORTABLE
3
4 using System;
5 using System.Collections.Generic;
6 using System.Linq;
7 using System.Text;
8 using System.Reflection;
9 #if NUNIT
10 using NUnit.Framework;
11 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
12 using TestMethodAttribute = NUnit.Framework.TestAttribute;
13 using TestInitializeAttribute = NUnit.Framework.SetUpAttribute;
14 #else
15 using Microsoft.VisualStudio.TestTools.UnitTesting;
16 #endif
17 using System.Runtime.CompilerServices;
18 using System.Linq.Expressions;
19 using System.ComponentModel;
20
21 namespace Tests
22 {
23 public partial class Tests
24 {
25 [TestMethod]
26 public void Queryable_Enumerable_Parity()
27 {
28 var enu = typeof(EnumerableEx).GetMethods(BindingFlags.Public | BindingFlags.Static).ToList();
29 var qry = typeof(QueryableEx).GetMethods(BindingFlags.Public | BindingFlags.Static).ToList();
30
31 var onlyInObs = enu.Select(m => m.Name).Except(qry.Select(m => m.Name)).Except(new[] { "ForEach", "ToEnumerable", "Multicast", "GetAwaiter", "ToEvent", "ToEventPattern", "ForEachAsync" }).ToList();
32 var onlyInQbs = qry.Select(m => m.Name).Except(enu.Select(m => m.Name)).Except(new[] { "ToQueryable", "get_Provider", "Empty", "Range" }).ToList();
33
34 Assert.IsTrue(onlyInObs.Count == 0, "Missing Queryable operator: " + string.Join(", ", onlyInObs.ToArray()));
35 Assert.IsTrue(onlyInQbs.Count == 0, "Missing Enumerable operator: " + string.Join(", ", onlyInQbs.ToArray()));
36
37 var enus = enu.GroupBy(m => m.Name);
38 var qrys = qry.GroupBy(m => m.Name);
39 var mtch = (from o in enus
40 join q in qrys on o.Key equals q.Key
41 select new { Name = o.Key, Enumerable = o.ToList(), Queryable = q.ToList() })
42 .ToList();
43
44 Func<Type, bool> filterReturn = t =>
45 {
46 if (t.IsGenericType)
47 {
48 var gd = t.GetGenericTypeDefinition();
49 if (gd == typeof(IBuffer<>))
50 return false;
51 }
52 return true;
53 };
54
55 Func<MethodInfo, bool> filterHelper = m =>
56 {
57 return !m.IsDefined(typeof(EditorBrowsableAttribute), false);
58 };
59
60 foreach (var group in mtch)
61 {
62 var oss = group.Enumerable
63 .Where(m => filterReturn(m.ReturnType))
64 .Select(m => GetSignature(m, false))
65 .OrderBy(x => x).ToList();
66
67 var qss = group.Queryable
68 .Where(m => filterHelper(m))
69 .Select(m => GetSignature(m, true))
70 .OrderBy(x => x).ToList();
71
72 if (!group.Name.Equals("Create"))
73 Assert.IsTrue(oss.SequenceEqual(qss), "Mismatch between QueryableEx and EnumerableEx for " + group.Name);
74 }
75 }
76
77 public static string GetSignature(MethodInfo m, bool correct)
78 {
79 var ps = m.GetParameters();
80 var pss = ps.AsEnumerable();
81 if (correct && ps.Length > 0 && ps[0].ParameterType == typeof(IQueryProvider))
82 pss = pss.Skip(1);
83
84 var gens = m.IsGenericMethod ? string.Format("<{0}>", string.Join(", ", m.GetGenericArguments().Select(a => GetTypeName(a, correct)).ToArray())) : "";
85
86 var pars = string.Join(", ", pss.Select(p => (Attribute.IsDefined(p, typeof(ParamArrayAttribute)) ? "params " : "") + GetTypeName(p.ParameterType, correct) + " " + p.Name).ToArray());
87 if (Attribute.IsDefined(m, typeof(ExtensionAttribute)))
88 {
89 if (pars.StartsWith("IQbservable") || pars.StartsWith("IQueryable"))
90 pars = "this " + pars;
91 }
92
93 return string.Format("{0} {1}{2}({3})", GetTypeName(m.ReturnType, correct), m.Name, gens, pars);
94 }
95
96 public static string GetTypeName(Type t, bool correct)
97 {
98 if (t.IsGenericType)
99 {
100 var gtd = t.GetGenericTypeDefinition();
101 if (gtd == typeof(Expression<>))
102 return GetTypeName(t.GetGenericArguments()[0], false);
103
104 var args = string.Join(", ", t.GetGenericArguments().Select(a => GetTypeName(a, false)).ToArray());
105
106 var len = t.Name.IndexOf('`');
107 var name = len >= 0 ? t.Name.Substring(0, len) : t.Name;
108 if (correct && name == "IQbservable")
109 name = "IObservable";
110 if (correct && name == "IQueryable")
111 name = "IEnumerable";
112
113 return string.Format("{0}<{1}>", name, args);
114 }
115
116 if (t.IsArray)
117 {
118 return GetTypeName(t.GetElementType(), correct) + "[]";
119 }
120
121 return t.Name;
122 }
123
124 [TestMethod]
125 public void QueryableRetarget1()
126 {
127 var res = QueryableEx.Provider.Empty<int>().AsEnumerable().ToList();
128 Assert.IsTrue(res.SequenceEqual(new int[0]));
129 }
130
131 [TestMethod]
132 public void QueryableRetarget2()
133 {
134 var res = QueryableEx.Provider.Return(42).AsEnumerable().ToList();
135 Assert.IsTrue(res.SequenceEqual(new[] { 42 }));
136 }
137
138 [TestMethod]
139 public void QueryableRetarget3()
140 {
141 var res = Enumerable.Range(0, 10).AsQueryable().TakeLast(2).AsEnumerable().ToList();
142 Assert.IsTrue(res.SequenceEqual(new[] { 8, 9 }));
143 }
144
145 [TestMethod]
146 public void QueryableRetarget4()
147 {
148 var res = QueryableEx.Provider.Range(0, 10).AsEnumerable().ToList();
149 Assert.IsTrue(res.SequenceEqual(Enumerable.Range(0, 10)));
150 }
151 }
152 }
153
154 #endif