File size: 7,683 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;
using Xunit;

namespace PSTests.Parallel
{
    public class CompletionHelpersTests
    {
        [Theory]
        [InlineData("", "'", "''")]
        [InlineData("", "\"", "\"\"")]
        [InlineData("'", "'", "''''")]
        [InlineData("", "", "''")]
        [InlineData("", null, "''")]
        [InlineData("word", "'", "'word'")]
        [InlineData("word", "\"", "\"word\"")]
        [InlineData("word's", "'", "'word''s'")]
        [InlineData("already 'quoted'", "'", "'already ''quoted'''")]
        [InlineData("multiple 'quotes' in 'text'", "'", "'multiple ''quotes'' in ''text'''")]
        [InlineData("\"word\"", "'", "'\"word\"'")]
        [InlineData("'word'", "'", "''word''")]
        [InlineData("word", "", "word")]
        [InlineData("word", null, "word")]
        [InlineData("\"word\"", "\"", "\"\"word\"\"")]
        [InlineData("'word'", "\"", "\"'word'\"")]
        [InlineData("word with space", "'", "'word with space'")]
        [InlineData("word with space", "\"", "\"word with space\"")]
        [InlineData("word\"with\"quotes", "'", "'word\"with\"quotes'")]
        [InlineData("word'with'quotes", "\"", "\"word'with'quotes\"")]
        [InlineData("while", "'", "'while'")]
        [InlineData("while", "", "while")]
        [InlineData("while", "\"", "\"while\"")]
        [InlineData("$variable", "'", "'$variable'")]
        [InlineData("$variable", "", "$variable")]
        [InlineData("$variable", "\"", "\"$variable\"")]
        [InlineData("key$word", "'", "'key$word'")]
        [InlineData("key$word", "", "'key$word'")]
        [InlineData("key$word", "\"", "\"key$word\"")]
        [InlineData("`r`n", "\"", "\"`r`n\"")]
        [InlineData("`r`n", "'", "\"`r`n\"")]
        [InlineData("`r`n", "", "\"`r`n\"")]
        [InlineData("`r`n    `${0}", "\"", "\"`r`n    `${0}\"")]
        [InlineData("`r`n    `${0}", "'", "\"`r`n    `${0}\"")]
        [InlineData("`r`n    `${0}", "", "\"`r`n    `${0}\"")]
        [InlineData("`n", "\"", "\"`n\"")]
        [InlineData("`n", "'", "\"`n\"")]
        [InlineData("`n", "", "\"`n\"")]
        [InlineData("`n    `${0}", "\"", "\"`n    `${0}\"")]
        [InlineData("`n    `${0}", "'", "\"`n    `${0}\"")]
        [InlineData("`n    `${0}", "", "\"`n    `${0}\"")]
        public void TestQuoteCompletionText(
             string completionText,
             string quote,
             string expected)
        {
            string result = CompletionHelpers.QuoteCompletionText(completionText, quote);
            Assert.Equal(expected, result);
        }

        [Theory]
        [InlineData("normaltext", false)]
        [InlineData("$variable", false)]
        [InlineData("abc def", true)]
        [InlineData("while", false)] // PowerShell keyword
        [InlineData("key$word", true)]
        [InlineData("abc`def", true)]
        [InlineData("normal-text", false)]
        [InlineData("\"doublequotes\"", false)]
        [InlineData("'singlequotes'", false)]
        [InlineData("normal 'text'", true)]
        [InlineData("normal \"text\"", true)]
        [InlineData("text with ' and \"", true)]
        [InlineData("text\"with\"quotes", false)]
        [InlineData("text'with'quotes", false)]
        [InlineData("\"key$", true)]
        [InlineData("\"", true)]
        [InlineData("'", true)]
        [InlineData("", true)]
        [InlineData(";", true)]
        [InlineData("; ", true)]
        [InlineData(",", true)]
        [InlineData(", ", true)]
        public void TestCompletionRequiresQuotes(string completion, bool expected)
        {
            bool result = CompletionHelpers.CompletionRequiresQuotes(completion);
            Assert.Equal(expected, result);
        }

