From f214260502558df2d7c76589f0447dbf2f48c10f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Kro=CC=88ner?= Date: Sun, 29 Oct 2023 22:23:26 +0100 Subject: [PATCH] refactor: Reduce duplication in Parsing test --- .../Parsing_a_conventional_commit_message.cs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/test/ConventionalChangelog.Unit.Tests/Changelog_specs/Parsing_a_conventional_commit_message.cs b/test/ConventionalChangelog.Unit.Tests/Changelog_specs/Parsing_a_conventional_commit_message.cs index c5bc85b..eebc923 100644 --- a/test/ConventionalChangelog.Unit.Tests/Changelog_specs/Parsing_a_conventional_commit_message.cs +++ b/test/ConventionalChangelog.Unit.Tests/Changelog_specs/Parsing_a_conventional_commit_message.cs @@ -9,7 +9,8 @@ namespace ConventionalChangelog.Unit.Tests.Changelog_specs; public class Parsing_a_conventional_commit_message { - private static readonly IConfiguration Config = Configuration.Default(); + private static readonly MessageParser MessageParser = new(Configuration.Default()); + private static CommitMessage Parsed(string message) => MessageParser.Parse(message); // Lb = Linebreak. The abbreviation was chosen to keep string definitions short private static readonly string Lb = Environment.NewLine; @@ -17,7 +18,7 @@ public class Parsing_a_conventional_commit_message // Conventional commit specification: // https://www.conventionalcommits.org/en/v1.0.0/#specification - private static class ConventionalCommit + private static class TestCommit { public const string Type = "feat"; public const string Description = "description"; @@ -50,31 +51,30 @@ and multiple blank lines public const string Footer = $"{FooterToken}: {FooterValue}"; } - private readonly CommitMessage _parsed = MessageParser.Parse(ConventionalCommit.Message); + private readonly CommitMessage _parsed = Parsed(TestCommit.Message); [Fact] public void extracts_its_type_indicator() { - _parsed.TypeIndicator.Should().Be(ConventionalCommit.Type); + _parsed.TypeIndicator.Should().Be(TestCommit.Type); } [Fact] public void extracts_its_description() { - _parsed.Description.Should().Be(ConventionalCommit.Description); + _parsed.Description.Should().Be(TestCommit.Description); } [Fact] public void extracts_its_body() { - _parsed.Body.Should().Be(ConventionalCommit.Body); + _parsed.Body.Should().Be(TestCommit.Body); } [Fact] public void with_a_single_footer_extracts_its_footer() { - _parsed.Footers.Should().Equal( - new Footer(ConventionalCommit.FooterToken, ConventionalCommit.FooterValue)); + _parsed.Footers.Should().Equal(new Footer(TestCommit.FooterToken, TestCommit.FooterValue)); } [Fact] @@ -84,8 +84,8 @@ public void with_multiple_footers_extracts_all_footers() $"#value 3{Lb}token-4 #value{Lb}with extra line{Lb}{Lb}" + $"token-5: value{Lb}{Lb}with blank line"; - var messageWithSpecificFooter = ConventionalCommit.Message.Replace(ConventionalCommit.Footer, footers); - var parsed = MessageParser.Parse(messageWithSpecificFooter); + var message = TestCommit.Message.Replace(TestCommit.Footer, footers); + var parsed = Parsed(message); parsed.Footers.Should().Equal( new Footer("token1", "value1"), @@ -106,8 +106,6 @@ public void with_multiple_footers_extracts_all_footers() $"value with{Lb}{Lb}blank line", }; - private static readonly MessageParser MessageParser = new(Config); - public static IEnumerable BreakingChangeConventionFooters() { foreach (var token in new[] { "BREAKING CHANGE", "BREAKING-CHANGE", }) @@ -137,8 +135,8 @@ public static IEnumerable YouTrackConventionFooters() [MemberData(nameof(YouTrackConventionFooters))] public void extracts_from_a(string formattedFooter, string theToken, string andTheValue) { - var messageWithSpecificFooter = ConventionalCommit.Message.Replace(ConventionalCommit.Footer, formattedFooter); - var parsed = MessageParser.Parse(messageWithSpecificFooter); + var message = TestCommit.Message.Replace(TestCommit.Footer, formattedFooter); + var parsed = Parsed(message); parsed.Footers.Should().BeEquivalentTo(new Footer[]{new (theToken, andTheValue)}); }