Files
PowerShell-PowerShell/test/csharp/test_Utils.cs
T
Christoph Bergmeister [MVP] 4bc22d92df Add support for 'cd +' (#7206)
'cd +' adds the ability to revert 'cd -' by having another bounded stack with the same limit for 'redo' actions.
Behaviour is similar to back/forward navigation in explorer.exe
When the location is set to a path and the redo stack is non-empty then the redo stack gets flushed.
When cd + happens, then the redo stack is pushed to as well for a more intuitive way of navigating backwards and forwards.
All the work is done of course on the Set-location cmdlet, which is aliased to cd on all platforms.
2018-09-19 12:50:30 +05:00

76 lines
2.6 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using Xunit;
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Reflection;
namespace PSTests.Parallel
{
public static class UtilsTests
{
[SkippableFact]
public static void TestIsWinPEHost()
{
Skip.IfNot(Platform.IsWindows);
Assert.False(Utils.IsWinPEHost());
}
[Fact]
public static void TestHistoryStack()
{
var historyStack = new HistoryStack<string>(20);
Assert.Equal(0, historyStack.UndoCount);
Assert.Equal(0, historyStack.RedoCount);
historyStack.Push("first item");
historyStack.Push("second item");
Assert.Equal(2, historyStack.UndoCount);
Assert.Equal(0, historyStack.RedoCount);
Assert.Equal("second item", historyStack.Undo("second item"));
Assert.Equal("first item", historyStack.Undo("first item"));
Assert.Equal(0, historyStack.UndoCount);
Assert.Equal(2, historyStack.RedoCount);
Assert.Equal("first item", historyStack.Redo("first item"));
Assert.Equal(1, historyStack.UndoCount);
Assert.Equal(1, historyStack.RedoCount);
// Pushing a new item should invalidate the RedoCount
historyStack.Push("third item");
Assert.Equal(2, historyStack.UndoCount);
Assert.Equal(0, historyStack.RedoCount);
// Check for the correct exception when the Redo/Undo stack is empty.
Assert.Throws<InvalidOperationException>(() => historyStack.Redo("bar"));
historyStack.Undo("third item");
historyStack.Undo("first item");
Assert.Equal(0, historyStack.UndoCount);
Assert.Throws<InvalidOperationException>(() => historyStack.Undo("foo"));
}
[Fact]
public static void TestBoundedStack()
{
uint capacity = 20;
var boundedStack = new BoundedStack<string>(capacity);
Assert.Throws<InvalidOperationException>(() => boundedStack.Pop());
for (int i = 0; i < capacity; i++)
{
boundedStack.Push($"{i}");
}
for (int i = 0; i < capacity; i++)
{
var poppedItem = boundedStack.Pop();
Assert.Equal($"{20 - 1 - i}", poppedItem);
}
Assert.Throws<InvalidOperationException>(() => boundedStack.Pop());
}
}
}