diff --git a/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj b/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj index c99e37e..b4f81d1 100644 --- a/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj +++ b/Foundation.ObjectHydrator.Tests/Foundation.ObjectHydrator.Tests.csproj @@ -1,6 +1,6 @@  - net10.0 + net8.0 Library diff --git a/Foundation.ObjectHydrator.Tests/HydratorTests/LargeStringLength_Tests.cs b/Foundation.ObjectHydrator.Tests/HydratorTests/LargeStringLength_Tests.cs new file mode 100644 index 0000000..0fc0aaf --- /dev/null +++ b/Foundation.ObjectHydrator.Tests/HydratorTests/LargeStringLength_Tests.cs @@ -0,0 +1,60 @@ +using NUnit.Framework; +using Foundation.ObjectHydrator.Tests.POCOs; + +namespace Foundation.ObjectHydrator.Tests.HydratorTests +{ + [TestFixture] + public class LargeStringLength_Tests + { + [Test] + public void CanHandleLargeStringLengthAttribute() + { + var hydrator = new Hydrator(); + + // This should not throw an exception + var customer = hydrator.GetSingle(); + + Assert.That(customer.LargeDescription, Is.Not.Null); + Assert.That(customer.LargeDescription.Length, Is.LessThanOrEqualTo(4000)); + } + + [Test] + public void CanHandleStringLengthOfOne() + { + var hydrator = new Hydrator(); + + // This might throw an exception with the current implementation + var customer = hydrator.GetSingle(); + + Assert.That(customer.TinyDescription, Is.Not.Null); + Assert.That(customer.TinyDescription.Length, Is.LessThanOrEqualTo(1)); + } + + [Test] + public void CanHandleStringLengthOfTwo() + { + var hydrator = new Hydrator(); + + // This might throw an exception with the current implementation + var customer = hydrator.GetSingle(); + + Assert.That(customer.SmallDescription, Is.Not.Null); + Assert.That(customer.SmallDescription.Length, Is.LessThanOrEqualTo(2)); + } + + [Test] + public void CanGenerateMultipleLargeStringLengthObjects() + { + var hydrator = new Hydrator(); + + // Run multiple times to increase chances of hitting the random edge case + for (int i = 0; i < 100; i++) + { + var customer = hydrator.GetSingle(); + Assert.That(customer.LargeDescription, Is.Not.Null); + Assert.That(customer.TinyDescription, Is.Not.Null); + Assert.That(customer.SmallDescription, Is.Not.Null); + } + } + } +} \ No newline at end of file diff --git a/Foundation.ObjectHydrator.Tests/POCOs/LargeStringLengthCustomer.cs b/Foundation.ObjectHydrator.Tests/POCOs/LargeStringLengthCustomer.cs new file mode 100644 index 0000000..0ec9dce --- /dev/null +++ b/Foundation.ObjectHydrator.Tests/POCOs/LargeStringLengthCustomer.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace Foundation.ObjectHydrator.Tests.POCOs +{ + public class LargeStringLengthCustomer + { + public string FirstName { get; set; } + + [StringLength(4000)] + public string LargeDescription { get; set; } + + [StringLength(1)] + public string TinyDescription { get; set; } + + [StringLength(2)] + public string SmallDescription { get; set; } + } +} \ No newline at end of file diff --git a/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj b/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj index 6d1d896..b57154f 100644 --- a/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj +++ b/Foundation.ObjectHydrator/Foundation.ObjectHydrator.csproj @@ -1,6 +1,6 @@  - net10.0 + net8.0 Library diff --git a/Foundation.ObjectHydrator/Generators/TextGenerator.cs b/Foundation.ObjectHydrator/Generators/TextGenerator.cs index d0f7324..034b159 100644 --- a/Foundation.ObjectHydrator/Generators/TextGenerator.cs +++ b/Foundation.ObjectHydrator/Generators/TextGenerator.cs @@ -31,7 +31,18 @@ public TextGenerator(int length) public string Generate() { - return sampleText.Substring(0, random.Next(1, Length - 1)).Trim(); + // Ensure we have a valid length to work with + int maxLength = Math.Min(Length, sampleText.Length); + + if (maxLength <= 0) + return string.Empty; + + if (maxLength == 1) + return sampleText.Substring(0, 1).Trim(); + + // For lengths 2 and above, generate a random length between 1 and maxLength (inclusive) + int randomLength = random.Next(1, maxLength + 1); + return sampleText.Substring(0, randomLength).Trim(); } #endregion