Skip to content

Commit

Permalink
import CodeQuery project files instead of separate library
Browse files Browse the repository at this point in the history
  • Loading branch information
handcraftsman committed Apr 7, 2010
1 parent 8172456 commit 364711a
Show file tree
Hide file tree
Showing 11 changed files with 500 additions and 4 deletions.
Binary file removed lib/CodeQuery/CodeQuery.dll
Binary file not shown.
Binary file removed lib/CodeQuery/CodeQuery.pdb
Binary file not shown.
35 changes: 35 additions & 0 deletions src/MvbaCore/CodeQuery/FieldInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using JetBrains.Annotations;

namespace CodeQuery
{
public static class FieldInfoExtensions
{
[NotNull]
public static IEnumerable<FieldInfo> ThatAreStatic([NotNull] this IEnumerable<FieldInfo> items)
{
return items.Where(x => x.IsStatic);
}

[NotNull]
public static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this FieldInfo input) where T : Attribute
{
return ((MemberInfo)input).CustomAttributesOfType<T>();
}

public static bool HasAttributeOfType<TAttributeType>([NotNull] this FieldInfo input) where TAttributeType : Attribute
{
return input.CustomAttributesOfType<TAttributeType>().Any();
}

[NotNull]
public static IEnumerable<FieldInfo> WithAttributeOfType<TAttributeType>([NotNull] this IEnumerable<FieldInfo> input) where TAttributeType : Attribute
{
return input.Where(x => x.HasAttributeOfType<TAttributeType>());
}
}
}
18 changes: 18 additions & 0 deletions src/MvbaCore/CodeQuery/MemberInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using JetBrains.Annotations;

namespace CodeQuery
{
internal static class MemberInfoExtensions
{
[NotNull]
internal static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this MemberInfo input) where T : Attribute
{
return input.GetCustomAttributes(typeof(T), true).Cast<T>();
}
}
}
47 changes: 47 additions & 0 deletions src/MvbaCore/CodeQuery/PropertyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using JetBrains.Annotations;

namespace CodeQuery
{
public static class PropertyInfoExtensions
{
[NotNull]
public static IEnumerable<Attribute> CustomAttributes([NotNull] this PropertyInfo input)
{
return input.GetCustomAttributes(true).Cast<Attribute>();
}

[NotNull]
public static IEnumerable<T> CustomAttributesOfType<T>([NotNull] this PropertyInfo input) where T : Attribute
{
return ((MemberInfo)input).CustomAttributesOfType<T>();
}

public static bool HasAttributeOfType<TAttributeType>([NotNull] this PropertyInfo input) where TAttributeType : Attribute
{
return input.CustomAttributesOfType<TAttributeType>().Any();
}

[NotNull]
public static IEnumerable<PropertyInfo> ThatHaveAGetter([NotNull] this IEnumerable<PropertyInfo> input)
{
return input.Where(x => x.CanRead);
}

[NotNull]
public static IEnumerable<PropertyInfo> ThatHaveASetter([NotNull] this IEnumerable<PropertyInfo> input)
{
return input.Where(x => x.CanWrite);
}

[NotNull]
public static IEnumerable<PropertyInfo> WithAttributeOfType<TAttributeType>([NotNull] this IEnumerable<PropertyInfo> input) where TAttributeType : Attribute
{
return input.Where(x => x.HasAttributeOfType<TAttributeType>());
}
}
}
29 changes: 29 additions & 0 deletions src/MvbaCore/CodeQuery/TypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace CodeQuery
{
public static class TypeExtensions
{
public static bool IsGenericAssignableFrom(this Type target, Type source)
{
if (!target.IsGenericType)
{
return false;
}

if (target.IsAssignableFrom(source))
{
return true;
}

if (!typeof(Nullable<>).IsAssignableFrom(target))
{
return false;
}

var genericParameters = target.GetGenericArguments();

return genericParameters[0].IsAssignableFrom(source);
}
}
}
8 changes: 4 additions & 4 deletions src/MvbaCore/MvbaCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="CodeQuery, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\CodeQuery\CodeQuery.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Annotations, Version=4.5.1230.4, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\JetBrains.Annotations\JetBrains.Annotations.dll</HintPath>
Expand All @@ -53,6 +49,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="CodeQuery\FieldInfoExtensions.cs" />
<Compile Include="CodeQuery\MemberInfoExtensions.cs" />
<Compile Include="CodeQuery\PropertyInfoExtensions.cs" />
<Compile Include="CodeQuery\TypeExtensions.cs" />
<Compile Include="DefaultKeyAttribute.cs" />
<Compile Include="Extensions\IEnumerableTExtensions.cs" />
<Compile Include="Extensions\NamedConstantExtensions.cs" />
Expand Down
110 changes: 110 additions & 0 deletions src/MvbaCoreTests/CodeQuery/FieldInfoExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System.ComponentModel;
using System.Linq;

