Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove obsolete handling of \d marker #216

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/SIL.Machine/Corpora/MemoryStreamContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.IO;
using System.Text;
using SIL.ObjectModel;

namespace SIL.Machine.Corpora
{
public class MemoryStreamContainer : DisposableBase, IStreamContainer
{
private readonly string _usfm;

public MemoryStreamContainer(string usfm)
{
_usfm = usfm;
}

public Stream OpenStream()
{
return new MemoryStream(Encoding.UTF8.GetBytes(_usfm));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ IReadOnlyList<UsfmAttribute> attributes
if (_curVerseRef.IsDefault)
UpdateVerseRef(state.VerseRef, marker);

if (!state.IsVerseText || marker == "d")
if (!state.IsVerseText)
{
StartParentElement(marker);
StartNonVerseText(state);
Expand Down
29 changes: 29 additions & 0 deletions src/SIL.Machine/Corpora/UsfmMemoryText.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Text;
using SIL.Scripture;

namespace SIL.Machine.Corpora
{
public class UsfmMemoryText : UsfmTextBase
{
private readonly string _usfm;

public UsfmMemoryText(
UsfmStylesheet stylesheet,
Encoding encoding,
string id,
string usfm,
ScrVers versification = null,
bool includeMarkers = false,
bool includeAllText = false
)
: base(id, stylesheet, encoding, versification, includeMarkers, includeAllText)
{
_usfm = usfm;
}

protected override IStreamContainer CreateStreamContainer()
{
return new MemoryStreamContainer(_usfm);
}
}
}
8 changes: 8 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/CorporaTestHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO.Compression;
using NUnit.Framework.Constraints;

namespace SIL.Machine.Corpora;

Expand Down Expand Up @@ -35,4 +36,11 @@ public static string CreateTestParatextBackup()
ZipFile.CreateFromDirectory(UsfmTestProjectPath, path);
return path;
}

public static EqualConstraint IgnoreLineEndings(this EqualConstraint constraint)
{
return constraint.Using<string>(
(actual, expected) => actual.ReplaceLineEndings() == expected.ReplaceLineEndings()
);
}
}
65 changes: 65 additions & 0 deletions tests/SIL.Machine.Tests/Corpora/UsfmMemoryTextTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System.Text;
using NUnit.Framework;

namespace SIL.Machine.Corpora;

[TestFixture]
public class UsfmMemoryTextTests
{
[Test]
public void GetRows_VerseDescriptiveTitle()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
\d
\v 1 Descriptive title
\c 2
\b
\q1
\s
"
);

Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1")));
Assert.That(rows[0].Text, Is.EqualTo("Descriptive title"));
});
}

[Test]
public void GetRows_LastSegment()
{
TextRow[] rows = GetRows(
@"\id MAT - Test
\c 1
\v 1 Last segment
"
);

Assert.Multiple(() =>
{
Assert.That(rows, Has.Length.EqualTo(1));

Assert.That(rows[0].Ref, Is.EqualTo(ScriptureRef.Parse("MAT 1:1")));
Assert.That(rows[0].Text, Is.EqualTo("Last segment"));
});
}

private static TextRow[] GetRows(string usfm, bool includeMarkers = false, bool includeAllText = false)
{
UsfmMemoryText text =
new(
new UsfmStylesheet("usfm.sty"),
Encoding.UTF8,
"MAT",
usfm.Trim().ReplaceLineEndings("\r\n") + "\r\n",
includeMarkers: includeMarkers,
includeAllText: includeAllText
);
return text.GetRows().ToArray();
}
}
29 changes: 23 additions & 6 deletions tests/SIL.Machine.Tests/Corpora/UsfmTextUpdaterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,26 @@ public void GetUsfm_Verse_DoubleVaVp()
}

[Test]
public void GetUsfm_Verse_LastVerse()
public void GetUsfm_Verse_LastSegment()
{
var rows = new List<(IReadOnlyList<ScriptureRef>, string)> { (ScrRef("MAT 4:1"), "Updating the last verse.") };
var rows = new List<(IReadOnlyList<ScriptureRef>, string)> { (ScrRef("MAT 1:1"), "Updating the last verse.") };
string usfm =
@"\id MAT - Test
\c 1
\v 1
";
string target = UpdateUsfm(rows, usfm);

string target = UpdateUsfm(rows);
Assert.That(target, Contains.Substring("\\id MAT - Test\r\n"));
Assert.That(target, Contains.Substring("\\v 1 Updating the last verse.\r\n"));
Assert.That(
target,
Is.EqualTo(
@"\id MAT - Test
\c 1
\v 1 Updating the last verse.
"
)
.IgnoreLineEndings()
);
}

[Test]
Expand All @@ -409,12 +422,16 @@ private static ScriptureRef[] ScrRef(params string[] refs)

private static string UpdateUsfm(
IReadOnlyList<(IReadOnlyList<ScriptureRef>, string)>? rows = null,
string? source = null,
string? idText = null,
bool stripAllText = false,
bool preferExistingText = false
)
{
string source = ReadUsfm();
if (source is null)
source = ReadUsfm();
else
source = source.Trim().ReplaceLineEndings("\r\n") + "\r\n";
var updater = new UsfmTextUpdater(
rows,
idText,
Expand Down
Loading