forked from dart-lang/webdev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocation_test.dart
220 lines (181 loc) · 7.44 KB
/
location_test.dart
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2020 The Dart Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@Timeout(Duration(minutes: 2))
library;
import 'package:dwds/src/debugging/location.dart';
import 'package:dwds/src/utilities/dart_uri.dart';
import 'package:test/test.dart';
import 'fixtures/fakes.dart';
import 'fixtures/utilities.dart';
final sourceMapContents =
'{"version":3,"sourceRoot":"","sources":["main.dart"],"names":[],'
'"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAUwB,IAAtB,WAAM;AAKJ,'
'IAHF,4BAAkB,aAAa,SAAC,GAAG;AACb,MAApB,WAAM;AACN,YAAgC,+CAAO,AAAK,oBAAO,'
'yCAAC,WAAW;IAChE;AAC0D,IAA3D,AAAS,AAAK,0DAAO;AAAe,kBAAO;;;AAEvC,gBAAQ;'
'AAGV,IAFI,kCAAqC,QAAC;AACX,MAA/B,WAAM,AAAwB,0BAAP,QAAF,AAAE,KAAK,GAAP;'
';EAEzB","file":"main.ddc.js"}';
void main() {
const lines = 100;
const lineLength = 150;
final assetReader = FakeAssetReader(sourceMap: sourceMapContents);
final toolConfiguration = TestToolConfiguration.withLoadStrategy(
loadStrategy: MockLoadStrategy(assetReader),
);
setGlobalsForTesting(
toolConfiguration: toolConfiguration,
);
final dartUri = DartUri('org-dartlang-app://web/main.dart');
final modules = FakeModules(module: _module);
final locations = Locations(assetReader, modules, '');
locations.initialize('fake_entrypoint');
group('JS locations |', () {
const fakeRuntimeScriptId = '12';
group('location |', () {
test('is zero based', () {
final loc =
JsLocation.fromZeroBased(_module, 0, 0, fakeRuntimeScriptId);
expect(loc, _matchJsLocation(0, 0));
});
test('can compare to other location', () {
final loc00 =
JsLocation.fromZeroBased(_module, 0, 0, fakeRuntimeScriptId);
final loc01 =
JsLocation.fromZeroBased(_module, 0, 1, fakeRuntimeScriptId);
final loc10 =
JsLocation.fromZeroBased(_module, 1, 0, fakeRuntimeScriptId);
final loc11 =
JsLocation.fromZeroBased(_module, 1, 1, fakeRuntimeScriptId);
expect(loc00.compareTo(loc01), isNegative);
expect(loc00.compareTo(loc10), isNegative);
expect(loc00.compareTo(loc10), isNegative);
expect(loc00.compareTo(loc11), isNegative);
expect(loc00.compareTo(loc00), isZero);
expect(loc11.compareTo(loc00), isPositive);
});
});
group('best location |', () {
test('does not return location for setup code', () async {
final location = await locations.locationForJs(_module, 0, 0);
expect(location, isNull);
});
test('prefers precise match', () async {
final location = await locations.locationForJs(_module, 37, 0);
expect(location, _matchLocationForJs(37, 0));
});
test('finds a match in the beginning of the line', () async {
final location = await locations.locationForJs(_module, 39, 4);
expect(location, _matchLocationForJs(39, 0));
});
test('finds a match in the middle of the line', () async {
final location = await locations.locationForJs(_module, 39, 10);
expect(location, _matchLocationForJs(39, 6));
});
test('finds a match on a previous line', () async {
final location = await locations.locationForJs(_module, 44, 0);
expect(location, _matchLocationForJs(43, 18));
});
test('finds a match on a previous line with a closer match after',
() async {
final location =
await locations.locationForJs(_module, 44, lineLength - 1);
expect(location, _matchLocationForJs(43, 18));
});
test('finds a match on the last line', () async {
final location =
await locations.locationForJs(_module, lines - 1, lineLength - 1);
expect(location, _matchLocationForJs(50, 2));
});
test('finds a match on invalid line', () async {
final location =
await locations.locationForJs(_module, lines, lineLength - 1);
expect(location, _matchLocationForJs(50, 2));
});
test('finds a match on invalid column on the same line', () async {
final location = await locations.locationForJs(_module, 50, lineLength);
expect(location, _matchLocationForJs(50, 2));
});
test('finds a match on invalid column on a previous line', () async {
final location =
await locations.locationForJs(_module, lines - 1, lineLength);
expect(location, _matchLocationForJs(50, 2));
});
});
});
group('Dart locations |', () {
group('location |', () {
test('is one based', () async {
final loc = DartLocation.fromZeroBased(dartUri, 0, 0);
expect(loc, _matchDartLocation(1, 1));
});
test('can compare to other locations', () async {
final loc00 = DartLocation.fromZeroBased(dartUri, 0, 0);
final loc01 = DartLocation.fromZeroBased(dartUri, 0, 1);
final loc10 = DartLocation.fromZeroBased(dartUri, 1, 0);
final loc11 = DartLocation.fromZeroBased(dartUri, 1, 1);
expect(loc00.compareTo(loc01), isNegative);
expect(loc00.compareTo(loc10), isNegative);
expect(loc00.compareTo(loc10), isNegative);
expect(loc00.compareTo(loc11), isNegative);
expect(loc00.compareTo(loc00), isZero);
expect(loc11.compareTo(loc00), isPositive);
});
});
group('best location |', () {
test('does not return location for dart lines not mapped to JS',
() async {
final location = await locations.locationForDart(dartUri, 0, 0);
expect(location, isNull);
});
test('returns location after on the same line', () async {
final location = await locations.locationForDart(dartUri, 11, 0);
expect(location, _matchLocationForDart(11, 3));
});
test('return null on invalid line', () async {
final location = await locations.locationForDart(dartUri, lines, 0);
expect(location, isNull);
});
test('return null on invalid column', () async {
final location =
await locations.locationForDart(dartUri, lines - 1, lineLength);
expect(location, isNull);
});
});
});
}
Matcher _matchLocationForDart(int line, int column) => isA<Location>().having(
(l) => l.dartLocation,
'dartLocation',
_matchDartLocation(line, column),
);
Matcher _matchLocationForJs(int line, int column) => isA<Location>()
.having((l) => l.jsLocation, 'jsLocation', _matchJsLocation(line, column));
Matcher _matchDartLocation(int line, int column) => isA<DartLocation>()
.having((l) => l.line, 'line', line)
.having((l) => l.column, 'column', column);
Matcher _matchJsLocation(int line, int column) => isA<JsLocation>()
.having((l) => l.line, 'line', line)
.having((l) => l.column, 'column', column);
const _module = 'packages/module';
const _serverPath = 'web/main.dart';
const _sourceMapPath = 'packages/module.js.map';
class MockLoadStrategy extends FakeStrategy {
MockLoadStrategy(super.assetReader);
@override
Future<String?> moduleForServerPath(
String entrypoint,
String serverPath,
) async =>
_module;
@override
Future<String> serverPathForModule(String entrypoint, String module) async =>
_serverPath;
@override
Future<String> sourceMapPathForModule(
String entrypoint,
String module,
) async =>
_sourceMapPath;
@override
String serverPathForAppUri(String appUri) => _serverPath;
}