Skip to content

Commit

Permalink
TextFormatter shouldn't add final newline
Browse files Browse the repository at this point in the history
  • Loading branch information
menees committed Oct 24, 2023
1 parent 7e13632 commit eb84585
Show file tree
Hide file tree
Showing 21 changed files with 95 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<IsPublishable>false</IsPublishable>

<!-- Make the assembly, file, and NuGet package versions the same. -->
<Version>0.8.5-beta</Version>
<Version>0.8.6-beta</Version>
<UseLocaMeneesLibraries>false</UseLocaMeneesLibraries>
<LocaMeneesLibrariesSrc/>
<BuildingInsideVisualStudio Condition="'$(BuildingInsideVisualStudio)' == ''">false</BuildingInsideVisualStudio>
Expand Down
6 changes: 2 additions & 4 deletions src/Menees.Chords/ChordProLyricLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Text;
using Menees.Chords.Formatters;
using Menees.Chords.Parsers;

#endregion
Expand Down Expand Up @@ -246,10 +247,7 @@ void AppendChord(string chordText, Func<string, TextSegment> createChordSegment)
LyricLine? lyrics = null;
if (lyricLineText.Length > 0)
{
while (lyricLineText.Length > 0 && char.IsWhiteSpace(lyricLineText[^1]))
{
lyricLineText.Length--;
}
TextFormatter.TrimEnd(lyricLineText);

if (lyricLineText.Length > 0)
{
Expand Down
18 changes: 18 additions & 0 deletions src/Menees.Chords/Formatters/TextFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public TextFormatter(IEntryContainer container, string? indent = null)

#region Public Methods

/// <summary>
/// Trims trailing whitespace from <paramref name="value"/>.
/// </summary>
/// <param name="value">The builder to trim.</param>
/// <returns>The input <paramref name="value"/>.</returns>
public static StringBuilder TrimEnd(StringBuilder value)
{
Conditions.RequireNonNull(value);

while (value.Length > 0 && char.IsWhiteSpace(value[^1]))
{
value.Length--;
}

return value;
}

/// <inheritdoc/>
public override string ToString()
{
Expand Down Expand Up @@ -109,6 +126,7 @@ protected override void EndContainer(IEntryContainer container, IReadOnlyCollect

if (hierarchy.Count == 0)
{
TrimEnd(this.builder);
this.text = this.builder.ToString();
}
}
Expand Down
18 changes: 15 additions & 3 deletions tests/Menees.Chords.Tests/Formatters/TextFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#region Using Directives

using System.Diagnostics;
using System.Text;
using Menees.Chords.Parsers;

#endregion
Expand Down Expand Up @@ -42,7 +43,7 @@ public void IndentWithNewLineTest()
TextFormatter formatter = new(outer, "\t");
string text = formatter.ToString();
Debug.WriteLine(text);
text.ShouldBe("\tLine 1\r\n\tLine 2\r\n", StringCompareShould.IgnoreLineEndings);
text.ShouldBe("\tLine 1\r\n\tLine 2", StringCompareShould.IgnoreLineEndings);
}

[TestMethod]
Expand All @@ -59,11 +60,22 @@ public void AnnotatedTest()
Swing low, sweet chariot, ** Sing "low" as bass **
A Bb B (Half steps)
G G2 D/F# Em C Cmaj5 (2x)

""",
StringCompareShould.IgnoreLineEndings);
}

[TestMethod]
public void TrimEndTest()
{
Test("Test Case \t \r\n ", "Test Case");
Test("Unchanged", "Unchanged");
Test(" \r\n ", string.Empty);
Test(string.Empty, string.Empty);

static void Test(string text, string expected)
=> TextFormatter.TrimEnd(new StringBuilder(text)).ToString().ShouldBe(expected);
}

#endregion

#region Private Methods
Expand All @@ -81,7 +93,7 @@ private static string[] Test(string? indent)
}

string[] lines = text.Split('\n').Select(line => line.TrimEnd()).ToArray();
lines.Length.ShouldBe(19);
lines.Length.ShouldBe(18);
return lines;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Menees.Chords.Tests/Menees.Chords.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
<None Remove="Samples\Expected ChordPro\Alone With You.cho" />
<None Remove="Samples\Expected ChordPro\Bring Him Home.cho" />
<None Remove="Samples\Expected ChordPro\Every Rose Has Its Thorn.cho" />
<None Remove="Samples\Expected ChordPro\Swing Low Sweet Chariot.cho" />
<None Remove="Samples\Expected ChordPro\Test Only.cho" />
<None Remove="Samples\Expected MobileSheets\Alone With You.cho" />
<None Remove="Samples\Expected MobileSheets\Bring Him Home.cho" />
<None Remove="Samples\Expected MobileSheets\Every Rose Has Its Thorn.cho" />
<None Remove="Samples\Expected MobileSheets\Swing Low Sweet Chariot.cho" />
<None Remove="Samples\Expected MobileSheets\Test Only.cho" />
<None Remove="Samples\Swing Low Sweet Chariot.cho" />
<None Remove="Samples\Test Only.txt" />
Expand Down Expand Up @@ -59,6 +61,9 @@
<Content Include="Samples\Expected ChordPro\Every Rose Has Its Thorn.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Samples\Expected ChordPro\Swing Low Sweet Chariot.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Samples\Expected ChordPro\Test Only.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand All @@ -71,6 +76,9 @@
<Content Include="Samples\Expected MobileSheets\Every Rose Has Its Thorn.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Samples\Expected MobileSheets\Swing Low Sweet Chariot.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Samples\Expected MobileSheets\Test Only.cho">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ D|--/2---2-------------------------------4---4-0-0h2------|
e|-----------------5/7-5-3-3h5------5-7-8-7--------|
B|------3-5-5--------------------------------------|
G|--2/4--------------------------------------------|
D|-------------------------------------------------|
D|-------------------------------------------------|
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ C C* G*
** Use higher chords: **
C* 8-10-10-9-8-8
G* x-10-12-12-12-10
** arpeggiate slowly **
** arpeggiate slowly **
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@ Just like every night has its dawn
G D Cadd9 G
Just like every cowboy sings his sad, sad song
Cadd9 * G
Every rose has its thorn * E:0 E:2 A:0 D:0 E:0
Every rose has its thorn * E:0 E:2 A:0 D:0 E:0
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ A band of angels comin' after me,
A7 D
Comin' for to carry me home.

** Chorus **
** Chorus **
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@

[Intro - Escape (Pi�a Colada)]
F | Am G F | F | C | G F C | G
F | Am G F | F | C | G F C |
F | Am G F | F | C | G F C |
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ B|------3-5-5--------------------------------------|
G|--2/4--------------------------------------------|
D|-------------------------------------------------|
{end_of_tab}
{end_of_bridge}
{end_of_bridge}
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ And I am [Am]old, and will be [B]gone.
{chord: C* base-fret 8 frets 8 10 10 9 8 8}
{chord: G* base-fret 10 frets x 10 12 12 12 10}
{comment: arpeggiate slowly}
{end_of_bridge}
{end_of_bridge}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ And to [Cadd9]see you cuts me like a knife I guess
Just like [G]every night has its [Cadd9]dawn
Just like [G]every co[D]wboy sings hi[Cadd9]s sad, sad so[G]ng
Every rose has its t[Cadd9]horn [**] [G] * E:0 E:2 A:0 D:0 E:0
{end_of_chorus}
{end_of_chorus}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A simple ChordPro song.
# From https://www.chordpro.org/chordpro/chordpro-introduction/

{title: Swing Low Sweet Chariot}

{start_of_chorus}
Swing [D]low, sweet [G]chari[D]ot,
Comin' for to carry me [A7]home.
Swing [D7]low, sweet [G]chari[D]ot,
Comin' for to [A7]carry me [D]home.
{end_of_chorus}

I [D]looked over Jordan, and [G]what did I [D]see,
Comin' for to carry me [A7]home.
A [D]band of angels [G]comin' after [D]me,
Comin' for to [A7]carry me [D]home.

{comment: Chorus}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
F | Am G F | F | C | G F C | G
F | Am G F | F | C | G F C |
{end_of_grid}
{end_of_bridge}
{end_of_bridge}
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ B|------3-5-5--------------------------------------|
G|--2/4--------------------------------------------|
D|-------------------------------------------------|
{end_of_tab}
{end_of_verse}
{end_of_verse}
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ And I am [Am]old, and will be [B]gone.
{comment: Use higher chords:}
{comment: C* 8-10-10-9-8-8}
{comment: G* x-10-12-12-12-10 (arpeggiate slowly)}
{end_of_verse}
{end_of_verse}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ And to [Cadd9]see you cuts me like a knife I guess
Just like [G]every night has its [Cadd9]dawn
Just like [G]every co[D]wboy sings hi[Cadd9]s sad, sad so[G]ng
Every rose has its t[Cadd9]horn [**] [G] * E:0 E:2 A:0 D:0 E:0
{end_of_chorus}
{end_of_chorus}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# A simple ChordPro song.
# From https://www.chordpro.org/chordpro/chordpro-introduction/

{title: Swing Low Sweet Chariot}

{start_of_chorus}
Swing [D]low, sweet [G]chari[D]ot,
Comin' for to carry me [A7]home.
Swing [D7]low, sweet [G]chari[D]ot,
Comin' for to [A7]carry me [D]home.
{end_of_chorus}

I [D]looked over Jordan, and [G]what did I [D]see,
Comin' for to carry me [A7]home.
A [D]band of angels [G]comin' after [D]me,
Comin' for to [A7]carry me [D]home.

{comment: Chorus}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@
[F] [*|] [Am] [G] [F] [*|] [F] [*|] [C] [*|] [G] [F] [C] [*|] [G]
[F] [*|] [Am] [G] [F] [*|] [F] [*|] [C] [*|] [G] [F] [C] [*|]
{end_of_grid}
{end_of_verse}
{end_of_verse}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,8 @@ public void GroupGridEnvironmentTest()
baseFolder,
expectedFolder,
Path.ChangeExtension(Path.GetFileName(fileName), extension));
string expectedText = File.Exists(expectedFileName)
? File.ReadAllText(expectedFileName)
: File.ReadAllText(fileName);
File.Exists(expectedFileName).ShouldBeTrue($"Expected file should exist: {expectedFileName}");
string expectedText = File.ReadAllText(expectedFileName);
text.ShouldBe(expectedText, StringCompareShould.IgnoreLineEndings);
}
}
Expand Down

0 comments on commit eb84585

Please sign in to comment.