Skip to content

Commit

Permalink
#919 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zhabis committed Sep 23, 2024
1 parent cd83cb2 commit 339b62d
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Azos/Text/TagParsingUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ internal Segment(bool isTag, int idxs, int idxe, string content)
public readonly bool IsTag;
public readonly int IdxStart;
public readonly int IdxEnd;
public int Length => IdxEnd - IdxStart;

public int Length => 1 + IdxEnd - IdxStart;
public readonly string Content;
}

Expand Down Expand Up @@ -83,7 +82,7 @@ public static IEnumerable<Segment> ParseSegments(this IEnumerable<char> source,
{
if (buf.Length > 0)
{
yield return new Segment(false, idxs, idx, buf.ToString());
yield return new Segment(false, idxs, idx - 1, buf.ToString());
buf.Clear();
}
isTag = true;
Expand Down Expand Up @@ -161,7 +160,7 @@ public static IEnumerable<Tag> ParseTags(this IEnumerable<Segment> source, strin
/// <summary>
/// Expands HTML content by invoking a function to build tag content into a string builder
/// </summary>
public static StringBuilder ExpandHtmlTags(this IEnumerable<char> source, Action<Tag, StringBuilder> fTagExpander, string tagPragma = "@")
public static StringBuilder ExpandHtmlTags(this IEnumerable<char> source, Action<StringBuilder, Tag> fTagExpander, string tagPragma = "@")
{
fTagExpander.NonNull(nameof(fTagExpander));
var spans = source.ParseSegments('<', '>');
Expand All @@ -170,8 +169,8 @@ public static StringBuilder ExpandHtmlTags(this IEnumerable<char> source, Action
var result = new StringBuilder(1024);
foreach (var tag in tags)
{
if (tag.Segment.IsTag)
fTagExpander(tag, result);
if (tag.Segment.IsTag && tag.Data != null)
fTagExpander(result, tag);
else
result.Append($"<{tag.Segment.Content}>");
}
Expand Down
65 changes: 65 additions & 0 deletions src/testing/Azos.Tests.Nub/Parsing/TagParsingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*<FILE_LICENSE>
* Azos (A to Z Application Operating System) Framework
* The A to Z Foundation (a.k.a. Azist) licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
</FILE_LICENSE>*/

using System.Linq;
using Azos.Scripting;
using Azos.Text;

namespace Azos.Tests.Nub.Parsing
{
[Runnable]
public class TagParsingTests
{

[Run]
public void ParseSegments_00()
{
var got = "".ParseSegments().ToArray();
Aver.AreEqual(0, got.Length);

got = ((string)null).ParseSegments().ToArray();
Aver.AreEqual(0, got.Length);

got = " ".ParseSegments().ToArray();
Aver.AreEqual(1, got.Length);
Aver.IsFalse(got[0].IsTag);
Aver.AreEqual(" ", got[0].Content);
Aver.AreEqual(0, got[0].IdxStart);
Aver.AreEqual(2, got[0].IdxEnd);
Aver.AreEqual(3, got[0].Length);
}


[Run]
public void ParseSegments_01()
{
var got = "How is <monster> doing?".ParseSegments().ToArray();

got.See();

Aver.AreEqual(3, got.Length);

Aver.IsFalse(got[0].IsTag);
Aver.AreEqual("How is ", got[0].Content);
Aver.AreEqual(0, got[0].IdxStart);
Aver.AreEqual(6, got[0].IdxEnd);
Aver.AreEqual(7, got[0].Length);

Aver.IsTrue(got[1].IsTag);
Aver.AreEqual("monster", got[1].Content);
Aver.AreEqual(7, got[1].IdxStart);
Aver.AreEqual(15, got[1].IdxEnd);
Aver.AreEqual(9, got[1].Length);//with < >

Aver.IsFalse(got[2].IsTag);
Aver.AreEqual(" doing?", got[2].Content);
Aver.AreEqual(16, got[2].IdxStart);
Aver.AreEqual(22, got[2].IdxEnd);
Aver.AreEqual(7, got[2].Length);
}

}
}

0 comments on commit 339b62d

Please sign in to comment.