Skip to content

Commit

Permalink
Removed sealed keywords from ColumnIndex and ColunmName attributes (#76)
Browse files Browse the repository at this point in the history
* Removed sealed keywords from ColumnIndex and ColunmName attributes

* Update nuget version

* Encapsulated attribute properties

* Unit tests added

* Argument validation fix
  • Loading branch information
yoldascevik authored Oct 23, 2021
1 parent e64557e commit 119b79d
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
28 changes: 21 additions & 7 deletions src/ExcelColumnIndexAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,39 @@ namespace ExcelMapper
/// Specifies the column index that is used when deserializing a property
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class ExcelColumnIndexAttribute : Attribute
public class ExcelColumnIndexAttribute : Attribute
{
private int _index;

/// <summary>
/// Initializes a new instance of <see cref="ExcelColumnIndexAttribute"/> with the specified column index.
/// </summary>
/// <param name="index">The index of the column.</param>
public ExcelColumnIndexAttribute(int index)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(nameof(index), index, $"Column index {index} must be greater or equal to zero.");
}

CheckIndexGreaterOrEqualToZero(index, nameof(index));
Index = index;
}

/// <summary>
/// The index of the column.
/// </summary>
public int Index { get; }
public int Index
{
get => _index;
set
{
CheckIndexGreaterOrEqualToZero(value, nameof(value));
_index = value;
}
}

private void CheckIndexGreaterOrEqualToZero(int index, string paramName)
{
if (index < 0)
{
throw new ArgumentOutOfRangeException(paramName, index, $"Column index {index} must be greater or equal to zero.");
}
}
}
}
34 changes: 24 additions & 10 deletions src/ExcelColumnNameAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,44 @@ namespace ExcelMapper
/// Specifies the column name that is used when deserializing a property
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public sealed class ExcelColumnNameAttribute : Attribute
public class ExcelColumnNameAttribute : Attribute
{
private string _name;

/// <summary>
/// Initializes a new instance of <see cref="ExcelColumnNameAttribute"/> with the specified column name.
/// </summary>
/// <param name="name">The name of the column.</param>
public ExcelColumnNameAttribute(string name)
{
CheckNameNullOrEmpty(name , nameof(name));
Name = name;
}

/// <summary>
/// The name of the column.
/// </summary>
public string Name
{
get => _name;
set
{
CheckNameNullOrEmpty(value , nameof(value));
_name = value;
}
}

private void CheckNameNullOrEmpty(string name, string paramName)
{
if (name == null)
{
throw new ArgumentNullException(nameof(name));
throw new ArgumentNullException(paramName);
}

if (name.Length == 0)
{
throw new ArgumentException("Column name cannot be empty.", nameof(name));
throw new ArgumentException("Column name cannot be empty.", paramName);
}

Name = name;
}

/// <summary>
/// The name of the column.
/// </summary>
public string Name { get; }
}
}
2 changes: 1 addition & 1 deletion src/ExcelMapper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<PropertyGroup>
<PackageId>ExcelDataReader.Mapping</PackageId>
<PackageVersion>2.2.1</PackageVersion>
<PackageVersion>2.2.2</PackageVersion>
<Authors>Hugh Bellamy</Authors>
<Title>Excel Mapping Helper</Title>
<Description>An extension of ExcelDataReader that supports fluent mapping of rows to C# objects.</Description>
Expand Down
19 changes: 19 additions & 0 deletions tests/ExcelMapper/ExcelColumnIndexAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,24 @@ public void Ctor_InvalidIndex_ThrowsArgumentOutOfRangeException()
{
Assert.Throws<ArgumentOutOfRangeException>("index", () => new ExcelColumnIndexAttribute(-1));
}

[Theory]
[InlineData(0)]
[InlineData(1)]
public void Index_Set_GetReturnsExpectedInt(int index)
{
var attribute = new ExcelColumnIndexAttribute(10)
{
Index = index
};
Assert.Equal(index, attribute.Index);
}

[Fact]
public void Index_Set_GetInvalidIndex_ThrowsArgumentOutOfRangeException()
{
var attribute = new ExcelColumnIndexAttribute(1);
Assert.Throws<ArgumentOutOfRangeException>("value", () => attribute.Index = -1);
}
}
}
26 changes: 26 additions & 0 deletions tests/ExcelMapper/ExcelColumnNameAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,31 @@ public void Ctor_EmptyName_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentException>("name", () => new ExcelColumnNameAttribute(string.Empty));
}

[Theory]
[InlineData("columnname")]
[InlineData("ColumnName")]
public void Name_Set_GetReturnsExpected(string value)
{
var attribute = new ExcelColumnNameAttribute("Name")
{
Name = value
};
Assert.Equal(value, attribute.Name);
}

[Fact]
public void Name_SetNull_ThrowsArgumentNullException()
{
var attribute = new ExcelColumnNameAttribute("Name");
Assert.Throws<ArgumentNullException>("value", () => attribute.Name = null);
}

[Fact]
public void Name_SetEmpty_ThrowsArgumentNullException()
{
var attribute = new ExcelColumnNameAttribute("Name");
Assert.Throws<ArgumentException>("value", () => attribute.Name = string.Empty);
}
}
}

0 comments on commit 119b79d

Please # to comment.