Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/CalendarVersioning/CalendarVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,17 @@ public static CalendarVersion Parse(string input, CalendarVersionFormat? format
if (string.IsNullOrWhiteSpace(input))
throw new ArgumentException("Input cannot be null or empty", nameof(input));

if (input.Length > 256)
throw new ArgumentException("Input version string is too long", nameof(input));

int year = 0, month = 0;
int? day = null, minor = null;

var parts = input.Split('.');
string[] parts;
if (format == null)
{
parts = input.Split('.', 5);

// Default parsing: YYYY.MM[.DD[.Minor]]
if (parts.Length is < 2 or > 4)
throw new FormatException($"Version string '{input}' does not match default format 'YYYY.MM[.DD[.Minor]]'");
Expand All @@ -51,7 +55,9 @@ public static CalendarVersion Parse(string input, CalendarVersionFormat? format

// Parsing using the format
string pattern = format.Pattern;
var tokens = pattern.Split('.');
var tokens = pattern.Split('.', 10);
parts = input.Split('.', tokens.Length + 1);

if (tokens.Length != parts.Length)
throw new FormatException($"Version string '{input}' does not match format '{pattern}'");

Expand Down
22 changes: 22 additions & 0 deletions tests/CalendarVersioning.Tests/UnitTests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,27 @@ public void Parse_CustomFormat_InvalidYY_ShouldThrowFormatException()
var format = new CalendarVersionFormat("YY.MM");
Assert.Throws<FormatException>(() => CalendarVersion.Parse("123.04", format));
}

[Fact]
public void Parse_TooLongInput_ShouldThrowArgumentException()
{
string longInput = new string('a', 257);
Assert.Throws<ArgumentException>(() => CalendarVersion.Parse(longInput));
}

[Fact]
public void Parse_TooManyDots_ShouldThrowFormatException()
{
string inputWithManyDots = "2025.04.01.1.extra.dots"; // 6 parts
Assert.Throws<FormatException>(() => CalendarVersion.Parse(inputWithManyDots));
}

[Fact]
public void Parse_CustomFormat_TooManyDots_ShouldThrowFormatException()
{
var format = new CalendarVersionFormat("YYYY.MM");
string inputWithManyDots = "2025.04.01"; // 3 parts for 2 tokens
Assert.Throws<FormatException>(() => CalendarVersion.Parse(inputWithManyDots, format));
}
}
}
Loading