Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions CaPPMS/Attributes/CappmsStringLengthAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.ComponentModel.DataAnnotations;

namespace CaPPMS.Attributes
{
/// <summary>
/// Provides a custom string length attribute that includes the current length of the string in the error message.
/// </summary>
public class CappmsStringLengthAttribute : StringLengthAttribute
{
private string? context;

/// <summary>
/// Initializes a new instance of the <see cref="CappmsStringLengthAttribute"/> class.
/// </summary>
/// <param name="maximumLength">The maximum length of the object shall be.</param>
public CappmsStringLengthAttribute(int maximumLength) : base(maximumLength)
{
}

/// <summary>
/// Override the default IsValid method to store the current string value.
/// </summary>
/// <param name="value">The object to validate.</param>
/// <returns><c>true</c> if valid.</returns>
public override bool IsValid(object? value)
{
if (value is string str)
{
this.context = str;
}

return base.IsValid(value);
}

/// <summary>
/// Override the default error message to include the current length of the string.
/// </summary>
/// <param name="name">Property Name.</param>
/// <returns>A formatted message.</returns>
public override string FormatErrorMessage(string name)
{
if (this.context != null)
{
string message = base.FormatErrorMessage(name);
message += $" Current length:{this.context.Length}.";
return message;
}

return base.FormatErrorMessage(name);
}
}
}
12 changes: 11 additions & 1 deletion CaPPMS/CaPPMS.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Microsoft Visual Studio Solution File, Format Version 12.00

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.13.35913.81 d17.13
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CaPPMS", "CaPPMS.csproj", "{AA9B970F-5351-4EA2-A78C-7E83F05C57C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CaPPMSTests", "..\CaPPMSTests\CaPPMSTests.csproj", "{5A356024-F9CF-BC44-22A7-4FEE4DBA2663}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -11,6 +17,10 @@ Global
{AA9B970F-5351-4EA2-A78C-7E83F05C57C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AA9B970F-5351-4EA2-A78C-7E83F05C57C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AA9B970F-5351-4EA2-A78C-7E83F05C57C1}.Release|Any CPU.Build.0 = Release|Any CPU
{5A356024-F9CF-BC44-22A7-4FEE4DBA2663}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5A356024-F9CF-BC44-22A7-4FEE4DBA2663}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A356024-F9CF-BC44-22A7-4FEE4DBA2663}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A356024-F9CF-BC44-22A7-4FEE4DBA2663}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
10 changes: 9 additions & 1 deletion CaPPMS/Model/Review.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace CaPPMS.Model
[SqlTableName("StudentReviews")]
public class Review : ISqlTableModel
{
/// <summary>
/// The maximum length of a string in the database.
/// </summary>
public const int MaxStringLength = 4 * 1024;

public Review() { }

[SqlIdProperty]
Expand All @@ -29,7 +34,10 @@ public Review() { }
public string Score { get; set; } = "0";

[Required(ErrorMessage = "Your comments for the student are appreciated.", AllowEmptyStrings = false)]
[StringLength(maximumLength: 255, MinimumLength = 25, ErrorMessage = "Please use at least 25 characters to describe the interaction of this student.")]
[CappmsStringLength(
maximumLength: MaxStringLength,
MinimumLength = 25,
ErrorMessage = "Please use at least {2} characters to describe the interaction of this student and at most {1} characters.")]
public string Comments { get; set; } = string.Empty;
}
}
55 changes: 55 additions & 0 deletions CaPPMSTests/Attribute/CappmsStringLengthAttributeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using CaPPMS.Attributes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Threading.Tasks;

namespace CaPPMSTests.Attribute
{
[TestClass]
public class CappmsStringLengthAttributeTests
{
[TestMethod]
public async Task StringIsValid()
{
// Arrange
var attribute = new CappmsStringLengthAttribute(100)
{
MinimumLength = 5,
};

string value = "This is a test string.";
Assert.IsTrue(attribute.IsValid(value));
await Task.CompletedTask;
}

[TestMethod]
public async Task StringIsInvalidMinLen()
{
// Arrange
var attribute = new CappmsStringLengthAttribute(100)
{
MinimumLength = 5,
};
string value = "Test";
Assert.IsFalse(attribute.IsValid(value));
string errorMessage = attribute.FormatErrorMessage("Test");
Assert.AreEqual("The field Test must be a string with a minimum length of 5 and a maximum length of 100. Current length:4.", errorMessage);

await Task.CompletedTask;
}

[TestMethod]
public async Task StringIsInvalidMaxLen()
{
// Arrange
var attribute = new CappmsStringLengthAttribute(10)
{
MinimumLength = 5,
};
string value = "This is a test string.";
Assert.IsFalse(attribute.IsValid(value));
string errorMessage = attribute.FormatErrorMessage("Test");
Assert.AreEqual("The field Test must be a string with a minimum length of 5 and a maximum length of 10. Current length:22.", errorMessage);
await Task.CompletedTask;
}
}
}
9 changes: 5 additions & 4 deletions CaPPMSTests/Model/Table/TableTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CaPPMS.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CaPPMSTests.Model.Table
{
Expand All @@ -8,7 +9,7 @@ public class TableTests
[TestMethod]
public void NumberOfColumns()
{
var table = new CaPPMS.Model.Table.Table()
var table = new CaPPMS.Model.Table.Table<DummyTableObject>()
{
DataSource = new IEnumberableDummyObject()
};
Expand All @@ -19,7 +20,7 @@ public void NumberOfColumns()
[TestMethod]
public void ColumnNames()
{
var table = new CaPPMS.Model.Table.Table()
var table = new CaPPMS.Model.Table.Table<DummyTableObject>()
{
DataSource = new IEnumberableDummyObject()
};
Expand All @@ -31,7 +32,7 @@ public void ColumnNames()
[TestMethod]
public void VerifySingleRow()
{
var table = new CaPPMS.Model.Table.Table()
var table = new CaPPMS.Model.Table.Table<DummyTableObject>()
{
DataSource = new IEnumberableDummyObject()
};
Expand Down
Loading