Skip to content

[no-merge] Add unit tests for Issue389 #392

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/CommandLine.Tests/Unit/Issue389_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using CommandLine.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace CommandLine.Tests.Unit
{

public class Issue389_Tests
{

private const int ERROR_SUCCESS = 0;

// Test method (xUnit) which fails
[Fact]
public void CallMain_GiveHelpArgument_ExpectSuccess()
{
var result = Program.__Main(new[] { "--help" });

Assert.Equal(ERROR_SUCCESS, result);
}

// main program
internal class Program
{


internal static int __Main(string[] args)
{
bool hasError = false;
bool helpOrVersionRequested = false;

ParserResult<Options> parsedOptions = Parser.Default.ParseArguments<Options>(args)
.WithNotParsed(errors => {
helpOrVersionRequested = errors.Any(
x => x.Tag == ErrorType.HelpRequestedError
|| x.Tag == ErrorType.VersionRequestedError);
hasError = true;
});

if(helpOrVersionRequested)
{
return ERROR_SUCCESS;
}

// Execute as a normal call
// ...
return ERROR_SUCCESS;
}

}

// Options
internal class Options
{

[Option('c', "connectionString", Required = true, HelpText = "Texts.ExplainConnection")]
public string ConnectionString { get; set; }

[Option('j', "jobId", Required = true, HelpText = "Texts.ExplainJob")]
public int JobId { get; set; }

[Usage(ApplicationAlias = "Importer.exe")]
public static IEnumerable<Example> Examples
{
get => new[] {
new Example("Texts.ExplainExampleExecution", new Options() {
ConnectionString="Server=MyServer;Database=MyDatabase",
JobId = 5
}),
};
}

}
}
}