Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added an alternative way to retrieve the DescriptionAttribute utilizi… #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
using System.ComponentModel;
using ProductivityTools.DescriptionValue;
using FluentAssertions;

namespace ProductivityTools.DescriptionValue.Tests
{

[TestClass]
public class DescriptionAttributeExtensionsTests
{

public class TestClass
{
[System.ComponentModel.Description("Property1 Description")]
public string Property1 { get; set; }

public int Property2 { get; set; }
}

[TestMethod]
public void GetPropertyDescription_ShouldReturnDescription_WhenDescriptionAttributeExists()
{
// Arrange
var myClass = new TestClass();

// Act
string description = myClass.GetPropertyDescription(x => x.Property1);

// Assert
description.Should().Be("Property1 Description");
}

[TestMethod]
public void GetPropertyDescription_ShouldReturnEmptyString_WhenDescriptionAttributeDoesNotExist()
{
// Arrange
var myClass = new TestClass();

// Act
string description = myClass.GetPropertyDescription(x => x.Property2);

// Assert
description.Should().BeEmpty();
}

[TestMethod]
public void DescriptionExists_ShouldReturnTrue_WhenDescriptionAttributeExists()
{
// Arrange
var myClass = new TestClass();

// Act
bool exists = myClass.PropertyDescriptionExists(x => x.Property1);

// Assert
exists.Should().BeTrue();
}

[TestMethod]
public void DescriptionExists_ShouldReturnFalse_WhenDescriptionAttributeDoesNotExist()
{
// Arrange
var myClass = new TestClass();

// Act
bool exists = myClass.PropertyDescriptionExists(x => x.Property2);

// Assert
exists.Should().BeFalse();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;

namespace ProductivityTools.DescriptionValue
{
public static class DescriptionAttributeExtensions
{

/// <summary>
/// Gets the description specified by the DescriptionAttribute of a property.
/// </summary>
/// <typeparam name="TObject">The type containing the property.</typeparam>
/// <typeparam name="TProperty">The type of the property for which to retrieve the description.</typeparam>
/// <param name="obj">The instance of the containing class.</param>
/// <param name="propertyExpression">An expression that identifies the property for which to retrieve the description.</param>
/// <returns>
/// The description specified by the DescriptionAttribute of the property,
/// or string.Empty if the attribute is not found or does not have a Description property.
/// </returns>
public static string GetPropertyDescription<TObject, TProperty>(this TObject obj, Expression<Func<TObject, TProperty>> propertyExpression) where TObject : class
{
if (propertyExpression == null)
{
throw new ArgumentNullException(nameof(propertyExpression));
}

PropertyInfo propertyInfo = GetPropertyInfoFromExpression(propertyExpression);

DescriptionAttribute descriptionAttribute = GetDescriptionAttribute(propertyInfo);

return descriptionAttribute != null ? descriptionAttribute.Description : string.Empty;
}

/// <summary>
/// Checks if a DescriptionAttribute exists for a property.
/// </summary>
/// <typeparam name="TObject">The type containing the property.</typeparam>
/// <typeparam name="TProperty">The type of the property for which to check the DescriptionAttribute.</typeparam>
/// <param name="obj">The instance of the containing class.</param>
/// <param name="propertyExpression">An expression that identifies the property for which to check the DescriptionAttribute.</param>
/// <returns>
/// True if the DescriptionAttribute exists for the property; otherwise, false.
/// </returns>
public static bool PropertyDescriptionExists<TObject, TProperty>(this TObject obj, Expression<Func<TObject, TProperty>> propertyExpression) where TObject : class
{
if (propertyExpression == null)
{
throw new ArgumentNullException(nameof(propertyExpression));
}

PropertyInfo propertyInfo = GetPropertyInfoFromExpression(propertyExpression);

return GetDescriptionAttribute(propertyInfo) != null;
}

private static PropertyInfo GetPropertyInfoFromExpression<TObject, TProperty>(Expression<Func<TObject, TProperty>> expression)
{
MemberExpression memberExpression = GetMemberExpression(expression) ?? throw new ArgumentException("Property expression must be a MemberExpression.", nameof(expression));

PropertyInfo propertyInfo = memberExpression.Member as PropertyInfo ?? throw new ArgumentException("Property expression must represent a property.", nameof(expression));

return propertyInfo;
}

private static MemberExpression GetMemberExpression<TObject, TProperty>(Expression<Func<TObject, TProperty>> expression)
{
if (expression.Body is MemberExpression memberExpression)
{
return memberExpression;
}

if (expression.Body is UnaryExpression unaryExpression && unaryExpression.Operand is MemberExpression operandExpression)
{
return operandExpression;
}

return null;
}

private static DescriptionAttribute GetDescriptionAttribute(PropertyInfo propertyInfo) => propertyInfo?.GetCustomAttribute<DescriptionAttribute>();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
</ItemGroup>

</Project>