Skip to content

Commit

Permalink
Add sync method to advance parser & update tests (all passing).
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan130200 committed May 6, 2024
1 parent b3ef0ff commit 9da6d85
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
32 changes: 31 additions & 1 deletion XmppSharp.Test/ParserTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using XmppSharp.Dom;

namespace XmppSharp.Test;
Expand Down Expand Up @@ -38,7 +39,7 @@ internal static async Task<Element> ParseFromBuffer([StringSyntax("Xml")] string
{
await Task.Delay(1);

var result = await parser.Advance();
var result = await parser;

if (!result)
break;
Expand Down Expand Up @@ -158,4 +159,33 @@ public async Task ParseWithCommentAndCdataAndText()

PrintResult(elem);
}

[TestMethod]
public async Task TestWithFactory()
{
using var ms = new MemoryStream();
await ms.WriteAsync("<foo xmlns='bar'><baz/></foo>".GetBytes());
ms.Position = 0;

using var parser = new XmppParser(() => ms);

Element el = default!;

parser.OnStreamElement += e =>
{
el = e;
return Task.CompletedTask;
};

while (el == null && await parser)
{
Console.WriteLine("parser::advance(): true");
await Task.Delay(1);
}

Console.WriteLine("parser::advance(): false");
Console.WriteLine(el);

Console.WriteLine("XML:\n" + el!.ToString(XmlFormatting.Indented));
}
}
6 changes: 5 additions & 1 deletion XmppSharp/Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Runtime.CompilerServices;
using System.Text;

namespace XmppSharp;

Expand Down Expand Up @@ -29,6 +30,9 @@ public static string ToHex(this byte[] bytes, bool lowercase = true)
return result.ToLowerInvariant();
}

public static TaskAwaiter<bool> GetAwaiter(this XmppParser parser)
=> parser.AdvanceAsync().GetAwaiter();

public static byte[] FromHex(this string str)
=> Convert.FromHexString(str);

Expand Down
8 changes: 6 additions & 2 deletions XmppSharp/XmppParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;
using System.Xml.Schema;
using XmppSharp.Exceptions;
Expand Down Expand Up @@ -171,7 +172,10 @@ public int Depth
}
}

public async Task<bool> Advance()
public bool Advance()
=> AdvanceAsync().GetAwaiter().GetResult();

public async Task<bool> AdvanceAsync()
{
if (this._disposed)
return false;
Expand Down

0 comments on commit 9da6d85

Please sign in to comment.