Skip to content

Commit

Permalink
Add UnexpectedResponseHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Oct 26, 2024
1 parent acdd483 commit 7fafa45
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,17 @@ protected bool ShouldReleaseHeldContent ()
State = AnsiResponseParserState.Normal;

// Maybe swallow anyway if user has custom delegate
return ShouldReleaseUnexpectedResponse ();
var swallow = ShouldSwallowUnexpectedResponse ();

if (swallow)
{
heldContent.ClearHeld ();
// Do not send back to input stream
return false;
}

// Do release back to input stream
return true;
}

return false; // Continue accumulating
Expand All @@ -217,7 +227,7 @@ protected bool ShouldReleaseHeldContent ()
/// Based on <see cref="_knownTerminators"/></remarks>
/// </summary>
/// <returns></returns>
protected abstract bool ShouldReleaseUnexpectedResponse ();
protected abstract bool ShouldSwallowUnexpectedResponse ();

private bool MatchResponse (string cur, List<AnsiResponseExpectation> collection, bool invokeCallback, bool removeExpectation)
{
Expand Down Expand Up @@ -289,7 +299,7 @@ public AnsiResponseParser () : base (new GenericHeld<T> ()) { }


/// <inheritdoc cref="AnsiResponseParser.UnknownResponseHandler"/>
public Func<IEnumerable<Tuple<char, T>>, bool> UnknownResponseHandler { get; set; } = (_) => false;
public Func<IEnumerable<Tuple<char, T>>, bool> UnexpectedResponseHandler { get; set; } = (_) => false;


public IEnumerable<Tuple<char, T>> ProcessInput (params Tuple<char, T> [] input)
Expand Down Expand Up @@ -340,9 +350,9 @@ public void ExpectResponseT (string terminator, Action<IEnumerable<Tuple<char,T>
}

/// <inheritdoc />
protected override bool ShouldReleaseUnexpectedResponse ()
protected override bool ShouldSwallowUnexpectedResponse ()
{
return !UnknownResponseHandler.Invoke (HeldToEnumerable ());
return UnexpectedResponseHandler.Invoke (HeldToEnumerable ());
}
}

Expand Down Expand Up @@ -385,8 +395,8 @@ public string Release ()
}

/// <inheritdoc />
protected override bool ShouldReleaseUnexpectedResponse ()
protected override bool ShouldSwallowUnexpectedResponse ()
{
return !UnknownResponseHandler.Invoke (heldContent.ToString () ?? string.Empty);
return UnknownResponseHandler.Invoke (heldContent.HeldToString ());
}
}
43 changes: 42 additions & 1 deletion UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ public void ShouldSwallowUnknownResponses_WhenDelegateSaysSo ()
int i = 0;

Check warning on line 421 in UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs

View workflow job for this annotation

GitHub Actions / build_release

The variable 'i' is assigned but its value is never used

// Swallow all unknown escape codes
_parser1.UnknownResponseHandler = _ => true;
_parser1.UnexpectedResponseHandler = _ => true;
_parser2.UnknownResponseHandler = _ => true;


Expand All @@ -439,6 +439,47 @@ public void ShouldSwallowUnknownResponses_WhenDelegateSaysSo ()
29);
}

[Fact]
public void UnknownResponses_ParameterShouldMatch ()
{
int i = 0;

Check warning on line 445 in UnitTests/ConsoleDrivers/AnsiResponseParserTests.cs

View workflow job for this annotation

GitHub Actions / build_release

The variable 'i' is assigned but its value is never used

// Track unknown responses passed to the UnexpectedResponseHandler
var unknownResponses = new List<string> ();

// Set up the UnexpectedResponseHandler to log each unknown response
_parser1.UnexpectedResponseHandler = r1 =>
{
unknownResponses.Add (BatchToString (r1));
return true; // Return true to swallow unknown responses
};

_parser2.UnknownResponseHandler = r2 =>
{
// parsers should be agreeing on what these responses are!
Assert.Equal(unknownResponses.Last(),r2);
return true; // Return true to swallow unknown responses
};

// Input with known and unknown responses
AssertReleased (
"Just te\u001b[<0;0;0M\u001b[3c\u001b[2c\u001b[4cst",
"Just test");

// Expected unknown responses (ANSI sequences that are unknown)
var expectedUnknownResponses = new List<string>
{
"\u001b[<0;0;0M",
"\u001b[3c",
"\u001b[2c",
"\u001b[4c"
};

// Assert that the UnexpectedResponseHandler was called with the correct unknown responses
Assert.Equal (expectedUnknownResponses.Count, unknownResponses.Count);
Assert.Equal (expectedUnknownResponses, unknownResponses);
}

private Tuple<char, int> [] StringToBatch (string batch)
{
return batch.Select ((k) => Tuple.Create (k, tIndex++)).ToArray ();
Expand Down

0 comments on commit 7fafa45

Please sign in to comment.