MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections;6using System.Collections.Specialized;7using System.Management.Automation;8using System.Management.Automation.Internal;9using System.Threading;10using System.Reflection;11using Microsoft.PowerShell.Commands;12using Xunit;13 14namespace PSTests.Parallel15{16 public static class UtilsTests17 {18 [SkippableFact]19 public static void TestIsWinPEHost()20 {21 Skip.IfNot(Platform.IsWindows);22 Assert.False(Utils.IsWinPEHost());23 }24 25 [Theory]26 [InlineData(long.MinValue, "0 Bytes")]27 [InlineData(-1, "0 Bytes")]28 [InlineData(0, "0 Bytes")]29 [InlineData(1, "1 Bytes")]30 [InlineData(1024, "1.0 KB")]31 [InlineData(3000, "2.9 KB")]32 [InlineData(1024 * 1024, "1.0 MB")]33 [InlineData(1024 * 1024 * 1024, "1.000 GB")]34 [InlineData((long)(1024 * 1024 * 1024) * 1024, "1.00000 TB")]35 [InlineData((long)(1024 * 1024 * 1024) * 1024 * 1024, "1.0000000 PB")]36 [InlineData(long.MaxValue, "8.000000000 EB")]37 public static void DisplayHumanReadableFileSize(long bytes, string expected)38 {39 Assert.Equal(expected, Utils.DisplayHumanReadableFileSize(bytes));40 }41 42 [Fact]43 public static void TestHistoryStack()44 {45 var historyStack = new HistoryStack<string>(20);46 Assert.Equal(0, historyStack.UndoCount);47 Assert.Equal(0, historyStack.RedoCount);48 49 historyStack.Push("first item");50 historyStack.Push("second item");51 Assert.Equal(2, historyStack.UndoCount);52 Assert.Equal(0, historyStack.RedoCount);53 54 Assert.Equal("second item", historyStack.Undo("second item"));55 Assert.Equal("first item", historyStack.Undo("first item"));56 Assert.Equal(0, historyStack.UndoCount);57 Assert.Equal(2, historyStack.RedoCount);58 59 Assert.Equal("first item", historyStack.Redo("first item"));60 Assert.Equal(1, historyStack.UndoCount);61 Assert.Equal(1, historyStack.RedoCount);62 63 // Pushing a new item should invalidate the RedoCount64 historyStack.Push("third item");65 Assert.Equal(2, historyStack.UndoCount);66 Assert.Equal(0, historyStack.RedoCount);67 68 // Check for the correct exception when the Redo/Undo stack is empty.69 Assert.Throws<InvalidOperationException>(() => historyStack.Redo("bar"));70 historyStack.Undo("third item");71 historyStack.Undo("first item");72 Assert.Equal(0, historyStack.UndoCount);73 Assert.Throws<InvalidOperationException>(() => historyStack.Undo("foo"));74 }75 76 [Fact]77 public static void TestBoundedStack()78 {79 const int capacity = 20;80 var boundedStack = new BoundedStack<string>(capacity);81 Assert.Throws<InvalidOperationException>(() => boundedStack.Pop());82 83 for (int i = 0; i < capacity; i++)84 {85 boundedStack.Push($"{i}");86 }87 88 for (int i = 0; i < capacity; i++)89 {90 var poppedItem = boundedStack.Pop();91 Assert.Equal($"{20 - 1 - i}", poppedItem);92 }93 94 Assert.Throws<InvalidOperationException>(() => boundedStack.Pop());95 }96 97 [Fact]98 public static void TestConvertToJsonBasic()99 {100 var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true);101 string expected = "{\"name\":\"req\",\"type\":\"http\"}";102 OrderedDictionary hash = new OrderedDictionary {103 {"name", "req"},104 {"type", "http"}105 };106 string json = JsonObject.ConvertToJson(hash, in context);107 Assert.Equal(expected, json);108 109 hash.Add("self", hash);110 json = JsonObject.ConvertToJson(hash, context);111 expected = "{\"name\":\"req\",\"type\":\"http\",\"self\":{\"name\":\"req\",\"type\":\"http\",\"self\":\"System.Collections.Specialized.OrderedDictionary\"}}";112 Assert.Equal(expected, json);113 }114 115 [Fact]116 public static void TestConvertToJsonWithEnum()117 {118 var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: false, compressOutput: true);119 string expected = "{\"type\":1}";120 Hashtable hash = new Hashtable {121 {"type", CommandTypes.Alias}122 };123 string json = JsonObject.ConvertToJson(hash, in context);124 Assert.Equal(expected, json);125 126 context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: true);127 json = JsonObject.ConvertToJson(hash, in context);128 expected = "{\"type\":\"Alias\"}";129 Assert.Equal(expected, json);130 }131 132 [Fact]133 public static void TestConvertToJsonWithoutCompress()134 {135 var context = new JsonObject.ConvertToJsonContext(maxDepth: 1, enumsAsStrings: true, compressOutput: false);136 const string expected = @"{137 ""type"": ""Alias""138}";139 Hashtable hash = new Hashtable {140 {"type", CommandTypes.Alias}141 };142 string json = JsonObject.ConvertToJson(hash, in context);143 Assert.Equal(expected, json);144 }145 146 [Fact]147 public static void TestConvertToJsonCancellation()148 {149 var source = new CancellationTokenSource();150 var context = new JsonObject.ConvertToJsonContext(151 maxDepth: 1,152 enumsAsStrings: true,153 compressOutput: false,154 Newtonsoft.Json.StringEscapeHandling.Default,155 targetCmdlet: null,156 source.Token);157 158 source.Cancel();159 Hashtable hash = new Hashtable {160 {"type", CommandTypes.Alias}161 };162 163 string json = JsonObject.ConvertToJson(hash, in context);164 Assert.Null(json);165 }166 167 [Fact]168 public static void TestEmptyErrorRecordToString()169 {170 Assert.Equal(string.Empty, new ErrorRecord(new Exception(string.Empty), null, ErrorCategory.NotSpecified, null).ToString());171 }172 173 [Fact]174 public static void TestNonEmptyErrorRecordToString()175 {176 Assert.Equal("test", new ErrorRecord(new Exception("test"), null, ErrorCategory.NotSpecified, null).ToString());177 }178 }179}180 