Skip to content

Commit fbabb13

Browse files
authored
Merge pull request microsoft#13792 from jchadwick/codeCoverage-editorState
Adding tests for editor state
2 parents bbba181 + 3a1bb2d commit fbabb13

1 file changed

Lines changed: 108 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
'use strict';
6+
7+
import * as assert from 'assert';
8+
import URI from 'vs/base/common/uri';
9+
import { CodeEditorStateFlag, ICommonCodeEditor, IModel } from 'vs/editor/common/editorCommon';
10+
import { EditorState } from 'vs/editor/common/core/editorState';
11+
import { Selection } from 'vs/editor/common/core/selection';
12+
import { Position } from 'vs/editor/common/core/position';
13+
14+
interface IStubEditorState {
15+
model?: { uri?: URI, version?: number };
16+
position?: Position;
17+
selection?: Selection;
18+
scroll?: { left?: number, top?: number };
19+
}
20+
21+
suite('Editor Core - Editor State', () => {
22+
23+
const allFlags = Object.keys(CodeEditorStateFlag)
24+
.map(k => CodeEditorStateFlag[k])
25+
.filter(v => typeof v === 'number') as number[];
26+
27+
28+
test('empty editor state should be valid', () => {
29+
let result = validate( {}, {} );
30+
assert.equal(result, true);
31+
});
32+
33+
test('different model URIs should be invalid', () => {
34+
let result = validate(
35+
{ model: { uri: URI.parse('http://test1') } },
36+
{ model: { uri: URI.parse('http://test2') } }
37+
);
38+
39+
assert.equal(result, false);
40+
});
41+
42+
test('different model versions should be invalid', () => {
43+
let result = validate(
44+
{ model: { version: 1 } },
45+
{ model: { version: 2 } }
46+
);
47+
48+
assert.equal(result, false);
49+
});
50+
51+
test('different positions should be invalid', () => {
52+
let result = validate(
53+
{ position: new Position(1, 2) },
54+
{ position: new Position(2, 3) }
55+
);
56+
57+
assert.equal(result, false);
58+
});
59+
60+
test('different selections should be invalid', () => {
61+
let result = validate(
62+
{ selection: new Selection(1, 2, 3, 4) },
63+
{ selection: new Selection(5, 2, 3, 4) }
64+
);
65+
66+
assert.equal(result, false);
67+
});
68+
69+
test('different scroll positions should be invalid', () => {
70+
let result = validate(
71+
{ scroll: { left: 1, top: 2 } },
72+
{ scroll: { left: 3, top: 2 } }
73+
);
74+
75+
assert.equal(result, false);
76+
});
77+
78+
79+
function validate(source: IStubEditorState, target: IStubEditorState) {
80+
let sourceEditor = createEditor(source),
81+
targetEditor = createEditor(target);
82+
83+
let result = new EditorState(sourceEditor, allFlags)
84+
.validate(targetEditor);
85+
86+
return result;
87+
}
88+
89+
function createEditor({ model, position, selection, scroll }: IStubEditorState = {}): ICommonCodeEditor {
90+
91+
let mappedModel = model
92+
? {
93+
uri: model.uri ? model.uri : URI.parse('http://dummy.org'),
94+
getVersionId: () => model.version
95+
}
96+
: null;
97+
98+
return <any>{
99+
getModel: (): IModel => <any>mappedModel,
100+
getPosition: (): Position => position,
101+
getSelection: (): Selection => selection,
102+
getScrollLeft: (): number => scroll && scroll.left,
103+
getScrollTop: (): number => scroll && scroll.top
104+
};
105+
}
106+
107+
});
108+

0 commit comments

Comments
 (0)