Skip to content

Commit

Permalink
Merge pull request #11 from henkla/feature/unit-tests
Browse files Browse the repository at this point in the history
add unit tests
  • Loading branch information
henkla authored Oct 6, 2024
2 parents d8d33d8 + 801251e commit 5a6b316
Show file tree
Hide file tree
Showing 18 changed files with 742 additions and 15 deletions.
314 changes: 314 additions & 0 deletions Simple.ArgumentParser.Tests/ArgsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,314 @@
namespace Simple.ArgumentParser.Tests;

public class ArgsTests
{
[Fact]
public void IsValid_should_return_true_if_no_missing_or_invalid_arguments()
{
// arrange
// act

var args = new Args();

// assert

Assert.True(args.IsValid);
}

[Fact]
public void IsValid_should_return_false_if_any_missing_argument_exists()
{
// arrange
// act

var args = new Args();
args.Missing.Add("missing-argument");

// assert

Assert.False(args.IsValid);
}

[Fact]
public void IsValid_should_return_false_if_any_invalid_argument_exists()
{
// arrange
// act

var args = new Args();
args.Invalid.Add("invalid-argument");

// assert

Assert.False(args.IsValid);
}

[Fact]
public void HasIgnoredCommands_should_return_true_if_any_ignored_commands_exists()
{
// arrange
// act

var args = new Args();
args.Ignored.Add(new Command { Name = "ignored-command" });

// assert

Assert.True(args.HasIgnoredCommands);
}

[Fact]
public void HasIgnoredCommands_should_return_false_if_no_ignored_commands_exists()
{
// arrange
// act

var args = new Args();

// assert

Assert.False(args.HasIgnoredCommands);
}

[Fact]
public void HasMissingCommands_should_return_true_if_any_missing_commands_exists()
{
// arrange
// act

var args = new Args();
args.Missing.Add("missing-command");

// assert

Assert.True(args.HasMissingCommands);
}

[Fact]
public void HasMissingCommands_should_return_false_if_no_missing_commands_exists()
{
// arrange
// act

var args = new Args();

// assert

Assert.False(args.HasMissingCommands);
}

[Fact]
public void HasInvalidCommands_should_return_true_if_any_invalid_commands_exists()
{
// arrange
// act

var args = new Args();
args.Invalid.Add("invalid-command");

// assert

Assert.True(args.HasInvalidCommands);
}

[Fact]
public void HasInvalidCommands_should_return_false_if_no_invalid_commands_exists()
{
// arrange
// act

var args = new Args();

// assert

Assert.False(args.HasInvalidCommands);
}

[Fact]
public void HelpRequested_should_return_true_if_help_flag_is_specified()
{
// arrange
// act

var args = new ArgumentParser().AddHelpOption().Parse(["--help"]);

// assert

Assert.True(args.HelpRequested);
}

[Fact]
public void HelpRequested_should_return_false_if_help_flag_specified()
{
// arrange
// act

var args = new ArgumentParser().AddHelpOption().Parse(["--not-help"]);

// assert

Assert.False(args.HelpRequested);
}

[Fact]
public void VersionRequested_should_return_true_if_version_flag_is_specified()
{
// arrange
// act

var args = new ArgumentParser().AddVersionOption().Parse(["--version"]);

// assert

Assert.True(args.VersionRequested);
}

[Fact]
public void HelpRequested_should_return_false_if_version_flag_is_specified()
{
// arrange
// act

var args = new ArgumentParser().AddVersionOption().Parse(["--not-version"]);

// assert

Assert.False(args.VersionRequested);
}

[Fact]
public void HelpSection_is_not_empty_if_help_flag_is_specified()
{
// arrange
// act

var args = new ArgumentParser().AddHelpOption().Parse(["--help"]);

// assert

Assert.NotNull(args.HelpSection);
Assert.NotEmpty(args.HelpSection);
}

[Fact]
public void HelpSection_is_empty_if_help_flag_is_not_specified()
{
// arrange
// act

var args = new ArgumentParser().AddHelpOption().Parse(["--not-help"]);

// assert

Assert.Null(args.HelpSection);
}

[Fact]
public void Version_is_not_empty_if_version_flag_is_specified()
{
// arrange
// act

var args = new ArgumentParser().AddVersionOption().Parse(["--version"]);

// assert

Assert.NotNull(args.Version);
Assert.NotEmpty(args.Version);
}

[Fact]
public void Version_is_empty_if_version_flag_is_not_specified()
{
// arrange
// act

var args = new ArgumentParser().AddVersionOption().Parse(["--not-version"]);

// assert

Assert.Null(args.Version);
}

[Fact]
public void Any_returns_true_if_any_valid_command_exists()
{
// arrange
// act

var args = new Args { Valid = { new Command { Name = "valid-command" } } };

// assert

Assert.True(args.Any());
}

[Fact]
public void Any_returns_false_if_no_valid_command_exists()
{
// arrange
// act

var args = new Args();

// assert

Assert.False(args.Any());
}

[Fact]
public void Get_returns_matching_command_if_valid_key()
{
// arrange

var args = new Args { Valid = { new Command { Name = "valid-command" } } };

// act

var result = args.Get("valid-command");

// assert

Assert.NotNull(result);
Assert.Equal(result.Name, "valid-command");

Check warning on line 273 in Simple.ArgumentParser.Tests/ArgsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

The literal or constant value "valid-command" should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'Get_returns_matching_command_if_valid_key' on type 'ArgsTests'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)
}

