-
Notifications
You must be signed in to change notification settings - Fork 18
Add readable pretty-JSON output and validation tests #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aaea656
feat: add jsonpretty output mode and validation tests
mattleibow de643ee
test: add validation tests for OutputHelper
mattleibow 5e12081
chore(tests): enhance OutputHelper tests with exact string matching a…
mattleibow f0501ab
test: add missing JsonPretty coverage and support hyphenated alias
mattleibow a3be9e0
fix: prevent NullReferenceException in OutputFormatTypeConverter
mattleibow 0640a34
Refactor OutputHelper tests to use AnsiConsole capture for all formats
mattleibow 7b37e49
fix it
mattleibow 765ae50
Fix missing Spectre.Console using in OutputHelper tests
mattleibow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| #nullable enable | ||
| using AndroidSdk.Tool; | ||
| using Xunit; | ||
|
|
||
| namespace AndroidSdk.Tests; | ||
|
|
||
| public class CommandSettingsValidation_Tests | ||
| { | ||
| [Fact] | ||
| public void AvdDeleteCommandSettingsRequireName() | ||
| { | ||
| var settings = new AvdDeleteCommandSettings { Name = "" }; | ||
|
|
||
| var result = settings.Validate(); | ||
|
|
||
| Assert.False(result.Successful); | ||
| Assert.Contains("--name", result.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void AvdStartCommandSettingsRequireName() | ||
| { | ||
| var settings = new AvdStartCommandSettings { Name = "" }; | ||
|
|
||
| var result = settings.Validate(); | ||
|
|
||
| Assert.False(result.Successful); | ||
| Assert.Contains("--name", result.Message); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void OutputFormatTypeConverterSupportsJsonPretty() | ||
| { | ||
| var converter = new OutputFormatTypeConverter(); | ||
|
|
||
| var format = converter.ConvertFrom("jsonpretty"); | ||
|
|
||
| Assert.Equal(OutputFormat.JsonPretty, format); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void OutputFormatTypeConverterSupportsJsonPrettyWithHyphen() | ||
| { | ||
| var converter = new OutputFormatTypeConverter(); | ||
|
|
||
| var format = converter.ConvertFrom("json-pretty"); | ||
|
|
||
| Assert.Equal(OutputFormat.JsonPretty, format); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,250 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Spectre.Console; | ||
| using Xunit; | ||
| using AndroidSdk.Tool; | ||
|
|
||
| namespace AndroidSdk.Tests; | ||
|
|
||
| public class OutputHelper_Tests | ||
| { | ||
|
mattleibow marked this conversation as resolved.
|
||
| public class TestData | ||
| { | ||
| public string Name { get; set; } | ||
| public int Value { get; set; } | ||
|
|
||
| public override string ToString() => $"{Name}:{Value}"; | ||
|
|
||
| // Needed for XmlSerializer deserialization if we were doing that, but for serialization strictly not needed | ||
| // unless we want to be safe. Public is required. | ||
| public TestData() { } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Data_Json_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "Test", Value = 123 }; | ||
| // OutputHelper.Output(data, format) uses Console.Write, so no trailing newline | ||
| var expected = "{\"Name\":\"Test\",\"Value\":123}"; | ||
|
|
||
| var actual = CaptureOutput(data, OutputFormat.Json); | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Data_JsonPretty_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "Test", Value = 123 }; | ||
|
|
||
| var actual = CaptureOutput(data, OutputFormat.JsonPretty); | ||
|
|
||
| Assert.Contains("\"Name\": \"Test\"", actual); | ||
| Assert.Contains("\"Value\": 123", actual); | ||
| // Console.Write used, so maybe no newline at very end? | ||
| // But Json.NET indented adds newlines between properties. | ||
| Assert.StartsWith("{", actual); | ||
| Assert.EndsWith("}", actual); | ||
| } | ||
|
|
||
| [Fact] | ||
|
mattleibow marked this conversation as resolved.
|
||
| public void Output_Data_Xml_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "Test", Value = 123 }; | ||
|
|
||
| var actual = CaptureOutput(data, OutputFormat.Xml); | ||
|
|
||
| Assert.Contains("<?xml version=\"1.0\" encoding=\"utf-16\"?>", actual); | ||
| Assert.Contains("<TestData", actual); | ||
| Assert.Contains("<Name>Test</Name>", actual); | ||
| Assert.Contains("<Value>123</Value>", actual); | ||
| Assert.Contains("</TestData>", actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Items_None_Formats_Correctly() | ||
| { | ||
| var data = new[] | ||
| { | ||
| new TestData { Name = "A", Value = 1 }, | ||
| new TestData { Name = "B", Value = 2 } | ||
| }; | ||
| // Items with None format iterates and prints ToString() + NewLine | ||
| var expected = "A:1" + Environment.NewLine + "B:2" + Environment.NewLine; | ||
|
|
||
| var actual = CaptureOutput(data, OutputFormat.None); | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Items_Json_Formats_Correctly() | ||
| { | ||
| var data = new[] { new TestData { Name = "A", Value = 1 } }; | ||
| var expected = "[{\"Name\":\"A\",\"Value\":1}]" + Environment.NewLine; | ||
|
|
||
| var actual = CaptureOutputItems(data, OutputFormat.Json); | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Items_JsonPretty_Formats_Correctly() | ||
| { | ||
| var data = new[] { new TestData { Name = "A", Value = 1 } }; | ||
|
|
||
| var actual = CaptureOutputItems(data, OutputFormat.JsonPretty); | ||
|
|
||
| Assert.Contains("\"Name\": \"A\"", actual); | ||
| Assert.Contains("\"Value\": 1", actual); | ||
| Assert.StartsWith("[", actual.TrimStart()); | ||
| Assert.EndsWith("]", actual.TrimEnd()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Item_JsonPretty_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "A", Value = 1 }; | ||
|
|
||
| var actual = CaptureOutputItem(data, OutputFormat.JsonPretty); | ||
|
|
||
| Assert.Contains("\"Name\": \"A\"", actual); | ||
| Assert.Contains("\"Value\": 1", actual); | ||
| Assert.StartsWith("{", actual.TrimStart()); | ||
| Assert.EndsWith("}", actual.TrimEnd()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Item_Json_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "A", Value = 1 }; | ||
| var expected = "{\"Name\":\"A\",\"Value\":1}" + Environment.NewLine; | ||
|
|
||
| var actual = CaptureOutputItem(data, OutputFormat.Json); | ||
| Assert.Equal(expected, actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Items_Table_Formats_Correctly() | ||
| { | ||
| var data = new[] { new TestData { Name = "A", Value = 1 } }; | ||
|
|
||
| var actual = CaptureOutputItems(data, OutputFormat.None); | ||
|
|
||
| Assert.Contains("Name", actual); | ||
| Assert.Contains("Value", actual); | ||
| Assert.Contains("A", actual); | ||
| Assert.Contains("1", actual); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Output_Item_Table_Formats_Correctly() | ||
| { | ||
| var data = new TestData { Name = "A", Value = 1 }; | ||
|
|
||
| var actual = CaptureOutputItem(data, OutputFormat.None); | ||
|
|
||
| Assert.Contains("Property", actual); | ||
| Assert.Contains("Value", actual); | ||
| Assert.Contains("Name", actual); | ||
| Assert.Contains("A", actual); | ||
| Assert.Contains("1", actual); | ||
| } | ||
|
|
||
| private string CaptureOutput<T>(T data, OutputFormat format) | ||
| { | ||
| var sw = new StringWriter(); | ||
|
|
||
| // Capture Console.Out (for Json/Xml/None) | ||
| var oldOut = Console.Out; | ||
| Console.SetOut(sw); | ||
|
|
||
| // Also capture AnsiConsole (for Table/None that might use it) | ||
| var oldConsole = AnsiConsole.Console; | ||
| AnsiConsole.Console = AnsiConsole.Create(new AnsiConsoleSettings | ||
| { | ||
| Ansi = AnsiSupport.No, | ||
| ColorSystem = ColorSystemSupport.NoColors, | ||
| Out = new AnsiConsoleOutput(sw), | ||
| Interactive = InteractionSupport.No, | ||
| Enrichment = new ProfileEnrichment { UseDefaultEnrichers = false } | ||
| }); | ||
|
|
||
| try | ||
| { | ||
| OutputHelper.Output(data, format); | ||
| return sw.ToString(); | ||
| } | ||
| finally | ||
| { | ||
| Console.SetOut(oldOut); | ||
| AnsiConsole.Console = oldConsole; | ||
| } | ||
| } | ||
|
|
||
| private string CaptureOutputItems<T>(IEnumerable<T> data, OutputFormat format) | ||
| { | ||
| var sw = new StringWriter(); | ||
|
|
||
| var oldOut = Console.Out; | ||
| Console.SetOut(sw); | ||
|
|
||
| var oldConsole = AnsiConsole.Console; | ||
| AnsiConsole.Console = AnsiConsole.Create(new AnsiConsoleSettings | ||
| { | ||
| Ansi = AnsiSupport.No, | ||
| ColorSystem = ColorSystemSupport.NoColors, | ||
| Out = new AnsiConsoleOutput(sw), | ||
| Interactive = InteractionSupport.No, | ||
| Enrichment = new ProfileEnrichment { UseDefaultEnrichers = false } | ||
| }); | ||
|
|
||
| try | ||
| { | ||
| // Explicitly call the IEnumerable overload | ||
| OutputHelper.Output<T>(data, format, new[] { "Name", "Value" }, i => | ||
| { | ||
| var p = i as TestData; | ||
| return p != null ? new[] { p.Name, p.Value.ToString() } : new[] { i.ToString(), "" }; | ||
| }); | ||
| sw.Flush(); | ||
| return sw.ToString(); | ||
| } | ||
| finally | ||
| { | ||
| Console.SetOut(oldOut); | ||
| AnsiConsole.Console = oldConsole; | ||
| } | ||
| } | ||
|
|
||
| private string CaptureOutputItem<T>(T data, OutputFormat format) | ||
| { | ||
| var sw = new StringWriter(); | ||
|
|
||
| var oldOut = Console.Out; | ||
| Console.SetOut(sw); | ||
|
|
||
| var oldConsole = AnsiConsole.Console; | ||
| AnsiConsole.Console = AnsiConsole.Create(new AnsiConsoleSettings | ||
| { | ||
| Ansi = AnsiSupport.No, | ||
| ColorSystem = ColorSystemSupport.NoColors, | ||
| Out = new AnsiConsoleOutput(sw), | ||
| Interactive = InteractionSupport.No, | ||
| Enrichment = new ProfileEnrichment { UseDefaultEnrichers = false } | ||
| }); | ||
|
|
||
| try | ||
| { | ||
| OutputHelper.Output(data, format, new[] { "Name", "Value" }, i => | ||
| { | ||
| var p = i as TestData; | ||
| return p != null ? new[] { p.Name, p.Value.ToString() } : new[] { i.ToString(), "" }; | ||
| }); | ||
| return sw.ToString(); | ||
| } | ||
| finally | ||
| { | ||
| Console.SetOut(oldOut); | ||
| AnsiConsole.Console = oldConsole; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.