using CodeQuery;

using FluentAssert;

using NUnit.Framework;

namespace CodeQueryTests
{
public class FieldInfoExtensionsTests
{
[TestFixture]
public class When_asked_for_the_static_FieldInfos
{
[Test]
public void Should_return_only_the_static_ones()
{
var fields = typeof(TestClass).GetFields();
fields.Length.ShouldBeEqualTo(2);
var fieldInfos = fields.ThatAreStatic();
fieldInfos.Count().ShouldBeEqualTo(1);
}

public class TestClass
{
public static int Foo = 7;
public int Bar = 6;
}
}

[TestFixture]
public class When_asked_if_there_are_any_custom_Attributes_of_a_specific_type_for_a_FieldInfo
{
[Test]
public void Should_return_False_if_there_are_no_matching_attributes()
{
var hasAttributeOfType = typeof(TestClass).GetField("Id").HasAttributeOfType<TestAttribute>();
hasAttributeOfType.ShouldBeFalse();
}

[Test]
public void Should_return_True_if_there_are_matching_attributes()
{
var hasAttributeOfType = typeof(TestClass).GetField("Id").HasAttributeOfType<ReadOnlyAttribute>();
hasAttributeOfType.ShouldBeTrue();
}

public class TestClass
{
[ReadOnly(true)]
public int Id;
}
}

[TestFixture]
public class When_asked_to_get_custom_Attributes_of_a_specific_type_for_a_FieldInfo
{
[Test]
public void Should_get_the_matching_Attributes()
{
var attributes = typeof(TestClass).GetField("Id").CustomAttributesOfType<ReadOnlyAttribute>();
attributes.Count().ShouldBeEqualTo(1);
}

[Test]
public void Should_return_an_empty_container_if_there_are_no_matching_Attributes()
{
var attributes = typeof(TestClass).GetField("Id").CustomAttributesOfType<TestAttribute>();
attributes.ShouldNotBeNull();
attributes.Count().ShouldBeEqualTo(0);
}

public class TestClass
{
[ReadOnly(true)]
public int Id;
}
}

[TestFixture]
public class When_asked_to_get_FieldInfos_that_have_custom_Attributes_of_a_specific_type
{
[Test]
public void Should_get_an_empty_container_if_there_are_no_matching_Fields()
{
var fieldInfos = typeof(TestClass).GetFields().WithAttributeOfType<TestAttribute>();
fieldInfos.ShouldNotBeNull();
fieldInfos.Count().ShouldBeEqualTo(0);
}

[Test]
public void Should_get_the_matching_FieldInfos()
{
var fieldInfos = typeof(TestClass).GetFields().WithAttributeOfType<ReadOnlyAttribute>();
fieldInfos.Count().ShouldBeEqualTo(1);
fieldInfos.First().Name.ShouldBeEqualTo("Id");
}

public class TestClass
{
[ReadOnly(true)]
public int Id;

public string Name;
}
}
}
}
Loading

0 comments on commit 364711a

Please sign in to comment.