        [Theory]
        [InlineData("", "", "")]
        [InlineData("\"", "", "\"")]
        [InlineData("'", "", "'")]
        [InlineData("\"word\"", "word", "\"")]
        [InlineData("'word'", "word", "'")]
        [InlineData("\"word", "word", "\"")]
        [InlineData("'word", "word", "'")]
        [InlineData("word\"", "word\"", "")]
        [InlineData("word'", "word'", "")]
        [InlineData("\"word's\"", "word's", "\"")]
        [InlineData("'word\"", "'word\"", "")]
        [InlineData("\"word'", "\"word'", "")]
        [InlineData("'word\"s'", "word\"s", "'")]
        public void TestHandleDoubleAndSingleQuote(string wordToComplete, string expectedWordToComplete, string expectedQuote)
        {
            string quote = CompletionHelpers.HandleDoubleAndSingleQuote(ref wordToComplete);
            Assert.Equal(expectedQuote, quote);
            Assert.Equal(expectedWordToComplete, wordToComplete);
        }

        [Theory]
        [InlineData("word", "word", true)]
        [InlineData("Word", "word", true)]
        [InlineData("word", "wor", true)]
        [InlineData("word", "words", false)]
        [InlineData("word`nnext", "word`n", true)]
        [InlineData("word`r`nnext", "word`r`n", true)]
        [InlineData("word;next", "word;", true)]
        [InlineData("word,next", "word,", true)]
        [InlineData("word[*]next", "word[*", true)]
        [InlineData("word[abc]next", "word[abc", true)]
        [InlineData("word", "word*", true)]
        [InlineData("Word", "word*", true)]
        [InlineData("word(Special)", "word*", true)]
        [InlineData("testword", "test", true)]
        [InlineData("word", "", true)]
        [InlineData("", "word", false)]
        public void TestDefaultMatch(string value, string wordToComplete, bool expected)
        {
            bool result = CompletionHelpers.DefaultMatch(value, wordToComplete);
            Assert.Equal(expected, result);
        }

        [Theory]
        [InlineData("word", "word", true)]
        [InlineData("Word", "word", true)]
        [InlineData("word", "wor", true)]
        [InlineData("word", "words", false)]
        [InlineData("word`nnext", "word`n", true)]
        [InlineData("word`r`nnext", "word`r`n", true)]
        [InlineData("word;next", "word;", true)]
        [InlineData("word,next", "word,", true)]
        [InlineData("word[*]next", "word[*", true)]
        [InlineData("word[abc]next", "word[abc", true)]
        [InlineData("word", "word*", false)]
        [InlineData("Word", "word*", false)]
        [InlineData("word(Special)", "word*", false)]
        [InlineData("testword", "test", true)]
        [InlineData("word", "", true)]
        [InlineData("", "word", false)]
        public void TestWildcardPatternEscapeMatch(string value, string wordToComplete, bool expected)
        {
            bool result = CompletionHelpers.WildcardPatternEscapeMatch(value, wordToComplete);
            Assert.Equal(expected, result);
        }

        [Theory]
        [InlineData("\n", "`n")]
        [InlineData("\r", "`r")]
        [InlineData("\r\n", "`r`n")]
        [InlineData("\t", "`t")]
        [InlineData("\0", "`0")]
        [InlineData("\a", "`a")]
        [InlineData("\b", "`b")]
        [InlineData("\u001b", "`e")]
        [InlineData("\f", "`f")]
        [InlineData("\v", "`v")]
        [InlineData("word\n", "word`n")]
        [InlineData("word\r", "word`r")]
        [InlineData("word\t", "word`t")]
        [InlineData("word\u001b", "word`e")]
        [InlineData("word\f", "word`f")]
        [InlineData("word\v", "word`v")]
        [InlineData("word\r\n", "word`r`n")]
        public void TestNormalizeToExpandableString(string value, string expected)
        {
            string result = CompletionHelpers.NormalizeToExpandableString(value);
            Assert.Equal(expected, result);
        }
    }
}