-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add missing comments * Fix .NET unit test execution logic * Remove obsolete files * Change hardcoded throw for call to ErrorUtil * Add unit tests * Fix output file names * Change PowerShell package ref to private * Change PowerShell ref to full SDK, and drop net471 * Optimize reflection lookup logic * Add PowerShell test fixture * Remove platform-dependent tests * Add unit tests * Add missing [Fact] attribute * Update gitignore * Fix typos * Add workspace settings file * Fix missing tags in comment * Fix flaky test * Improve interaction with PS * Improve parameter override logic * Refactor Mru class * Remove stale file * Fix PS flaky behavior * Add ThrowIfNull method * Refactor simple methods into expression-bodied * Add separate Merge and Override methods * Update release notes
- Loading branch information
Showing
37 changed files
with
1,023 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,3 +214,7 @@ TfsCmdlets.PSDesktop.xml | |
# JetBrains Rider project | ||
|
||
CSharp/.idea/** | ||
|
||
# Miscellaneous files | ||
|
||
.testtemplate.cs |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 0 additions & 14 deletions
14
CSharp/TfsCmdlets.Tests.Integration.PSDesktop/UnitTest1.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System.Management.Automation; | ||
using System; | ||
using System.Management.Automation.Runspaces; | ||
|
||
namespace TfsCmdlets.Tests.UnitTests | ||
{ | ||
public class PowerShellFixture : IDisposable | ||
{ | ||
private bool _IsDisposed; | ||
|
||
public PowerShell PowerShell {get; private set;} | ||
|
||
public PowerShellFixture() | ||
{ | ||
PowerShell = PowerShell.Create(); | ||
Runspace.DefaultRunspace = PowerShell.Runspace; | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{ | ||
if (!_IsDisposed) | ||
{ | ||
if (disposing) | ||
{ | ||
PowerShell.Dispose(); | ||
} | ||
|
||
Runspace.DefaultRunspace = null; | ||
PowerShell = null; | ||
_IsDisposed = true; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(disposing: true); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
CSharp/TfsCmdlets.Tests.UnitTests/Util/EnvironmentUtil_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// using Xunit; | ||
// using TfsCmdlets.Util; | ||
// using System; | ||
|
||
// namespace TfsCmdlets.Tests.UnitTests.Util | ||
// { | ||
// public class EnvironmentUtil_Tests | ||
// { | ||
// [Fact] | ||
// [Trait("Platform", "Core")] | ||
// public void PSEdition_Returns_Core_When_PSCore() | ||
// { | ||
// Assert.Equal("Core", EnvironmentUtil.PSEdition); | ||
// } | ||
|
||
// [Fact] | ||
// [Trait("Platform", "Desktop")] | ||
// public void PSEdition_Returns_Desktop_When_PSWindows() | ||
// { | ||
// Assert.Equal("Desktop", EnvironmentUtil.PSEdition); | ||
// } | ||
|
||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using Xunit; | ||
using TfsCmdlets.Util; | ||
using System; | ||
|
||
namespace TfsCmdlets.Tests.UnitTests.Util | ||
{ | ||
public class ErrorUtil_Tests | ||
{ | ||
[Fact] | ||
public void ThrowDesktopOnlyCmdlet_Throws_NotSupportedException() | ||
{ | ||
Assert.Throws<NotSupportedException>(() => | ||
ErrorUtil.ThrowDesktopOnlyCmdlet() | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfNotFound_Throws_ArgumentException() | ||
{ | ||
Assert.Throws<ArgumentException>("test", () => | ||
ErrorUtil.ThrowIfNotFound(null, "test", "searchCriteria") | ||
); | ||
} | ||
|
||
[Fact] | ||
public void ThrowIfNull_Throws_ArgumentNullException() | ||
{ | ||
Assert.Throws<ArgumentNullException>("myVar", () => | ||
ErrorUtil.ThrowIfNull(null, "myVar")); | ||
} | ||
} | ||
} |
92 changes: 92 additions & 0 deletions
92
CSharp/TfsCmdlets.Tests.UnitTests/Util/LazyProperty_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using Xunit; | ||
using TfsCmdlets.Util; | ||
using TfsCmdlets.Extensions; | ||
using System; | ||
using System.Linq; | ||
using System.Management.Automation; | ||
using System.Collections; | ||
using System.Management.Automation.Runspaces; | ||
|
||
namespace TfsCmdlets.Tests.UnitTests.Util | ||
{ | ||
public class LazyProperty_Tests: IClassFixture<PowerShellFixture> | ||
{ | ||
private PowerShell _PowerShell; | ||
|
||
public LazyProperty_Tests(PowerShellFixture fixture) | ||
{ | ||
_PowerShell = fixture.PowerShell; | ||
} | ||
|
||
[Fact] | ||
public void PropertyBag_Is_Created_On_First_Call() | ||
{ | ||
const string propName = "__PropertyBag"; | ||
|
||
var returnValues = _PowerShell.AddScript(@" | ||
Function PropertyBag_Is_Created_On_First_Call | ||
{ | ||
$pso = [PSCustomObject] @{Foo='Bar'} | ||
$pso | Add-Member -Name MyLazy -MemberType ScriptProperty ` | ||
-Value { [TfsCmdlets.Util.LazyProperty]::Get($pso, 'MyLazy', {[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Next()}) } | ||
[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Reset() | ||
$val1 = $pso.MyLazy | ||
return @($val1, $pso) | ||
} | ||
$r = PropertyBag_Is_Created_On_First_Call; echo $r" | ||
).Invoke().ToList(); | ||
|
||
Assert.Equal(1, returnValues[0]); | ||
Assert.True(((PSObject) returnValues[1]).Properties.Match(propName, PSMemberTypes.NoteProperty).Any(), $"Property '{propName}' not found"); | ||
} | ||
|
||
[Fact] | ||
public void Lazy_Property_Is_Called_Only_Once() | ||
{ | ||
var returnValues = _PowerShell.AddScript(@" | ||
Function Lazy_Property_Is_Called_Only_Once | ||
{ | ||
$pso = [PSCustomObject]@{Foo='Bar'} | ||
$pso | Add-Member -Name MyLazy -MemberType ScriptProperty ` | ||
-Value { [TfsCmdlets.Util.LazyProperty]::Get($pso, 'MyLazy', {[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Next()}) } | ||
[TfsCmdlets.Tests.UnitTests.Util.LazyPropertySimulator]::Reset() | ||
$val1 = $pso.MyLazy | ||
$val2 = $pso.MyLazy | ||
return $val1, $val2 | ||
} | ||
$r = Lazy_Property_Is_Called_Only_Once; echo $r" | ||
).Invoke().ToList(); | ||
|
||
Assert.Equal(1, returnValues[0].BaseObject); | ||
Assert.Equal(1, returnValues[1].BaseObject); | ||
} | ||
} | ||
|
||
public static class LazyPropertySimulator | ||
{ | ||
private static int _Counter; | ||
|
||
static LazyPropertySimulator() | ||
{ | ||
Reset(); | ||
} | ||
|
||
public static int Next() { | ||
return _Counter++; | ||
} | ||
|
||
public static void Reset() | ||
{ | ||
_Counter = 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
using System; | ||
using TfsCmdlets.Util; | ||
using Xunit; | ||
|
||
namespace TfsCmdlets.Tests.UnitTests | ||
{ | ||
public class Mru_Tests | ||
{ | ||
private readonly string[] _SampleItems = new[] { "item1", "item2", "item3" }; | ||
|
||
[Fact] | ||
public void Supports_Add_All() | ||
{ | ||
var mruList = new MruList<string>(); | ||
|
||
mruList.Append(_SampleItems); | ||
|
||
Assert.Collection(mruList, | ||
i => Assert.Equal("item1", i), | ||
i => Assert.Equal("item2", i), | ||
i => Assert.Equal("item3", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Supports_Initial_Items() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems); | ||
|
||
Assert.Collection(mruList, | ||
i => Assert.Equal("item1", i), | ||
i => Assert.Equal("item2", i), | ||
i => Assert.Equal("item3", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Can_Find_Items() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems); | ||
|
||
Assert.Equal("item3", mruList.Get(i => i.EndsWith("3"))); | ||
} | ||
|
||
[Fact] | ||
public void Get_Returns_Top_Item() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems); | ||
|
||
Assert.Equal("item1", mruList.Get()); | ||
} | ||
|
||
[Fact] | ||
public void Get_Find_Moves_Item_To_Top() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems); | ||
|
||
var item = mruList.Get(i => i.EndsWith("3")); | ||
|
||
Assert.Equal(item, mruList.Get()); | ||
} | ||
|
||
[Fact] | ||
public void Can_Limit_Initial_Capacity() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems, 2); | ||
|
||
Assert.Collection(mruList, | ||
i => Assert.Equal("item1", i), | ||
i => Assert.Equal("item2", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Supports_Collection_Initialization() | ||
{ | ||
var mruList = new MruList<string>() | ||
{ | ||
"item1", | ||
"item2", | ||
"item3" | ||
}; | ||
|
||
Assert.Collection(mruList, | ||
i => Assert.Equal("item1", i), | ||
i => Assert.Equal("item2", i), | ||
i => Assert.Equal("item3", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Can_Discard_Lru_On_Capacity_Reached() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems, 3); | ||
|
||
mruList.Insert("item4"); | ||
|
||
Assert.Equal(3, mruList.Count); | ||
|
||
Assert.Collection(mruList, | ||
i => Assert.Equal("item4", i), | ||
i => Assert.Equal("item1", i), | ||
i => Assert.Equal("item2", i) | ||
); | ||
} | ||
|
||
[Fact] | ||
public void Throw_On_Append_When_Capacity_Reached() | ||
{ | ||
var mruList = new MruList<string>(_SampleItems, 3); | ||
|
||
Assert.Throws<Exception>(() => mruList.Append("item4")); | ||
} | ||
} | ||
} |
Oops, something went wrong.