Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/editTelemetry/test/node/arcTracker.test.ts
4784 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
8
import { StringText } from '../../../../../editor/common/core/text/abstractText.js';
9
import { ArcTracker } from '../../common/arcTracker.js';
10
import { FileAccess } from '../../../../../base/common/network.js';
11
import { readFileSync } from 'fs';
12
import { join, resolve } from '../../../../../base/common/path.js';
13
import { StringEdit, StringReplacement } from '../../../../../editor/common/core/edits/stringEdit.js';
14
import { OffsetRange } from '../../../../../editor/common/core/ranges/offsetRange.js';
15
import { ensureDependenciesAreSet } from '../../../../../editor/common/core/text/positionToOffset.js';
16
17
suite('ArcTracker', () => {
18
ensureNoDisposablesAreLeakedInTestSuite();
19
ensureDependenciesAreSet();
20
21
const fixturesOutDir = FileAccess.asFileUri('vs/workbench/contrib/editTelemetry/test/node/data').fsPath;
22
const fixturesSrcDir = resolve(fixturesOutDir).replaceAll('\\', '/').replace('/out/vs/workbench/', '/src/vs/workbench/');
23
24
function getData(name: string): IEdits {
25
const path = join(fixturesSrcDir, name + '.edits.w.json');
26
const src = readFileSync(path, 'utf8');
27
return JSON.parse(src);
28
}
29
30
test('issue-264048', () => {
31
const stats = runTestWithData(getData('issue-264048'));
32
assert.deepStrictEqual(stats, ([
33
{
34
arc: 8,
35
deletedLineCounts: 1,
36
insertedLineCounts: 1
37
},
38
{
39
arc: 8,
40
deletedLineCounts: 0,
41
insertedLineCounts: 1
42
},
43
{
44
arc: 8,
45
deletedLineCounts: 0,
46
insertedLineCounts: 1
47
}
48
]));
49
});
50
51
test('line-insert', () => {
52
const stats = runTestWithData(getData('line-insert'));
53
assert.deepStrictEqual(stats, ([
54
{
55
arc: 7,
56
deletedLineCounts: 0,
57
insertedLineCounts: 1
58
},
59
{
60
arc: 5,
61
deletedLineCounts: 0,
62
insertedLineCounts: 1
63
}
64
]));
65
});
66
67
test('line-modification', () => {
68
const stats = runTestWithData(getData('line-modification'));
69
assert.deepStrictEqual(stats, ([
70
{
71
arc: 6,
72
deletedLineCounts: 1,
73
insertedLineCounts: 1
74
},
75
{
76
arc: 6,
77
deletedLineCounts: 1,
78
insertedLineCounts: 1
79
},
80
{
81
arc: 0,
82
deletedLineCounts: 0,
83
insertedLineCounts: 0
84
}
85
]));
86
});
87
88
test('multiline-insert', () => {
89
const stats = runTestWithData(getData('multiline-insert'));
90
assert.deepStrictEqual(stats, ([
91
{
92
arc: 24,
93
deletedLineCounts: 0,
94
insertedLineCounts: 3
95
},
96
{
97
arc: 23,
98
deletedLineCounts: 0,
99
insertedLineCounts: 2
100
}
101
]));
102
});
103
});
104
105
interface IEdits {
106
initialText: string;
107
edits: Array<{
108
replacements: Array<{
109
start: number;
110
endEx: number;
111
text: string;
112
}>;
113
}>;
114
}
115
116
function createStringEditFromJson(editData: IEdits['edits'][0]): StringEdit {
117
const replacements = editData.replacements.map(replacement =>
118
new StringReplacement(
119
OffsetRange.ofStartAndLength(replacement.start, replacement.endEx - replacement.start),
120
replacement.text
121
)
122
);
123
return new StringEdit(replacements);
124
}
125
126
function runTestWithData(data: IEdits): unknown {
127
const edits = data.edits.map(editData => createStringEditFromJson(editData));
128
129
const t = new ArcTracker(
130
new StringText(data.initialText),
131
edits[0]
132
);
133
134
const stats: unknown[] = [];
135
stats.push(t.getValues());
136
let lastLineNumbers = t.getLineCountInfo().insertedLineCounts;
137
let lastArc = t.getAcceptedRestrainedCharactersCount();
138
139
for (let i = 1; i < edits.length; i++) {
140
t.handleEdits(edits[i]);
141
stats.push(t.getValues());
142
143
const newLineNumbers = t.getLineCountInfo().insertedLineCounts;
144
assert.ok(newLineNumbers <= lastLineNumbers, `Line numbers must not increase. Last: ${lastLineNumbers}, new: ${newLineNumbers}`);
145
lastLineNumbers = newLineNumbers;
146
147
const newArc = t.getAcceptedRestrainedCharactersCount();
148
assert.ok(newArc <= lastArc, `ARC must not increase. Last: ${lastArc}, new: ${newArc}`);
149
lastArc = newArc;
150
}
151
return stats;
152
}
153
154