forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.unit.test.ts
More file actions
202 lines (179 loc) · 9.64 KB
/
utils.unit.test.ts
File metadata and controls
202 lines (179 loc) · 9.64 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as assert from 'assert';
import {
JSONRPC_CONTENT_LENGTH_HEADER,
JSONRPC_CONTENT_TYPE_HEADER,
JSONRPC_UUID_HEADER,
ExtractJsonRPCData,
parseJsonRPCHeadersAndData,
splitTestNameWithRegex,
argKeyExists,
addValueIfKeyNotExist,
} from '../../../client/testing/testController/common/utils';
suite('Test Controller Utils: JSON RPC', () => {
test('Empty raw data string', async () => {
const rawDataString = '';
const output = parseJsonRPCHeadersAndData(rawDataString);
assert.deepStrictEqual(output.headers.size, 0);
assert.deepStrictEqual(output.remainingRawData, '');
});
test('Valid data empty JSON', async () => {
const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: 2\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n{}`;
const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString);
assert.deepStrictEqual(rpcHeaders.headers.size, 3);
assert.deepStrictEqual(rpcHeaders.remainingRawData, '{}');
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, '{}');
});
test('Valid data NO JSON', async () => {
const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: 0\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n`;
const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString);
assert.deepStrictEqual(rpcHeaders.headers.size, 3);
assert.deepStrictEqual(rpcHeaders.remainingRawData, '');
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, '');
});
test('Valid data with full JSON', async () => {
// this is just some random JSON
const json =
'{"jsonrpc": "2.0", "method": "initialize", "params": {"processId": 1234, "rootPath": "/home/user/project", "rootUri": "file:///home/user/project", "capabilities": {}}, "id": 0}';
const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: ${json.length}\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n${json}`;
const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString);
assert.deepStrictEqual(rpcHeaders.headers.size, 3);
assert.deepStrictEqual(rpcHeaders.remainingRawData, json);
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, json);
});
test('Valid data with multiple JSON', async () => {
const json =
'{"jsonrpc": "2.0", "method": "initialize", "params": {"processId": 1234, "rootPath": "/home/user/project", "rootUri": "file:///home/user/project", "capabilities": {}}, "id": 0}';
const rawDataString = `${JSONRPC_CONTENT_LENGTH_HEADER}: ${json.length}\n${JSONRPC_CONTENT_TYPE_HEADER}: application/json\n${JSONRPC_UUID_HEADER}: 1234\n\n${json}`;
const rawDataString2 = rawDataString + rawDataString;
const rpcHeaders = parseJsonRPCHeadersAndData(rawDataString2);
assert.deepStrictEqual(rpcHeaders.headers.size, 3);
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, json);
assert.deepStrictEqual(rpcContent.remainingRawData, rawDataString);
});
test('Valid constant', async () => {
const data = `{"cwd": "/Users/eleanorboyd/testingFiles/inc_dec_example", "status": "success", "result": {"test_dup_class.test_a.TestSomething.test_a": {"test": "test_dup_class.test_a.TestSomething.test_a", "outcome": "success", "message": "None", "traceback": null, "subtest": null}}}`;
const secondPayload = `Content-Length: 270
Content-Type: application/json
Request-uuid: 496c86b1-608f-4886-9436-ec00538e144c
${data}`;
const payload = `Content-Length: 270
Content-Type: application/json
Request-uuid: 496c86b1-608f-4886-9436-ec00538e144c
${data}${secondPayload}`;
const rpcHeaders = parseJsonRPCHeadersAndData(payload);
assert.deepStrictEqual(rpcHeaders.headers.size, 3);
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, data);
assert.deepStrictEqual(rpcContent.remainingRawData, secondPayload);
});
test('Valid content length as only header with carriage return', async () => {
const payload = `Content-Length: 7
`;
const rpcHeaders = parseJsonRPCHeadersAndData(payload);
assert.deepStrictEqual(rpcHeaders.headers.size, 1);
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, '');
assert.deepStrictEqual(rpcContent.remainingRawData, '');
});
test('Valid content length header with no value', async () => {
const payload = `Content-Length:`;
const rpcHeaders = parseJsonRPCHeadersAndData(payload);
const rpcContent = ExtractJsonRPCData(rpcHeaders.headers.get('Content-Length'), rpcHeaders.remainingRawData);
assert.deepStrictEqual(rpcContent.extractedJSON, '');
assert.deepStrictEqual(rpcContent.remainingRawData, '');
});
suite('Test Controller Utils: Other', () => {
interface TestCase {
name: string;
input: string;
expectedParent: string;
expectedSubtest: string;
}
const testCases: Array<TestCase> = [
{
name: 'Single parameter, named',
input: 'test_package.ClassName.test_method (param=value)',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '(param=value)',
},
{
name: 'Single parameter, unnamed',
input: 'test_package.ClassName.test_method [value]',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '[value]',
},
{
name: 'Multiple parameters, named',
input: 'test_package.ClassName.test_method (param1=value1, param2=value2)',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '(param1=value1, param2=value2)',
},
{
name: 'Multiple parameters, unnamed',
input: 'test_package.ClassName.test_method [value1, value2]',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '[value1, value2]',
},
{
name: 'Names with special characters',
input: 'test_package.ClassName.test_method (param1=value/1, param2=value+2)',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '(param1=value/1, param2=value+2)',
},
{
name: 'Names with spaces',
input: 'test_package.ClassName.test_method ["a b c d"]',
expectedParent: 'test_package.ClassName.test_method',
expectedSubtest: '["a b c d"]',
},
];
testCases.forEach((testCase) => {
test(`splitTestNameWithRegex: ${testCase.name}`, () => {
const splitResult = splitTestNameWithRegex(testCase.input);
assert.deepStrictEqual(splitResult, [testCase.expectedParent, testCase.expectedSubtest]);
});
});
});
suite('Test Controller Utils: Args Mapping', () => {
suite('addValueIfKeyNotExist', () => {
test('should add key-value pair if key does not exist', () => {
const args = ['key1=value1', 'key2=value2'];
const result = addValueIfKeyNotExist(args, 'key3', 'value3');
assert.deepEqual(result, ['key1=value1', 'key2=value2', 'key3=value3']);
});
test('should not add key-value pair if key already exists', () => {
const args = ['key1=value1', 'key2=value2'];
const result = addValueIfKeyNotExist(args, 'key1', 'value3');
assert.deepEqual(result, ['key1=value1', 'key2=value2']);
});
test('should not add key-value pair if key exists as a solo element', () => {
const args = ['key1=value1', 'key2'];
const result = addValueIfKeyNotExist(args, 'key2', 'yellow');
assert.deepEqual(result, ['key1=value1', 'key2']);
});
test('add just key if value is null', () => {
const args = ['key1=value1', 'key2'];
const result = addValueIfKeyNotExist(args, 'key3', null);
assert.deepEqual(result, ['key1=value1', 'key2', 'key3']);
});
});
suite('argKeyExists', () => {
test('should return true if key exists', () => {
const args = ['key1=value1', 'key2=value2'];
const result = argKeyExists(args, 'key1');
assert.deepEqual(result, true);
});
test('should return false if key does not exist', () => {
const args = ['key1=value1', 'key2=value2'];
const result = argKeyExists(args, 'key3');
assert.deepEqual(result, false);
});
});
});
});