[Fact]
public void Get_returns_null_if_invalid_key()
{
// arrange

var args = new Args { Valid = { new Command { Name = "valid-command" } } };

// act

var result = args.Get("invalid-key");

// assert

Assert.Null(result);
}

[Fact]
public void GetAll_returns_all_valid_commands()
{
// arrange

var args = new Args { Valid =
{
new Command { Name = "valid-command-1" },
new Command { Name = "valid-command-2" },
new Command { Name = "valid-command-3" }
} };

// act

var result = args.GetAll();

// assert

Assert.NotNull(result);
Assert.NotEmpty(result);
Assert.Equal(result.Count, 3);

Check warning on line 312 in Simple.ArgumentParser.Tests/ArgsTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

The literal or constant value 3 should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'GetAll_returns_all_valid_commands' on type 'ArgsTests'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)
}
}
3 changes: 3 additions & 0 deletions Simple.ArgumentParser.Tests/ArgumentParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public void Parse_should_handle_expected_arguments()
Assert.False(result.HasMissingCommands);
Assert.False(result.HasIgnoredCommands);
Assert.False(result.HasInvalidCommands);
Assert.Equal(result.Valid.Count, 9);

Check warning on line 46 in Simple.ArgumentParser.Tests/ArgumentParserTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

The literal or constant value 9 should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'Parse_should_handle_expected_arguments' on type 'ArgumentParserTests'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)
Assert.NotNull(result.Version);
Assert.NotEmpty(result.Version);
Assert.NotNull(result.HelpSection);
Expand Down Expand Up @@ -71,6 +72,8 @@ public void Parse_should_ignore_unexpected_arguments()
Assert.True(result.IsValid);
Assert.False(result.HasMissingCommands);
Assert.True(result.HasIgnoredCommands);
var ignored = Assert.Single(result.Ignored);
Assert.Equal(ignored.Name, "help");

Check warning on line 76 in Simple.ArgumentParser.Tests/ArgumentParserTests.cs

View workflow job for this annotation

GitHub Actions / build-and-publish

The literal or constant value "help" should be passed as the 'expected' argument in the call to 'Assert.Equal(expected, actual)' in method 'Parse_should_ignore_unexpected_arguments' on type 'ArgumentParserTests'. Swap the parameter values. (https://xunit.net/xunit.analyzers/rules/xUnit2000)
Assert.False(result.HasInvalidCommands);
Assert.Null(result.Version);
Assert.Null(result.HelpSection);
Expand Down
4 changes: 4 additions & 0 deletions Simple.ArgumentParser.Tests/Options/BooleanOptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public class BooleanOptionTests

[Theory]
[InlineData("true")]
[InlineData("TRUE")]
[InlineData("false")]
[InlineData("FALSE")]
[InlineData("1")]
[InlineData("0")]
public void ValueIsValid_should_return_true_with_strict_off(string inlineData)
Expand All @@ -40,6 +42,8 @@ public void ValueIsValid_should_return_true_with_strict_off(string inlineData)
[Theory]
[InlineData("1")]
[InlineData("0")]
[InlineData("TRUE")]
[InlineData("FALSE")]
public void ValueIsValid_should_return_false_with_strict_on(string inlineData)
{
// arrange
Expand Down
Loading

0 comments on commit 5a6b316

Please sign in to comment.