2020-03-24 19:08:37 +01:00
|
|
|
// Copyright (c) Microsoft Corporation.
|
2018-02-13 18:23:53 +01:00
|
|
|
// Licensed under the MIT License.
|
2018-12-18 17:11:21 +01:00
|
|
|
|
2015-07-29 15:54:42 +02:00
|
|
|
using System;
|
2019-01-23 19:39:12 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Specialized;
|
2015-07-29 15:54:42 +02:00
|
|
|
using System.Management.Automation;
|
2018-09-19 09:50:30 +02:00
|
|
|
using System.Management.Automation.Internal;
|
2019-01-23 19:39:12 +01:00
|
|
|
using System.Threading;
|
2017-11-30 23:44:41 +01:00
|
|
|
using System.Reflection;
|
2019-01-23 19:39:12 +01:00
|
|
|
using Microsoft.PowerShell.Commands;
|
2018-12-18 17:11:21 +01:00
|
|
|
using Xunit;
|
2015-07-29 15:54:42 +02:00
|
|
|
|
2018-01-03 21:43:28 +01:00
|
|
|
namespace PSTests.Parallel
|
2015-07-29 15:54:42 +02:00
|
|
|
{
|
|
|
|
public static class UtilsTests
|
|
|
|
{
|
2017-11-30 23:44:41 +01:00
|
|
|
[SkippableFact]
|
2015-07-29 15:54:42 +02:00
|
|
|
public static void TestIsWinPEHost()
|
|
|
|
{
|
2017-11-30 23:44:41 +01:00
|
|
|
Skip.IfNot(Platform.IsWindows);
|
2015-07-29 15:54:42 +02:00
|
|
|
Assert.False(Utils.IsWinPEHost());
|
|
|
|
}
|
2018-09-19 09:50:30 +02:00
|
|
|
|
|
|
|
[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()
|
|
|
|
{
|
2020-07-13 16:10:57 +02:00
|
|
|
int capacity = 20;
|
2018-09-19 09:50:30 +02:00
|
|
|
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());
|
|
|
|
}
|
2019-01-23 19:39:12 +01:00
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestConvertToJsonBasic()
|
|
|
|
{
|
|
|
|
var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true);
|
|
|
|
string expected = "{\"name\":\"req\",\"type\":\"http\"}";
|
|
|
|
OrderedDictionary hash = new OrderedDictionary {
|
|
|
|
{"name", "req"},
|
|
|
|
{"type", "http"}
|
|
|
|
};
|
|
|
|
string json = JsonObject.ConvertToJson(hash, in context);
|
|
|
|
Assert.Equal(expected, json);
|
|
|
|
|
|
|
|
hash.Add("self", hash);
|
|
|
|
json = JsonObject.ConvertToJson(hash, context);
|
|
|
|
expected = "{\"name\":\"req\",\"type\":\"http\",\"self\":{\"name\":\"req\",\"type\":\"http\",\"self\":\"System.Collections.Specialized.OrderedDictionary\"}}";
|
|
|
|
Assert.Equal(expected, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestConvertToJsonWithEnum()
|
|
|
|
{
|
|
|
|
var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true);
|
|
|
|
string expected = "{\"type\":1}";
|
|
|
|
Hashtable hash = new Hashtable {
|
|
|
|
{"type", CommandTypes.Alias}
|
|
|
|
};
|
|
|
|
string json = JsonObject.ConvertToJson(hash, in context);
|
|
|
|
Assert.Equal(expected, json);
|
|
|
|
|
|
|
|
context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: true);
|
|
|
|
json = JsonObject.ConvertToJson(hash, in context);
|
|
|
|
expected = "{\"type\":\"Alias\"}";
|
|
|
|
Assert.Equal(expected, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestConvertToJsonWithoutCompress()
|
|
|
|
{
|
|
|
|
var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: false);
|
|
|
|
string expected = @"{
|
|
|
|
""type"": ""Alias""
|
|
|
|
}";
|
|
|
|
Hashtable hash = new Hashtable {
|
|
|
|
{"type", CommandTypes.Alias}
|
|
|
|
};
|
|
|
|
string json = JsonObject.ConvertToJson(hash, in context);
|
|
|
|
Assert.Equal(expected, json);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
public static void TestConvertToJsonCancellation()
|
|
|
|
{
|
|
|
|
var source = new CancellationTokenSource();
|
|
|
|
var context = new JsonObject.ConvertToJsonContext(
|
|
|
|
maxDepth: 1,
|
|
|
|
enumsAsStrings: true,
|
|
|
|
compressOutput: false,
|
|
|
|
source.Token,
|
|
|
|
Newtonsoft.Json.StringEscapeHandling.Default,
|
|
|
|
targetCmdlet: null);
|
|
|
|
|
|
|
|
source.Cancel();
|
|
|
|
Hashtable hash = new Hashtable {
|
|
|
|
{"type", CommandTypes.Alias}
|
|
|
|
};
|
|
|
|
|
|
|
|
string json = JsonObject.ConvertToJson(hash, in context);
|
|
|
|
Assert.Null(json);
|
|
|
|
}
|
2015-07-29 15:54:42 +02:00
|
|
|
}
|
|
|
|
}
|