From 93344cc4e1ebc191ee732a450fde7ce9e90d428c Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 3 Dec 2024 16:24:55 +0100 Subject: [PATCH] Relax version directive argument, allow for major.minor format (#89) --- .../Myst/Directives/VersionBlock.cs | 8 +++-- .../Directives/VersionTests.cs | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/Elastic.Markdown/Myst/Directives/VersionBlock.cs b/src/Elastic.Markdown/Myst/Directives/VersionBlock.cs index ad225e7..9fda232 100644 --- a/src/Elastic.Markdown/Myst/Directives/VersionBlock.cs +++ b/src/Elastic.Markdown/Myst/Directives/VersionBlock.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information using Elastic.Markdown.Helpers; +using static System.StringSplitOptions; namespace Elastic.Markdown.Myst.Directives; @@ -17,7 +18,7 @@ public class VersionBlock(DirectiveBlockParser parser, string directive, Diction public override void FinalizeAndValidate(ParserContext context) { - var tokens = Arguments?.Split(" ", 2, StringSplitOptions.RemoveEmptyEntries) ?? []; + var tokens = Arguments?.Split(" ", 2, RemoveEmptyEntries) ?? []; if (tokens.Length < 1) { EmitError(context, $"{directive} needs exactly 2 arguments: "); @@ -26,8 +27,9 @@ public override void FinalizeAndValidate(ParserContext context) if (!SemVersion.TryParse(tokens[0], out var version)) { - EmitError(context, $"{tokens[0]} is not a valid version"); - return; + var numbers = tokens[0].Split('.', RemoveEmptyEntries); + if (numbers.Length != 2 || !SemVersion.TryParse($"{numbers[0]}.{numbers[1]}.0", out version)) + EmitError(context, $"'{tokens[0]}' is not a valid version"); } Version = version; diff --git a/tests/Elastic.Markdown.Tests/Directives/VersionTests.cs b/tests/Elastic.Markdown.Tests/Directives/VersionTests.cs index 8e0573e..f5fc59c 100644 --- a/tests/Elastic.Markdown.Tests/Directives/VersionTests.cs +++ b/tests/Elastic.Markdown.Tests/Directives/VersionTests.cs @@ -50,3 +50,35 @@ public class VersionDeprectatedTests(ITestOutputHelper output) : VersionTests(ou [Fact] public void SetsTitle() => Block!.Title.Should().Be("Deprecated (1.0.1-beta1): more information"); } + +public abstract class VersionValidationTests(ITestOutputHelper output, string version) : DirectiveTest<VersionBlock>(output, +$$""" +```{versionchanged} {{version}} more information +Version brief summary +``` +A regular paragraph. +""" +); + +public class SimpleVersion(ITestOutputHelper output) : VersionValidationTests(output, "7.17") +{ + [Fact] + public void SetsVersion() => Block!.Version.Should().Be(new SemVersion(7, 17, 0)); + + [Fact] + public void HasNoError() => Collector.Diagnostics.Should().BeEmpty(); +} + +public class MajorVersionOnly(ITestOutputHelper output) : VersionValidationTests(output, "8") +{ + [Fact] + public void HasError() => Collector.Diagnostics.Should().HaveCount(1) + .And.Contain(d => d.Message.Contains("'8' is not a valid version")); +} + +public class BranchVersion(ITestOutputHelper output) : VersionValidationTests(output, "8.x") +{ + [Fact] + public void HasError() => Collector.Diagnostics.Should().HaveCount(1) + .And.Contain(d => d.Message.Contains("'8.x' is not a valid version")); +}