|
| 1 | +// Copyright (c) .NET Foundation and contributors. All rights reserved. |
| 2 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 3 | + |
| 4 | +using System.Reflection; |
| 5 | +using FluentAssertions; |
| 6 | +using Xunit; |
| 7 | +using System.CommandLine.Parsing; |
| 8 | +using static System.Runtime.InteropServices.JavaScript.JSType; |
| 9 | + |
| 10 | +namespace System.CommandLine.Subsystems.Tests |
| 11 | +{ |
| 12 | + public class ErrorReportingSubsystemTests |
| 13 | + { |
| 14 | + [Fact] |
| 15 | + public void Report_when_single_error_writes_to_console_hack() |
| 16 | + { |
| 17 | + var error = new ParseError("a sweet error message"); |
| 18 | + var errors = new List<ParseError> { error }; |
| 19 | + var errorSubsystem = new ErrorReportingSubsystem(); |
| 20 | + var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
| 21 | + |
| 22 | + errorSubsystem.Report(consoleHack, errors); |
| 23 | + |
| 24 | + consoleHack.GetBuffer().Trim().Should().Be(error.Message); |
| 25 | + } |
| 26 | + |
| 27 | + [Fact] |
| 28 | + public void Report_when_multiple_error_writes_to_console_hack() |
| 29 | + { |
| 30 | + var error = new ParseError("a sweet error message"); |
| 31 | + var anotherError = new ParseError("another sweet error message"); |
| 32 | + var errors = new List<ParseError> { error, anotherError }; |
| 33 | + var errorSubsystem = new ErrorReportingSubsystem(); |
| 34 | + var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
| 35 | + |
| 36 | + errorSubsystem.Report(consoleHack, errors); |
| 37 | + |
| 38 | + consoleHack.GetBuffer().Trim().Should().Be($"{error.Message}\r\n{anotherError.Message}"); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void Report_when_no_errors_writes_nothing_to_console_hack() |
| 43 | + { |
| 44 | + var errors = new List<ParseError> { }; |
| 45 | + var errorSubsystem = new ErrorReportingSubsystem(); |
| 46 | + var consoleHack = new ConsoleHack().RedirectToBuffer(true); |
| 47 | + |
| 48 | + errorSubsystem.Report(consoleHack, errors); |
| 49 | + |
| 50 | + consoleHack.GetBuffer().Trim().Should().Be(""); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory] |
| 54 | + [InlineData("-v", false)] |
| 55 | + [InlineData("-x", true)] |
| 56 | + [InlineData("", false)] |
| 57 | + public void GetIsActivated_tests(string input, bool result) |
| 58 | + { |
| 59 | + var rootCommand = new CliRootCommand {new CliOption<bool>("-v")}; |
| 60 | + var configuration = new CliConfiguration(rootCommand); |
| 61 | + var errorSubsystem = new ErrorReportingSubsystem(); |
| 62 | + Subsystem.Initialize(errorSubsystem, configuration); |
| 63 | + |
| 64 | + var parseResult = CliParser.Parse(rootCommand, input, configuration); |
| 65 | + var isActive = Subsystem.GetIsActivated(errorSubsystem, parseResult); |
| 66 | + |
| 67 | + isActive.Should().Be(result); |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments