-
Notifications
You must be signed in to change notification settings - Fork 7
/
unittests.js
151 lines (131 loc) · 7.45 KB
/
unittests.js
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
/*
* Copyright (c) 2012 Adobe Systems Incorporated. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/*jslint vars: true, plusplus: true, devel: true, browser: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, describe, it, xit, expect, beforeEach, afterEach, waitsFor, runs, $, brackets, waitsForDone */
define(function (require, exports, module) {
"use strict";
var SpecRunnerUtils = brackets.getModule("spec/SpecRunnerUtils"),
Editor = brackets.getModule("editor/Editor").Editor,
SelectParent = require("SelectParent");
describe("Select Parent or Child", function () {
// 1 2 3 4
// 01234567890123456789012345678901234567890
var defaultContent = "function () {\n" +
" if (nest1) {\n" +
" if (nest2) {\n" +
" var x = { foo: 'bar' }; /*{*/ // }{\n" +
" } else {\n" +
" var y = { baz: 'boo' };\n" +
" }\n" +
" }\n" +
"}";
var testWindow;
var testDocument, testEditor;
beforeEach(function () {
// create dummy Document for the Editor
testDocument = SpecRunnerUtils.createMockDocument(defaultContent);
// create Editor instance (containing a CodeMirror instance)
$("body").append("<div id='editor'/>");
testEditor = new Editor(testDocument, true, $("#editor").get(0));
});
afterEach(function () {
testEditor.destroy();
testEditor = null;
$("#editor").remove();
testDocument = null;
SelectParent.maybeClearStack(true);
});
it("should expand once within innermost block", function () {
testEditor.setCursorPos(3, 23); // before "foo:"
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 3, ch: 21 }, end: { line: 3, ch: 35 } }); // x obj
});
it("should return to original selection after one expand", function () {
testEditor.setCursorPos(3, 23); // before "foo:"
SelectParent.selectParent(testEditor);
SelectParent.selectChild(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 3, ch: 23 }, end: { line: 3, ch: 23 } });
});
it("should expand twice from within innermost block", function () {
testEditor.setCursorPos(3, 23); // before "foo:"
SelectParent.selectParent(testEditor);
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 2, ch: 19 }, end: { line: 4, ch: 9 } }); // nest2 block
});
it("should contract twice after two expands", function () {
testEditor.setCursorPos(3, 23); // before "foo:"
SelectParent.selectParent(testEditor);
SelectParent.selectParent(testEditor);
SelectParent.selectChild(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 3, ch: 21 }, end: { line: 3, ch: 35 } }); // x obj
SelectParent.selectChild(testEditor);
result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 3, ch: 23 }, end: { line: 3, ch: 23 } });
});
it("should expand from before innermost block", function () {
testEditor.setCursorPos(3, 17); // before "x"
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 2, ch: 19 }, end: { line: 4, ch: 9 } }); // nest2 block
});
it("should expand from after innermost block", function () {
testEditor.setCursorPos(3, 35); // after x object close brace
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 2, ch: 19 }, end: { line: 4, ch: 9 } }); // nest2 block
});
it("should balance properly when selection contains equal but unbalanced braces", function () {
testEditor.setSelection({ line: 4, ch: 8 }, { line: 4, ch: 16 }); // around "} else {"
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 1, ch: 15 }, end: { line: 7, ch: 5 } }); // nest1 block
});
it("should balance properly when selection contains extra start brace", function () {
testEditor.setSelection({ line: 3, ch: 13 }, { line: 3, ch: 26 }); // around start of x obj
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 2, ch: 19 }, end: { line: 4, ch: 9 } }); // nest2 block
});
it("should balance properly when selection contains extra end brace", function () {
testEditor.setSelection({ line: 3, ch: 23 }, { line: 3, ch: 36 }); // around end of x obj
SelectParent.selectParent(testEditor);
var result = testEditor.getSelection();
expect(result).toEqual({ start: { line: 2, ch: 19 }, end: { line: 4, ch: 9 } }); // nest2 block
});
it("should not select child after stack is cleared", function () {
testEditor.setCursorPos(3, 23); // before "foo:"
SelectParent.selectParent(testEditor);
var parentSel = testEditor.getSelection();
expect(SelectParent.canSelectChild()).toBe(true);
SelectParent.maybeClearStack(true);
expect(SelectParent.canSelectChild()).toBe(false);
SelectParent.selectChild(testEditor); // should be a no op
var result = testEditor.getSelection();
expect(result).toEqual(parentSel);
});
});
});