Skip to content

Commit eaf7de6

Browse files
committed
Add additional tests to cover the case where TryParse encounters an exception.
Depending on the LogParseErrorToConsole flag we should or should not write to console.
1 parent 5b284ce commit eaf7de6

1 file changed

Lines changed: 56 additions & 1 deletion

File tree

test/CommandLineTests.Negative.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using CommandLine.ColorScheme;
2+
using System;
23
using System.IO;
34
using Xunit;
45

@@ -141,5 +142,59 @@ public void MismatchedQuotes()
141142
var exception = Assert.Throws<ParserException>(() => Helpers.Parse<object>("@\" foo "));
142143
Assert.IsType<FileNotFoundException>(exception.InnerException);
143144
}
145+
146+
[Trait("Category", "Negative")]
147+
[Fact]
148+
public void TryParseWithLoggingToConsole()
149+
{
150+
TestWriter _printer = new TestWriter();
151+
ParserOptions parserOptions = new ParserOptions() { LogParseErrorToConsole = true };
152+
IColors color = Parser.ColorScheme.Get();
153+
OutputColorizer.Colorizer.SetupWriter(_printer);
154+
bool value = Parser.TryParse("foo", out Options1 options, parserOptions);
155+
Assert.False(value);
156+
157+
Validate(_printer,
158+
new TextAndColor(color.ErrorColor, "Error"),
159+
new TextAndColor(_printer.ForegroundColor, $": Not all required arguments have been specified {Environment.NewLine}"),
160+
new TextAndColor(_printer.ForegroundColor, "Usage: "),
161+
new TextAndColor(_printer.ForegroundColor, " "),
162+
new TextAndColor(color.AssemblyNameColor, "testhost.exe"),
163+
new TextAndColor(_printer.ForegroundColor, " "),
164+
new TextAndColor(color.RequiredArgumentColor, "p1"),
165+
new TextAndColor(_printer.ForegroundColor, " "),
166+
new TextAndColor(color.RequiredArgumentColor, "p2"),
167+
new TextAndColor(_printer.ForegroundColor, " "),
168+
new TextAndColor(_printer.ForegroundColor, "[-"),
169+
new TextAndColor(color.OptionalArgumentColor, "opt1"),
170+
new TextAndColor(_printer.ForegroundColor, " value] "),
171+
new TextAndColor(_printer.ForegroundColor, "[-"),
172+
new TextAndColor(color.OptionalArgumentColor, "opt2"),
173+
new TextAndColor(_printer.ForegroundColor, " value] "),
174+
new TextAndColor(_printer.ForegroundColor, "[-"),
175+
new TextAndColor(color.OptionalArgumentColor, "opt3"),
176+
new TextAndColor(_printer.ForegroundColor, " value] "),
177+
new TextAndColor(_printer.ForegroundColor, "[-"),
178+
new TextAndColor(color.OptionalArgumentColor, "opt4"),
179+
new TextAndColor(_printer.ForegroundColor, " value] "),
180+
new TextAndColor(_printer.ForegroundColor, "For detailed information run '"),
181+
new TextAndColor(color.AssemblyNameColor, "testhost --help"),
182+
new TextAndColor(_printer.ForegroundColor, "'.")
183+
);
184+
}
185+
186+
[Trait("Category", "Negative")]
187+
[Fact]
188+
public void TryParseWithoutLoggingToConsole()
189+
{
190+
TestWriter _printer = new TestWriter();
191+
ParserOptions parserOptions = new ParserOptions() { LogParseErrorToConsole = false };
192+
IColors color = Parser.ColorScheme.Get();
193+
OutputColorizer.Colorizer.SetupWriter(_printer);
194+
bool value = Parser.TryParse("foo", out Options1 options, parserOptions);
195+
Assert.False(value);
196+
197+
Validate(_printer);
198+
}
144199
}
145200
}

0 commit comments

Comments
 (0)