Skip to content

Commit dd48544

Browse files
committed
Refactors SequenceDiagram and related methods
Replaces direct field access with a public property `DiagramBuilder` in `SequenceDiagram`. Updates related methods to use this property for better encapsulation and maintainability.
1 parent 2ef40a1 commit dd48544

File tree

2 files changed

+24
-41
lines changed

2 files changed

+24
-41
lines changed

src/GitVersion.Core.Tests/IntegrationTests/RepositoryFixtureExtensions.cs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,15 @@ internal static class RepositoryFixtureExtensions
77
public static void MakeACommit(this RepositoryFixtureBase fixture, string commitMsg)
88
{
99
fixture.Repository.MakeACommit(commitMsg);
10-
var diagramBuilder = (StringBuilder?)typeof(SequenceDiagram)
11-
.GetField("diagramBuilder", BindingFlags.Instance | BindingFlags.NonPublic)
12-
?.GetValue(fixture.SequenceDiagram);
1310

14-
var participant = GetParticipant(fixture.Repository.Head.FriendlyName);
15-
if (participant != null)
16-
{
17-
AddTheCommitMessage(fixture, commitMsg, diagramBuilder, participant);
18-
}
19-
20-
string? GetParticipant(string participantName) =>
21-
(string?)typeof(SequenceDiagram).GetMethod("GetParticipant", BindingFlags.Instance | BindingFlags.NonPublic)
22-
?.Invoke(fixture.SequenceDiagram,
23-
[
24-
participantName
25-
]);
26-
}
27-
28-
private static void AddTheCommitMessage(RepositoryFixtureBase fixture, string commitMsg, StringBuilder? diagramBuilder, string participant)
29-
{
11+
var participant = fixture.SequenceDiagram.GetParticipant(fixture.Repository.Head.FriendlyName);
3012
if (commitMsg.Length < 40)
3113
{
32-
diagramBuilder?.AppendLineFormat("{0} -> {0}: Commit '{1}'", participant, commitMsg);
14+
fixture.SequenceDiagram.DiagramBuilder.AppendLineFormat("{0} -> {0}: Commit '{1}'", participant, commitMsg);
3315
}
3416
else
3517
{
36-
var formattedCommitMsg = string.Join(System.Environment.NewLine, $"Commit '{commitMsg}'".SplitIntoLines(60));
18+
var formattedCommitMsg = string.Join(SysEnv.NewLine, $"Commit '{commitMsg}'".SplitIntoLines(60));
3719
fixture.SequenceDiagram.NoteOver(formattedCommitMsg, participant);
3820
}
3921
}

src/GitVersion.Testing/Fixtures/SequenceDiagram.cs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,32 @@ namespace GitVersion.Testing;
99
public class SequenceDiagram
1010
{
1111
private readonly Dictionary<string, string> participants = [];
12-
private readonly StringBuilder diagramBuilder;
1312

1413
/// <summary>
1514
/// Initializes a new instance of the <see cref="T:SequenceDiagram"/> class.
1615
/// </summary>
1716
public SequenceDiagram()
1817
{
19-
this.diagramBuilder = new StringBuilder();
20-
this.diagramBuilder.AppendLine("@startuml");
18+
this.DiagramBuilder = new StringBuilder();
19+
this.DiagramBuilder.AppendLine("@startuml");
2120
}
2221

22+
public StringBuilder DiagramBuilder { get; }
23+
2324
/// <summary>
2425
/// Activates a branch/participant in the sequence diagram
2526
/// </summary>
26-
public void Activate(string branch) => this.diagramBuilder.AppendLineFormat("activate {0}", GetParticipant(branch));
27+
public void Activate(string branch) => this.DiagramBuilder.AppendLineFormat("activate {0}", GetParticipant(branch));
2728

2829
/// <summary>
2930
/// Deactivates a branch/participant in the sequence diagram
3031
/// </summary>
31-
public void Deactivate(string branch) => this.diagramBuilder.AppendLineFormat("deactivate {0}", GetParticipant(branch));
32+
public void Deactivate(string branch) => this.DiagramBuilder.AppendLineFormat("deactivate {0}", GetParticipant(branch));
3233

3334
/// <summary>
3435
/// Destroys a branch/participant in the sequence diagram
3536
/// </summary>
36-
public void Destroy(string branch) => this.diagramBuilder.AppendLineFormat("destroy {0}", GetParticipant(branch));
37+
public void Destroy(string branch) => this.DiagramBuilder.AppendLineFormat("destroy {0}", GetParticipant(branch));
3738

3839
/// <summary>
3940
/// Creates a participant in the sequence diagram
@@ -43,21 +44,21 @@ public void Participant(string participant, string? @as = null)
4344
var cleanParticipant = ParticipantSanitizer.SanitizeParticipant(@as ?? participant);
4445
this.participants.Add(participant, cleanParticipant);
4546
if (participant == cleanParticipant)
46-
this.diagramBuilder.AppendLineFormat("participant {0}", participant);
47+
this.DiagramBuilder.AppendLineFormat("participant {0}", participant);
4748
else
48-
this.diagramBuilder.AppendLineFormat("participant \"{0}\" as {1}", participant, cleanParticipant);
49+
this.DiagramBuilder.AppendLineFormat("participant \"{0}\" as {1}", participant, cleanParticipant);
4950
}
5051

5152
/// <summary>
5253
/// Appends a divider with specified text to the sequence diagram
5354
/// </summary>
54-
public void Divider(string text) => this.diagramBuilder.AppendLineFormat("== {0} ==", text);
55+
public void Divider(string text) => this.DiagramBuilder.AppendLineFormat("== {0} ==", text);
5556

5657
/// <summary>
5758
/// Appends a note over one or many participants to the sequence diagram
5859
/// </summary>
5960
public void NoteOver(string noteText, string startParticipant, string? endParticipant = null, string? prefix = null, string? color = null) =>
60-
this.diagramBuilder.AppendLineFormat(
61+
this.DiagramBuilder.AppendLineFormat(
6162
prefix + """
6263
note over {0}{1}{2}
6364
{3}
@@ -71,7 +72,7 @@ end note
7172
/// <summary>
7273
/// Appends applying a tag to the specified branch/participant to the sequence diagram
7374
/// </summary>
74-
public void ApplyTag(string tag, string toBranch) => this.diagramBuilder.AppendLineFormat("{0} -> {0}: tag {1}", GetParticipant(toBranch), tag);
75+
public void ApplyTag(string tag, string toBranch) => this.DiagramBuilder.AppendLineFormat("{0} -> {0}: tag {1}", GetParticipant(toBranch), tag);
7576

7677
/// <summary>
7778
/// Appends branching from a branch to another branch, @as can override the participant name
@@ -80,11 +81,11 @@ public void BranchTo(string branchName, string currentName, string? @as)
8081
{
8182
if (!this.participants.ContainsKey(branchName))
8283
{
83-
this.diagramBuilder.Append("create ");
84+
this.DiagramBuilder.Append("create ");
8485
Participant(branchName, @as);
8586
}
8687

87-
this.diagramBuilder.AppendLineFormat(
88+
this.DiagramBuilder.AppendLineFormat(
8889
"{0} -> {1}: branch from {2}",
8990
GetParticipant(currentName),
9091
GetParticipant(branchName), currentName);
@@ -97,32 +98,32 @@ public void BranchToFromTag(string branchName, string fromTag, string onBranch,
9798
{
9899
if (!this.participants.ContainsKey(branchName))
99100
{
100-
this.diagramBuilder.Append("create ");
101+
this.DiagramBuilder.Append("create ");
101102
Participant(branchName, @as);
102103
}
103104

104-
this.diagramBuilder.AppendLineFormat("{0} -> {1}: branch from tag ({2})", GetParticipant(onBranch), GetParticipant(branchName), fromTag);
105+
this.DiagramBuilder.AppendLineFormat("{0} -> {1}: branch from tag ({2})", GetParticipant(onBranch), GetParticipant(branchName), fromTag);
105106
}
106107

107108
/// <summary>
108109
/// Appends a commit on the target participant/branch to the sequence diagram
109110
/// </summary>
110-
public void MakeACommit(string toParticipant) => this.diagramBuilder.AppendLineFormat("{0} -> {0}: commit", GetParticipant(toParticipant));
111+
public void MakeACommit(string toParticipant) => this.DiagramBuilder.AppendLineFormat("{0} -> {0}: commit", GetParticipant(toParticipant));
111112

112113
/// <summary>
113114
/// Append a merge to the sequence diagram
114115
/// </summary>
115-
public void Merge(string from, string to) => this.diagramBuilder.AppendLineFormat("{0} -> {1}: merge", GetParticipant(from), GetParticipant(to));
116+
public void Merge(string from, string to) => this.DiagramBuilder.AppendLineFormat("{0} -> {1}: merge", GetParticipant(from), GetParticipant(to));
116117

117-
private string GetParticipant(string branch) => this.participants.GetValueOrDefault(branch, branch);
118+
public string GetParticipant(string branch) => this.participants.GetValueOrDefault(branch, branch);
118119

119120
/// <summary>
120121
/// Ends the sequence diagram
121122
/// </summary>
122-
public void End() => this.diagramBuilder.AppendLine("@enduml");
123+
public void End() => this.DiagramBuilder.AppendLine("@enduml");
123124

124125
/// <summary>
125126
/// returns the plantUML representation of the Sequence Diagram
126127
/// </summary>
127-
public string GetDiagram() => this.diagramBuilder.ToString();
128+
public string GetDiagram() => this.DiagramBuilder.ToString();
128129
}

0 commit comments

Comments
 (0)