Skip to content

Commit

Permalink
refactor: Reduce duplication in Parsing test
Browse files Browse the repository at this point in the history
  • Loading branch information
Crown0815 committed Oct 29, 2023
1 parent 5230b9d commit f214260
Showing 1 changed file with 12 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ 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;

// 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";
Expand Down Expand Up @@ -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]
Expand All @@ -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"),
Expand All @@ -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<object[]> BreakingChangeConventionFooters()
{
foreach (var token in new[] { "BREAKING CHANGE", "BREAKING-CHANGE", })
Expand Down Expand Up @@ -137,8 +135,8 @@ public static IEnumerable<object[]> 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)});
}
Expand Down

0 comments on commit f214260

Please sign in to comment.