add InsinMessageFormatter

This commit is contained in:
EugeneTes
2026-07-23 07:39:52 +00:00
parent f8e5515050
commit d01efdd8c2
2 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
using Inspectron.Epson.PrintServer.Telemetry;
namespace Inspectron.Epson.Tests.Telemetry;
public class InsinMessageFormatterTests
{
[Fact]
public void Format_single_pair_no_spaces()
{
var result = InsinMessageFormatter.Format(("printer", "192.168.1.10"));
Assert.Equal("printer=192.168.1.10", result);
}
[Fact]
public void Format_multiple_pairs_joined_by_space()
{
var result = InsinMessageFormatter.Format(
("printer", "192.168.1.10"),
("jobId", "42"));
Assert.Equal("printer=192.168.1.10 jobId=42", result);
}
[Fact]
public void Format_quotes_value_containing_space()
{
var result = InsinMessageFormatter.Format(("error", "paper end detected"));
Assert.Equal("error=\"paper end detected\"", result);
}
[Fact]
public void Format_escapes_embedded_double_quote()
{
var result = InsinMessageFormatter.Format(("error", "he said \"nope\""));
Assert.Equal("error=\"he said \\\"nope\\\"\"", result);
}
[Fact]
public void Format_treats_null_or_empty_value_as_empty_string()
{
var result = InsinMessageFormatter.Format(("model", (string?)null), ("name", ""));
Assert.Equal("model= name=", result);
}
[Fact]
public void Format_omits_pairs_with_null_or_empty_key()
{
var result = InsinMessageFormatter.Format(
("", "ignored"),
("printer", "192.168.1.10"));
Assert.Equal("printer=192.168.1.10", result);
}
}

View File

@@ -0,0 +1,26 @@
using System.Text;
namespace Inspectron.Epson.PrintServer.Telemetry;
public static class InsinMessageFormatter
{
public static string Format(params (string Key, string? Value)[] pairs)
{
var sb = new StringBuilder();
foreach (var (key, value) in pairs)
{
if (string.IsNullOrEmpty(key)) continue;
if (sb.Length > 0) sb.Append(' ');
sb.Append(key).Append('=').Append(FormatValue(value));
}
return sb.ToString();
}
private static string FormatValue(string? value)
{
if (string.IsNullOrEmpty(value)) return string.Empty;
if (value.IndexOf(' ') < 0 && value.IndexOf('"') < 0) return value;
var escaped = value.Replace("\\", "\\\\").Replace("\"", "\\\"");
return $"\"{escaped}\"";
}
}