- Add JoeAppEngine folder to OPC UA nodes.json (BTCS, AlarmCntsBySeverity, Scheduler/ScanTime) - Fix DataConnectionActor: capture Self in PreStart for use from non-actor threads, preventing Self.Tell failure in Disconnected event handler - Implement InstanceActor.HandleConnectionQualityChanged to mark attributes Bad on disconnect - Fix LmxFakeProxy TagMapper to serialize arrays as JSON instead of "System.Int32[]" - Allow DataType and DataSourceReference updates in TemplateService.UpdateAttributeAsync - Update test_infra_opcua.md with JoeAppEngine documentation
103 lines
3.1 KiB
C#
103 lines
3.1 KiB
C#
using ScadaLink.CLI;
|
|
|
|
namespace ScadaLink.CLI.Tests;
|
|
|
|
public class OutputFormatterTests
|
|
{
|
|
[Fact]
|
|
public void WriteJson_WritesIndentedJson()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
OutputFormatter.WriteJson(new { Name = "test", Value = 42 });
|
|
|
|
var output = writer.ToString().Trim();
|
|
Assert.Contains("\"name\"", output);
|
|
Assert.Contains("\"value\": 42", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteError_WritesToStdErr()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetError(writer);
|
|
|
|
OutputFormatter.WriteError("something went wrong", "ERR_CODE");
|
|
|
|
var output = writer.ToString().Trim();
|
|
Assert.Contains("something went wrong", output);
|
|
Assert.Contains("ERR_CODE", output);
|
|
|
|
Console.SetError(new StreamWriter(Console.OpenStandardError()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteTable_RendersHeadersAndRows()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var headers = new[] { "Id", "Name", "Status" };
|
|
var rows = new List<string[]>
|
|
{
|
|
new[] { "1", "Alpha", "Active" },
|
|
new[] { "2", "Beta", "Inactive" }
|
|
};
|
|
|
|
OutputFormatter.WriteTable(rows, headers);
|
|
|
|
var output = writer.ToString();
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Name", output);
|
|
Assert.Contains("Status", output);
|
|
Assert.Contains("Alpha", output);
|
|
Assert.Contains("Beta", output);
|
|
Assert.Contains("Inactive", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteTable_EmptyRows_ShowsHeadersOnly()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var headers = new[] { "Id", "Name" };
|
|
OutputFormatter.WriteTable(Array.Empty<string[]>(), headers);
|
|
|
|
var output = writer.ToString();
|
|
Assert.Contains("Id", output);
|
|
Assert.Contains("Name", output);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
|
|
[Fact]
|
|
public void WriteTable_ColumnWidthsAdjustToContent()
|
|
{
|
|
var writer = new StringWriter();
|
|
Console.SetOut(writer);
|
|
|
|
var headers = new[] { "X", "LongColumnName" };
|
|
var rows = new List<string[]>
|
|
{
|
|
new[] { "ShortValue", "Y" }
|
|
};
|
|
|
|
OutputFormatter.WriteTable(rows, headers);
|
|
|
|
var lines = writer.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
|
|
// Header line: "X" should be padded to at least "ShortValue" width
|
|
Assert.True(lines.Length >= 2);
|
|
// The "X" column header should be padded wider than 1 character
|
|
var headerLine = lines[0];
|
|
Assert.True(headerLine.IndexOf("LongColumnName") > 1);
|
|
|
|
Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = true });
|
|
}
|
|
}
|