MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Management.Automation;6using Xunit;7 8namespace PSTests.Parallel9{10 public class CompletionHelpersTests11 {12 [Theory]13 [InlineData("", "'", "''")]14 [InlineData("", "\"", "\"\"")]15 [InlineData("'", "'", "''''")]16 [InlineData("", "", "''")]17 [InlineData("", null, "''")]18 [InlineData("word", "'", "'word'")]19 [InlineData("word", "\"", "\"word\"")]20 [InlineData("word's", "'", "'word''s'")]21 [InlineData("already 'quoted'", "'", "'already ''quoted'''")]22 [InlineData("multiple 'quotes' in 'text'", "'", "'multiple ''quotes'' in ''text'''")]23 [InlineData("\"word\"", "'", "'\"word\"'")]24 [InlineData("'word'", "'", "''word''")]25 [InlineData("word", "", "word")]26 [InlineData("word", null, "word")]27 [InlineData("\"word\"", "\"", "\"\"word\"\"")]28 [InlineData("'word'", "\"", "\"'word'\"")]29 [InlineData("word with space", "'", "'word with space'")]30 [InlineData("word with space", "\"", "\"word with space\"")]31 [InlineData("word\"with\"quotes", "'", "'word\"with\"quotes'")]32 [InlineData("word'with'quotes", "\"", "\"word'with'quotes\"")]33 [InlineData("while", "'", "'while'")]34 [InlineData("while", "", "while")]35 [InlineData("while", "\"", "\"while\"")]36 [InlineData("$variable", "'", "'$variable'")]37 [InlineData("$variable", "", "$variable")]38 [InlineData("$variable", "\"", "\"$variable\"")]39 [InlineData("key$word", "'", "'key$word'")]40 [InlineData("key$word", "", "'key$word'")]41 [InlineData("key$word", "\"", "\"key$word\"")]42 [InlineData("`r`n", "\"", "\"`r`n\"")]43 [InlineData("`r`n", "'", "\"`r`n\"")]44 [InlineData("`r`n", "", "\"`r`n\"")]45 [InlineData("`r`n `${0}", "\"", "\"`r`n `${0}\"")]46 [InlineData("`r`n `${0}", "'", "\"`r`n `${0}\"")]47 [InlineData("`r`n `${0}", "", "\"`r`n `${0}\"")]48 [InlineData("`n", "\"", "\"`n\"")]49 [InlineData("`n", "'", "\"`n\"")]50 [InlineData("`n", "", "\"`n\"")]51 [InlineData("`n `${0}", "\"", "\"`n `${0}\"")]52 [InlineData("`n `${0}", "'", "\"`n `${0}\"")]53 [InlineData("`n `${0}", "", "\"`n `${0}\"")]54 public void TestQuoteCompletionText(55 string completionText,56 string quote,57 string expected)58 {59 string result = CompletionHelpers.QuoteCompletionText(completionText, quote);60 Assert.Equal(expected, result);61 }62 63 [Theory]64 [InlineData("normaltext", false)]65 [InlineData("$variable", false)]66 [InlineData("abc def", true)]67 [InlineData("while", false)] // PowerShell keyword68 [InlineData("key$word", true)]69 [InlineData("abc`def", true)]70 [InlineData("normal-text", false)]71 [InlineData("\"doublequotes\"", false)]72 [InlineData("'singlequotes'", false)]73 [InlineData("normal 'text'", true)]74 [InlineData("normal \"text\"", true)]75 [InlineData("text with ' and \"", true)]76 [InlineData("text\"with\"quotes", false)]77 [InlineData("text'with'quotes", false)]78 [InlineData("\"key$", true)]79 [InlineData("\"", true)]80 [InlineData("'", true)]81 [InlineData("", true)]82 [InlineData(";", true)]83 [InlineData("; ", true)]84 [InlineData(",", true)]85 [InlineData(", ", true)]86 public void TestCompletionRequiresQuotes(string completion, bool expected)87 {88 bool result = CompletionHelpers.CompletionRequiresQuotes(completion);89 Assert.Equal(expected, result);90 }91 92 [Theory]93 [InlineData("", "", "")]94 [InlineData("\"", "", "\"")]95 [InlineData("'", "", "'")]96 [InlineData("\"word\"", "word", "\"")]97 [InlineData("'word'", "word", "'")]98 [InlineData("\"word", "word", "\"")]99 [InlineData("'word", "word", "'")]100 [InlineData("word\"", "word\"", "")]101 [InlineData("word'", "word'", "")]102 [InlineData("\"word's\"", "word's", "\"")]103 [InlineData("'word\"", "'word\"", "")]104 [InlineData("\"word'", "\"word'", "")]105 [InlineData("'word\"s'", "word\"s", "'")]106 public void TestHandleDoubleAndSingleQuote(string wordToComplete, string expectedWordToComplete, string expectedQuote)107 {108 string quote = CompletionHelpers.HandleDoubleAndSingleQuote(ref wordToComplete);109 Assert.Equal(expectedQuote, quote);110 Assert.Equal(expectedWordToComplete, wordToComplete);111 }112 113 [Theory]114 [InlineData("word", "word", true)]115 [InlineData("Word", "word", true)]116 [InlineData("word", "wor", true)]117 [InlineData("word", "words", false)]118 [InlineData("word`nnext", "word`n", true)]119 [InlineData("word`r`nnext", "word`r`n", true)]120 [InlineData("word;next", "word;", true)]121 [InlineData("word,next", "word,", true)]122 [InlineData("word[*]next", "word[*", true)]123 [InlineData("word[abc]next", "word[abc", true)]124 [InlineData("word", "word*", true)]125 [InlineData("Word", "word*", true)]126 [InlineData("word(Special)", "word*", true)]127 [InlineData("testword", "test", true)]128 [InlineData("word", "", true)]129 [InlineData("", "word", false)]130 public void TestDefaultMatch(string value, string wordToComplete, bool expected)131 {132 bool result = CompletionHelpers.DefaultMatch(value, wordToComplete);133 Assert.Equal(expected, result);134 }135 136 [Theory]137 [InlineData("word", "word", true)]138 [InlineData("Word", "word", true)]139 [InlineData("word", "wor", true)]140 [InlineData("word", "words", false)]141 [InlineData("word`nnext", "word`n", true)]142 [InlineData("word`r`nnext", "word`r`n", true)]143 [InlineData("word;next", "word;", true)]144 [InlineData("word,next", "word,", true)]145 [InlineData("word[*]next", "word[*", true)]146 [InlineData("word[abc]next", "word[abc", true)]147 [InlineData("word", "word*", false)]148 [InlineData("Word", "word*", false)]149 [InlineData("word(Special)", "word*", false)]150 [InlineData("testword", "test", true)]151 [InlineData("word", "", true)]152 [InlineData("", "word", false)]153 public void TestWildcardPatternEscapeMatch(string value, string wordToComplete, bool expected)154 {155 bool result = CompletionHelpers.WildcardPatternEscapeMatch(value, wordToComplete);156 Assert.Equal(expected, result);157 }158 159 [Theory]160 [InlineData("\n", "`n")]161 [InlineData("\r", "`r")]162 [InlineData("\r\n", "`r`n")]163 [InlineData("\t", "`t")]164 [InlineData("\0", "`0")]165 [InlineData("\a", "`a")]166 [InlineData("\b", "`b")]167 [InlineData("\u001b", "`e")]168 [InlineData("\f", "`f")]169 [InlineData("\v", "`v")]170 [InlineData("word\n", "word`n")]171 [InlineData("word\r", "word`r")]172 [InlineData("word\t", "word`t")]173 [InlineData("word\u001b", "word`e")]174 [InlineData("word\f", "word`f")]175 [InlineData("word\v", "word`v")]176 [InlineData("word\r\n", "word`r`n")]177 public void TestNormalizeToExpandableString(string value, string expected)178 {179 string result = CompletionHelpers.NormalizeToExpandableString(value);180 Assert.Equal(expected, result);181 }182 }183}184 