From 119b79d28cc2cabe4eb5ce89710382cab5a6997f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yolda=C5=9F=20=C3=87evik?= Date: Sat, 23 Oct 2021 12:54:50 +0300 Subject: [PATCH] Removed sealed keywords from ColumnIndex and ColunmName attributes (#76) * Removed sealed keywords from ColumnIndex and ColunmName attributes * Update nuget version * Encapsulated attribute properties * Unit tests added * Argument validation fix --- src/ExcelColumnIndexAttribute.cs | 28 +++++++++++---- src/ExcelColumnNameAttribute.cs | 34 +++++++++++++------ src/ExcelMapper.csproj | 2 +- .../ExcelColumnIndexAttributeTests.cs | 19 +++++++++++ .../ExcelColumnNameAttributeTests.cs | 26 ++++++++++++++ 5 files changed, 91 insertions(+), 18 deletions(-) diff --git a/src/ExcelColumnIndexAttribute.cs b/src/ExcelColumnIndexAttribute.cs index f89ac47..ddef2a6 100644 --- a/src/ExcelColumnIndexAttribute.cs +++ b/src/ExcelColumnIndexAttribute.cs @@ -10,25 +10,39 @@ namespace ExcelMapper /// Specifies the column index that is used when deserializing a property /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] - public sealed class ExcelColumnIndexAttribute : Attribute + public class ExcelColumnIndexAttribute : Attribute { + private int _index; + /// /// Initializes a new instance of with the specified column index. /// /// The index of the column. 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; } /// /// The index of the column. /// - 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."); + } + } } } diff --git a/src/ExcelColumnNameAttribute.cs b/src/ExcelColumnNameAttribute.cs index a3cb7a8..6e36021 100644 --- a/src/ExcelColumnNameAttribute.cs +++ b/src/ExcelColumnNameAttribute.cs @@ -10,30 +10,44 @@ namespace ExcelMapper /// Specifies the column name that is used when deserializing a property /// [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] - public sealed class ExcelColumnNameAttribute : Attribute + public class ExcelColumnNameAttribute : Attribute { + private string _name; + /// /// Initializes a new instance of with the specified column name. /// /// The name of the column. public ExcelColumnNameAttribute(string name) + { + CheckNameNullOrEmpty(name , nameof(name)); + Name = name; + } + + /// + /// The name of the column. + /// + 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; } - - /// - /// The name of the column. - /// - public string Name { get; } } } diff --git a/src/ExcelMapper.csproj b/src/ExcelMapper.csproj index 60abfb3..52361c9 100644 --- a/src/ExcelMapper.csproj +++ b/src/ExcelMapper.csproj @@ -7,7 +7,7 @@ ExcelDataReader.Mapping - 2.2.1 + 2.2.2 Hugh Bellamy Excel Mapping Helper An extension of ExcelDataReader that supports fluent mapping of rows to C# objects. diff --git a/tests/ExcelMapper/ExcelColumnIndexAttributeTests.cs b/tests/ExcelMapper/ExcelColumnIndexAttributeTests.cs index 89084b2..7608aa4 100644 --- a/tests/ExcelMapper/ExcelColumnIndexAttributeTests.cs +++ b/tests/ExcelMapper/ExcelColumnIndexAttributeTests.cs @@ -19,5 +19,24 @@ public void Ctor_InvalidIndex_ThrowsArgumentOutOfRangeException() { Assert.Throws("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("value", () => attribute.Index = -1); + } } } diff --git a/tests/ExcelMapper/ExcelColumnNameAttributeTests.cs b/tests/ExcelMapper/ExcelColumnNameAttributeTests.cs index 343ff18..62a0f77 100644 --- a/tests/ExcelMapper/ExcelColumnNameAttributeTests.cs +++ b/tests/ExcelMapper/ExcelColumnNameAttributeTests.cs @@ -25,5 +25,31 @@ public void Ctor_EmptyName_ThrowsArgumentNullException() { Assert.Throws("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("value", () => attribute.Name = null); + } + + [Fact] + public void Name_SetEmpty_ThrowsArgumentNullException() + { + var attribute = new ExcelColumnNameAttribute("Name"); + Assert.Throws("value", () => attribute.Name = string.Empty); + } } }