Skip to content

Commit

Permalink
add test for env parsed args
Browse files Browse the repository at this point in the history
  • Loading branch information
jannickj committed Oct 5, 2020
1 parent 0e9da2f commit de18c77
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/CommandLine.Tests/Fakes/Simple_Options_With_Env.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.

using System.Collections.Generic;

namespace CommandLine.Tests.Fakes
{
class Simple_Options_With_Env
{
[Option('s', Default = "", Env = "StringValue")]
public string StringValue { get; set; }

[Option("bvff", Env = "BoolValueFullFalse", HelpText = "Define a boolean or switch value here.")]
public bool BoolValueFullFalse { get; set; }
[Option("bvft", Env = "BoolValueFullTrue", HelpText = "Define a boolean or switch value here.")]
public bool BoolValueFullTrue { get; set; }
[Option("bvst", Env = "BoolValueShortTrue", HelpText = "Define a boolean or switch value here.")]
public bool BoolValueShortTrue { get; set; }
[Option("bvsf", Env = "BoolValueShortFalse", HelpText = "Define a boolean or switch value here.")]
public bool BoolValueShortFalse { get; set; }


[Option('l', Default = 1, Env = "LongValue")]
public long LongValue { get; set; }

[Option('i', Default = 2, Env = "IntValue")]
public long IntValue { get; set; }
}
}
33 changes: 33 additions & 0 deletions tests/CommandLine.Tests/Unit/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,39 @@ public void Parse_options_with_double_dash()
// Teardown
}

[Fact]
public void Parse_spec_with_enviroment_variables()
{
// Fixture setup
var expectedOptions = new Simple_Options_With_Env
{
StringValue = "astring",
LongValue = 20L,
IntValue = 2,
BoolValueFullFalse = false,
BoolValueFullTrue = true,
BoolValueShortTrue = true,
BoolValueShortFalse = false,
};
var sut = new Parser(with => with.EnableDashDash = true);

// Exercize system
Environment.SetEnvironmentVariable("StringValue", "astring");
Environment.SetEnvironmentVariable("LongValue", "20");
Environment.SetEnvironmentVariable("IntValue", null);
Environment.SetEnvironmentVariable("BoolValueFullFalse", "false");
Environment.SetEnvironmentVariable("BoolValueFullTrue", "true");
Environment.SetEnvironmentVariable("BoolValueShortTrue", "1");
Environment.SetEnvironmentVariable("BoolValueShortFalse", "0");
var result =
sut.ParseArguments<Simple_Options_With_Env>(
new string[0]);

// Verify outcome
((Parsed<Simple_Options_With_Env>)result).Value.Should().BeEquivalentTo(expectedOptions);
// Teardown
}

[Fact]
public void Parse_options_with_double_dash_and_option_sequence()
{
Expand Down

0 comments on commit de18c77

Please sign in to comment.