Updating ReflectOn.NameOf to support indexer calls

This is so that we can write stuff like this in aspx pages:
name="<%=Html.NameOf(m => m.PageEntries[pageIndex].PageId)%>"

--HG--
extra : convert_revision : svn%3A5ff7c347-ad56-4c35-b696-ccb81de16e03/trunk%4038939
This commit is contained in:
rpaquay
2009-11-08 06:23:03 +00:00
parent 72851737cc
commit ace78cfd33
8 changed files with 239 additions and 27 deletions

View File

@@ -130,6 +130,8 @@
<Compile Include="Records\Foo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Stubs\StubHttpContext.cs" />
<Compile Include="Utility\ReflectOnTests.cs" />
<Compile Include="Utility\ReflectTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Orchard\Orchard.csproj">

View File

@@ -0,0 +1,74 @@
using NUnit.Framework;
using Orchard.Utility;
namespace Orchard.Tests.Utility {
[TestFixture]
public class ReflectOnTests {
#region Setup/Teardown
[TestFixtureSetUp]
public void InitFixture() {
}
[TestFixtureTearDown]
public void TermFixture() {
}
#endregion
private class TestClass {
public int MyField;
public int MyField2;
public int MyProperty { get { MyField = 5; return MyField; } }
public int MyProperty2 { get { MyField2 = 5; return MyField2; } }
public void MyMethod(int i) { }
public int MyMethod(string s) { return 5; }
public void MyMethod2(int i) { }
public int MyMethod2(string s) { return 5; }
public TestClass MyTestClass { get { return null; } }
public TestClass this[int i] { get { return null; } }
}
[Test]
public void ReflectOnGetMemberShouldReturnCorrectMemberInfo() {
Assert.That(ReflectOn<TestClass>.GetMember(p => p.MyField).Name, Is.EqualTo("MyField"));
Assert.That(ReflectOn<TestClass>.GetMember(p => p.MyMethod(5)).Name, Is.EqualTo("MyMethod"));
}
[Test]
public void ReflectOnShouldWorkOnFields() {
Assert.That(ReflectOn<TestClass>.GetField(p => p.MyField).Name, Is.EqualTo("MyField"));
Assert.That(ReflectOn<TestClass>.GetField(p => p.MyField2).Name, Is.EqualTo("MyField2"));
}
[Test]
public void ReflectOnShouldWorkOnProperties() {
Assert.That(ReflectOn<TestClass>.GetProperty(p => p.MyProperty).Name, Is.EqualTo("MyProperty"));
Assert.That(ReflectOn<TestClass>.GetProperty(p => p.MyProperty2).Name, Is.EqualTo("MyProperty2"));
}
[Test]
public void ReflectOnShouldWorkOnMethods() {
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod(5)).Name, Is.EqualTo("MyMethod"));
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod("")).Name, Is.EqualTo("MyMethod"));
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod("")).ReturnType, Is.EqualTo(typeof(int)));
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod2(5)).Name, Is.EqualTo("MyMethod2"));
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod2("")).Name, Is.EqualTo("MyMethod2"));
Assert.That(ReflectOn<TestClass>.GetMethod(p => p.MyMethod2("")).ReturnType, Is.EqualTo(typeof(int)));
}
[Test]
public void ReflectOnShouldWorkOnDottedProperties() {
Assert.That(ReflectOn<TestClass>.NameOf(p => p.MyTestClass.MyTestClass.MyProperty), Is.EqualTo("MyTestClass.MyTestClass.MyProperty"));
}
[Test]
public void ReflectOnShouldWorkOnIndexers() {
Assert.That(ReflectOn<TestClass>.NameOf(p => p[0].MyTestClass[1].MyProperty), Is.EqualTo("[0].MyTestClass[1].MyProperty"));
int j = 5;
int index = j;
Assert.That(ReflectOn<TestClass>.NameOf(p => p.MyTestClass[index].MyProperty), Is.EqualTo("MyTestClass[5].MyProperty"));
}
}
}

View File

@@ -0,0 +1,47 @@
using NUnit.Framework;
using Orchard.Utility;
namespace Orchard.Tests.Utility {
[TestFixture]
public class ReflectTests {
private class TestClass {
public static int MyField;
public static int MyField2;
public static int MyProperty { get { MyField = 5; return MyField; } }
public static int MyProperty2 { get { MyField2 = 5; return MyField2; } }
public static void MyMethod(int i) { }
public static int MyMethod(string s) { return 5; }
public static void MyMethod2(int i) { }
public static int MyMethod2(string s) { return 5; }
}
[Test]
public void ReflectGetMemberShouldReturnCorrectMemberInfo() {
Assert.That(Reflect.GetMember(() => TestClass.MyField).Name, Is.EqualTo("MyField"));
Assert.That(Reflect.GetMember(() => TestClass.MyMethod(5)).Name, Is.EqualTo("MyMethod"));
}
[Test]
public void ReflectShouldWorkOnFields() {
Assert.That(Reflect.GetField(() => TestClass.MyField).Name, Is.EqualTo("MyField"));
Assert.That(Reflect.GetField(() => TestClass.MyField2).Name, Is.EqualTo("MyField2"));
}
[Test]
public void ReflectShouldWorkOnProperties() {
Assert.That(Reflect.GetProperty(() => TestClass.MyProperty).Name, Is.EqualTo("MyProperty"));
Assert.That(Reflect.GetProperty(() => TestClass.MyProperty2).Name, Is.EqualTo("MyProperty2"));
}
[Test]
public void ReflectShouldWorkOnMethods() {
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod(5)).Name, Is.EqualTo("MyMethod"));
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod("")).Name, Is.EqualTo("MyMethod"));
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod("")).ReturnType, Is.EqualTo(typeof(int)));
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod2(5)).Name, Is.EqualTo("MyMethod2"));
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod2("")).Name, Is.EqualTo("MyMethod2"));
Assert.That(Reflect.GetMethod(() => TestClass.MyMethod2("")).ReturnType, Is.EqualTo(typeof(int)));
}
}
}