Files
ScadaBridge/tests/ZB.MOM.WW.ScadaBridge.CLI.Tests/OutputFormatterTests.cs
T
Joseph Doherty 513fa1aa16 docs(tests): fix CommentChecker findings across tests/ — 13,392 → 0 (repo-wide scan clean)
Comment/XML-doc changes only:
- add summaries to ~10k test methods, fixtures, fakes and helpers
  (+ <param> on theories, <returns> on async Task methods,
  <inheritdoc/> on interface implementations the checker can resolve)
- strip ~2.6k project-tracking references (WP*, P*-*, row-NN, Task N,
  #NN, Component-0NN) from comments, keeping the explanatory text;
  reword false positives in comments only
- real docs on fakes implementing BCL/library interfaces
  (IDisposable, ILogger, IOptionsMonitor, IAsyncLifetime)
- reword the last 5 src/ false positives (CWE-1236, failures-1,
  year-0001, M365)

Only non-comment edits: four one-line nested classes split across lines
so members can carry docs. Build 0 errors / 0 warnings.
2026-10-07 15:05:49 -04:00

109 lines
3.5 KiB
C#

using ZB.MOM.WW.ScadaBridge.CLI;
namespace ZB.MOM.WW.ScadaBridge.CLI.Tests;
[Collection("Console")]
public class OutputFormatterTests
{
/// <summary>Verifies that JSON output is written with indentation.</summary>
[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 });
}
/// <summary>Verifies that an error message is written to standard error.</summary>
[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 });
}
/// <summary>Verifies that a table render includes the headers and all rows.</summary>
[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 });
}
/// <summary>Verifies that a table with no rows renders only its headers.</summary>
[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 });
}
/// <summary>Verifies that table column widths adjust to the widest cell content.</summary>
[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 });
}
}