Skip to content

Commit

Permalink
refactor usage of tuples (use tuples only if they explain the data be…
Browse files Browse the repository at this point in the history
…tter)
  • Loading branch information
Philipp-Binder committed Oct 26, 2024
1 parent 3b4ebac commit 83e777e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 56 deletions.
48 changes: 20 additions & 28 deletions test/DotNetEnv.Tests/LoadOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,32 @@ namespace DotNetEnv.Tests
{
public class LoadOptionsTests
{
public static readonly IndexedTheoryData<(LoadOptions OptionsUnderTest,
bool ExpectedSetEnvVars,
bool ExpectedClobberExistingVars,
bool ExpectedOnlyExactPath)>
LoadOptionTestCombinations = new ()
public static readonly IndexedTheoryData<LoadOptions,
(bool ExpectedSetEnvVars, bool ExpectedClobberExistingVars, bool ExpectedOnlyExactPath)>
LoadOptionTestCombinations = new()
{
(Env.NoEnvVars(), false, true, true),
(Env.NoClobber(), true, false, true),
(Env.TraversePath(), true, true, false),
(LoadOptions.NoEnvVars(), false, true, true),
(LoadOptions.NoClobber(), true, false, true),
(LoadOptions.TraversePath(), true, true, false),
(new LoadOptions(), true, true, true),
(new LoadOptions().NoEnvVars(), false, true, true),
(new LoadOptions().NoClobber(), true, false, true),
(new LoadOptions().TraversePath(), true, true, false),
(Env.NoEnvVars().NoClobber().TraversePath(), false, false, false),
(Env.NoClobber().TraversePath(), true, false, false),
(Env.NoEnvVars().NoClobber(), false, false, true),
(Env.NoEnvVars().TraversePath(), false, true, false),
{ Env.NoEnvVars(), (false, true, true) },
{ Env.NoClobber(), (true, false, true) },
{ Env.TraversePath(), (true, true, false) },
{ LoadOptions.NoEnvVars(), (false, true, true) },
{ LoadOptions.NoClobber(), (true, false, true) },
{ LoadOptions.TraversePath(), (true, true, false) },
{ new LoadOptions(), (true, true, true) },
{ new LoadOptions().NoEnvVars(), (false, true, true) },
{ new LoadOptions().NoClobber(), (true, false, true) },
{ new LoadOptions().TraversePath(), (true, true, false) },
{ Env.NoEnvVars().NoClobber().TraversePath(), (false, false, false) },
{ Env.NoClobber().TraversePath(), (true, false, false) },
{ Env.NoEnvVars().NoClobber(), (false, false, true) },
{ Env.NoEnvVars().TraversePath(), (false, true, false) },
};

[Theory]
[MemberData(nameof(LoadOptionTestCombinations))]
public void LoadOptionsShouldHaveCorrectPropertiesSet(string _,
(LoadOptions OptionsUnderTest,
bool ExpectedSetEnvVars,
bool ExpectedClobberExistingVars,
bool ExpectedOnlyExactPath) testData)
public void LoadOptionsShouldHaveCorrectPropertiesSet(string _, LoadOptions optionsUnderTest,
(bool ExpectedSetEnvVars, bool ExpectedClobberExistingVars, bool ExpectedOnlyExactPath) testData)
{
var (optionsUnderTest,
expectedSetEnvVars,
expectedClobberExistingVars,
expectedOnlyExactPath) = testData;
var (expectedSetEnvVars, expectedClobberExistingVars, expectedOnlyExactPath) = testData;

Assert.Equal(expectedSetEnvVars, optionsUnderTest.SetEnvVars);
Assert.Equal(expectedClobberExistingVars, optionsUnderTest.ClobberExistingVars);
Expand Down
60 changes: 34 additions & 26 deletions test/DotNetEnv.Tests/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,25 +405,33 @@ public void AssignmentShouldThrowOnParseUntilEnd(string invalidInput) =>
public void AssignmentShouldThrowOnParse(string invalidInput) =>
Assert.Throws<ParseException>(() => Parsers.Assignment.Parse(invalidInput));

public static readonly IndexedTheoryData<(string Contents, KeyValuePair<string, string>[] Expecteds)>
ParseDotEnvTests = new()
public static readonly IndexedTheoryData<string, KeyValuePair<string, string>[]> ParseDotEnvTests = new()
{
{ "", Array.Empty<KeyValuePair<string, string>>() },
{ "EV_DNE=abc", new[] { new KeyValuePair<string, string>("EV_DNE", "abc") } },
{ "SET EV_DNE=\"0\n1\"", new[] { new KeyValuePair<string, string>("EV_DNE", "0\n1") } },
{
("", Array.Empty<KeyValuePair<string, string>>()),
("EV_DNE=abc", new[] { new KeyValuePair<string, string>("EV_DNE", "abc") }),
("SET EV_DNE=\"0\n1\"", new[] { new KeyValuePair<string, string>("EV_DNE", "0\n1") }),
(@"
@"
# this is a header
export EV_DNE='a b c' #works!
"
, new[] { new KeyValuePair<string, string>("EV_DNE", "a b c") }),
("# this is a header\nexport EV_DNE='d e f' #works!",
new[] { new KeyValuePair<string, string>("EV_DNE", "d e f") }),
("#\n# this is a header\n#\n\nexport EV_DNE='g h i' #yep still\n",
new[] { new KeyValuePair<string, string>("EV_DNE", "g h i") }),
("#\n# this is a header\n#\n\nexport EV_DNE=\"\\xe6\\x97\\xa5 $ENVVAR_TEST 本\" #yep still\n",
new[] { new KeyValuePair<string, string>("EV_DNE", "日 ENV value 本") }),
(@"#
",
new[] { new KeyValuePair<string, string>("EV_DNE", "a b c") }
},
{
"# this is a header\nexport EV_DNE='d e f' #works!",
new[] { new KeyValuePair<string, string>("EV_DNE", "d e f") }
},
{
"#\n# this is a header\n#\n\nexport EV_DNE='g h i' #yep still\n",
new[] { new KeyValuePair<string, string>("EV_DNE", "g h i") }
},
{
"#\n# this is a header\n#\n\nexport EV_DNE=\"\\xe6\\x97\\xa5 $ENVVAR_TEST 本\" #yep still\n",
new[] { new KeyValuePair<string, string>("EV_DNE", "日 ENV value 本") }
},
{
@"#
# this is a header
#
Expand All @@ -437,20 +445,20 @@ public void AssignmentShouldThrowOnParse(string invalidInput) =>
ENVVAR_TEST = ' yahooooo '
",
new[]
{
new KeyValuePair<string, string>("EV_DNE", "x y z"),
new KeyValuePair<string, string>("EV_TEST_1", "日 $ENVVAR_TEST 本"),
new KeyValuePair<string, string>("EV_TEST_2", "☠\n®"),
new KeyValuePair<string, string>("ENVVAR_TEST", " yahooooo "),
}),
};
new[]
{
new KeyValuePair<string, string>("EV_DNE", "x y z"),
new KeyValuePair<string, string>("EV_TEST_1", "日 $ENVVAR_TEST 本"),
new KeyValuePair<string, string>("EV_TEST_2", "☠\n®"),
new KeyValuePair<string, string>("ENVVAR_TEST", " yahooooo "),
}
},
};

[Theory]
[MemberData(nameof(ParseDotEnvTests))]
public void ParseDotenvFileShouldParseContents(string _, (string Contents, KeyValuePair<string, string>[] Expecteds) testData)
public void ParseDotenvFileShouldParseContents(string _, string contents, KeyValuePair<string, string>[] expectedPairs)
{
var (contents, expectedPairs) = testData;

var outputs = Parsers.ParseDotenvFile(contents, Parsers.SetEnvVar).ToArray();
Assert.Equal(expectedPairs.Length, outputs.Length);

Expand Down
13 changes: 11 additions & 2 deletions test/DotNetEnv.Tests/XUnit/IndexedTheoryData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@

namespace DotNetEnv.Tests.XUnit;

public class IndexedTheoryData<TData> : TheoryData
public class IndexedTheoryData<TP1> : TheoryData
{
private int _index = 0;
public void Add(TData p1, [CallerMemberName]string callerMemberName = null, [CallerLineNumber]int callerLineNumber = 0)
public void Add(TP1 p1, [CallerMemberName]string callerMemberName = null, [CallerLineNumber]int callerLineNumber = 0)
{
AddRow($"{_index++,4}; {callerMemberName}:{callerLineNumber}", p1);
}
}

public class IndexedTheoryData<TP1, TP2> : TheoryData
{
private int _index = 0;
public void Add(TP1 p1, TP2 p2, [CallerMemberName]string callerMemberName = null, [CallerLineNumber]int callerLineNumber = 0)
{
AddRow($"{_index++,4}; {callerMemberName}:{callerLineNumber}", p1, p2);
}
}

0 comments on commit 83e777e

Please sign in to comment.