Permalink
Browse files

Use explicit interface implementation in ResolveVisitor to make it cl…

…ear what the public API is supposed to be.

Added support for QueryJoinClause and QueryOrderClause to the resolver.
  • Loading branch information...
dgrunwald committed Aug 27, 2011
1 parent f807e61 commit 5b17740beaa429cfbcb3e3f1ee6190bb4afeac62
@@ -313,5 +313,52 @@ public void QueryWithGroupBy()
}
}});
}
+
+ [Test]
+ public void QueryWithJoin()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "from a in b join c in d on e equals f select g",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "a",
+ Expression = new IdentifierExpression("b")
+ },
+ new QueryJoinClause {
+ JoinIdentifier = "c",
+ InExpression = new IdentifierExpression("d"),
+ OnExpression = new IdentifierExpression("e"),
+ EqualsExpression = new IdentifierExpression("f")
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("g")
+ }
+ }});
+ }
+
+ [Test]
+ public void QueryWithGroupJoin()
+ {
+ ParseUtilCSharp.AssertExpression(
+ "from a in b join c in d on e equals f into g select h",
+ new QueryExpression {
+ Clauses = {
+ new QueryFromClause {
+ Identifier = "a",
+ Expression = new IdentifierExpression("b")
+ },
+ new QueryJoinClause {
+ JoinIdentifier = "c",
+ InExpression = new IdentifierExpression("d"),
+ OnExpression = new IdentifierExpression("e"),
+ EqualsExpression = new IdentifierExpression("f"),
+ IntoIdentifier = "g"
+ },
+ new QuerySelectClause {
+ Expression = new IdentifierExpression("h")
+ }
+ }});
+ }
}
}
@@ -216,10 +216,10 @@ class Program {
static void T(ref int y) {}
}";
- InvocationResolveResult mrr = Resolve<InvocationResolveResult>(program, "T(a)");
+ InvocationResolveResult mrr = Resolve<InvocationResolveResult>(program.Replace("T(a)", "$T(a)$"));
Assert.IsFalse(mrr.Member.Parameters[0].IsRef);
- mrr = Resolve<InvocationResolveResult>(program, "T(ref a)");
+ mrr = Resolve<InvocationResolveResult>(program.Replace("T(ref a)", "$T(ref a)$"));
Assert.IsTrue(mrr.Member.Parameters[0].IsRef);
}
@@ -26,7 +26,7 @@ namespace ICSharpCode.NRefactory.CSharp.Resolver
public class LinqTests : ResolverTestBase
{
[Test]
- public void SimpleLinqTest()
+ public void SimpleLinq()
{
string program = @"using System; using System.Linq;
class TestClass {
@@ -51,7 +51,7 @@ where e.StartsWith(""/"")
}
[Test]
- public void LinqGroupTest()
+ public void Group()
{
string program = @"using System; using System.Linq;
class TestClass {
@@ -71,7 +71,7 @@ class TestClass {
}
[Test]
- public void LinqQueryableGroupTest()
+ public void QueryableGroup()
{
string program = @"using System; using System.Linq;
class TestClass {
@@ -91,7 +91,7 @@ class TestClass {
}
[Test]
- public void ParenthesizedLinqTest()
+ public void Parenthesized()
{
string program = @"using System; using System.Linq;
class TestClass {
@@ -100,13 +100,14 @@ class TestClass {
}
}
";
- ResolveResult rr = Resolve<ResolveResult>(program);
+ var rr = Resolve<InvocationResolveResult>(program);
+ Assert.AreEqual("System.Linq.Enumerable.Select", rr.Member.FullName);
Assert.AreEqual("System.Collections.Generic.IEnumerable", rr.Type.FullName);
Assert.AreEqual("System.Int32", ((ParameterizedType)rr.Type).TypeArguments[0].FullName);
}
[Test]
- public void LinqSelectReturnTypeTest()
+ public void SelectReturnType()
{
string program = @"using System;
class TestClass { static void M() {
@@ -115,12 +116,13 @@ class TestClass { static void M() {
class XYZ {
public int Select<U>(Func<string, U> f) { return 42; }
}";
- ResolveResult rr = Resolve<ResolveResult>(program);
+ var rr = Resolve<InvocationResolveResult>(program);
+ Assert.AreEqual("XYZ.Select", rr.Member.FullName);
Assert.AreEqual("System.Int32", rr.Type.FullName);
}
[Test]
- public void LinqQueryContinuationTest()
+ public void Continuation()
{
string program = @"using System; using System.Linq;
class TestClass {
@@ -147,5 +149,140 @@ class TestClass {
lrr = Resolve<LocalResolveResult>(program.Replace("r.ToString", "$r$.ToString"));
Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.Int32]]", lrr.Type.ReflectionName);
}
+
+ [Test]
+ public void OrderingWithSelectCall()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ orderby x.Length
+ select x + x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void OrderingWithoutSelectCall()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ orderby x.Length
+ select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Linq.IOrderedEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void OrderingWithSelectCallDueToSecondRangeVariable1()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ from y in input
+ orderby x.Length
+ select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void OrderingWithSelectCallDueToSecondRangeVariable2()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ join y in input on x equals y
+ orderby x.Length
+ select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void OrderingWithSelectCallDueToSecondRangeVariable3()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ join y in input on x equals y into g
+ orderby x.Length
+ select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void OrderingWithSelectCallDueToSecondRangeVariable4()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input
+ let y = x
+ orderby x.Length
+ select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test]
+ public void DegenerateQuery()
+ {
+ string program = @"using System; using System.Linq;
+class TestClass {
+ void Test(string[] input) {
+ $var$ r = from x in input select x;
+ }
+}
+";
+ TypeResolveResult rr = Resolve<TypeResolveResult>(program);
+ Assert.AreEqual("System.Collections.Generic.IEnumerable`1[[System.String]]", rr.Type.ReflectionName);
+ }
+
+ [Test, Ignore("lots of bugs here.. not only parser bugs")]
+ public void GroupJoinWithCustomMethod()
+ {
+ string program = @"using System;
+class TestClass { static void M(long [] args) {
+ var q = (from a in new XYZ() join b in args on a equals b into g select g);
+}}
+class XYZ {
+ public XYZ GroupJoin<T, K, R>(IEnumerable<T> f, Func<string, K> key1, Func<T, K> key2, Func<string, decimal, R> s) { return this; }
+ public int Select<U>(Func<string, U> f) { return 42; }
+}";
+ var local = Resolve<LocalResolveResult>(program.Replace("into g", "into $g$"));
+ Assert.AreEqual("System.Decimal", local.Type.FullName);
+
+ local = Resolve<LocalResolveResult>(program.Replace("select g", "select $g$"));
+ Assert.AreEqual("System.Decimal", local.Type.FullName);
+
+ var trr = Resolve<TypeResolveResult>(program.Replace("var", "$var$"));
+ Assert.AreEqual("XYZ", trr.Type.FullName); // because 'Select' is done as part of GroupJoin()
+ }
}
}
@@ -391,11 +391,11 @@ class TestClass {
COL.ArrayList ff;
}
";
- TypeResolveResult type = Resolve<TypeResolveResult>(program, "COL.ArrayList");
+ TypeResolveResult type = Resolve<TypeResolveResult>(program.Replace("COL.ArrayList", "$COL.ArrayList$"));
Assert.IsNotNull(type, "COL.ArrayList should resolve to a type");
Assert.AreEqual("System.Collections.ArrayList", type.Type.FullName, "TypeResolveResult");
- MemberResolveResult member = Resolve<MemberResolveResult>(program, "ff");
+ MemberResolveResult member = Resolve<MemberResolveResult>(program.Replace("ff", "$ff$"));
Assert.AreEqual("System.Collections.ArrayList", member.Type.FullName, "the full type should be resolved");
}
@@ -132,13 +132,13 @@ public C()
{}
}
";
- InvocationResolveResult mrr = Resolve<InvocationResolveResult>(program, "base(b)");
+ InvocationResolveResult mrr = Resolve<InvocationResolveResult>(program.Replace("base(b)", "$base(b)$"));
Assert.AreEqual("A..ctor", mrr.Member.FullName);
- mrr = Resolve<InvocationResolveResult>(program, "base(c)");
+ mrr = Resolve<InvocationResolveResult>(program.Replace("base(c)", "$base(c)$"));
Assert.AreEqual("B..ctor", mrr.Member.FullName);
- mrr = Resolve<InvocationResolveResult>(program, "this(0)");
+ mrr = Resolve<InvocationResolveResult>(program.Replace("this(0)", "$this(0)$"));
Assert.AreEqual("C..ctor", mrr.Member.FullName);
}
@@ -226,11 +226,6 @@ protected ResolveResult Resolve(string code)
return (T)rr;
}
- protected T Resolve<T>(string code, string exprToResolve) where T : ResolveResult
- {
- return Resolve<T>(code.Replace(exprToResolve, "$" + exprToResolve + "$"));
- }
-
sealed class FindNodeVisitor : DepthFirstAstVisitor<object, object>
{
readonly AstLocation start;
@@ -312,6 +312,10 @@ public class QueryJoinClause : QueryClause
}
}
+ public Identifier IntoIdentifierToken {
+ get { return GetChildByRole(IntoIdentifierRole); }
+ }
+
public override S AcceptVisitor<T, S> (IAstVisitor<T, S> visitor, T data)
{
return visitor.VisitQueryJoinClause (this, data);
@@ -2889,7 +2889,7 @@ public override object Visit (Mono.CSharp.Linq.Join join)
var location = LocationsBag.GetLocations (join);
result.AddChild (new CSharpTokenNode (Convert (join.Location), "join".Length), QueryJoinClause.JoinKeywordRole);
- result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), Identifier.Roles.Identifier);
+ result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), "in".Length), QueryJoinClause.InKeywordRole);
@@ -2913,7 +2913,7 @@ public override object Visit (Mono.CSharp.Linq.GroupJoin join)
var location = LocationsBag.GetLocations (join);
result.AddChild (new CSharpTokenNode (Convert (join.Location), "join".Length), QueryJoinClause.JoinKeywordRole);
- result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), Identifier.Roles.Identifier);
+ result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.JoinIdentifierRole);
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[0]), "in".Length), QueryJoinClause.InKeywordRole);
@@ -2932,7 +2932,7 @@ public override object Visit (Mono.CSharp.Linq.GroupJoin join)
if (location != null)
result.AddChild (new CSharpTokenNode (Convert (location[3]), "into".Length), QueryJoinClause.IntoKeywordRole);
- result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), Identifier.Roles.Identifier);
+ result.AddChild (Identifier.Create (join.JoinVariable.Name, Convert (join.JoinVariable.Location)), QueryJoinClause.IntoIdentifierRole);
return result;
}
Oops, something went wrong.

0 comments on commit 5b17740

Please sign in to comment.