-
Notifications
You must be signed in to change notification settings - Fork 347
/
importer_test.dart
222 lines (193 loc) · 6.71 KB
/
importer_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
221
222
// Copyright 2017 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
@TestOn('vm')
import 'dart:convert';
import 'package:test/test.dart';
import 'package:sass/sass.dart';
import 'from_import_importer.dart';
import 'test_importer.dart';
import '../utils.dart';
void main() {
test("uses an importer to resolve an @import", () {
var css = compileString('@import "orange";', importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) {
var color = url.path;
return ImporterResult('.$color {color: $color}', indented: false);
})
]);
expect(css, equals(".orange {\n color: orange;\n}"));
});
test("passes the canonicalized URL to the importer", () {
var css = compileString('@import "orange";', importers: [
TestImporter((url) => Uri.parse('u:blue'), (url) {
var color = url.path;
return ImporterResult('.$color {color: $color}', indented: false);
})
]);
expect(css, equals(".blue {\n color: blue;\n}"));
});
test("only invokes the importer once for a given canonicalization", () {
var css = compileString("""
@import "orange";
@import "orange";
""", importers: [
TestImporter(
(url) => Uri.parse('u:blue'),
expectAsync1((url) {
var color = url.path;
return ImporterResult('.$color {color: $color}', indented: false);
}, count: 1))
]);
expect(css, equals("""
.blue {
color: blue;
}
.blue {
color: blue;
}"""));
});
test("resolves URLs relative to the pre-canonicalized URL", () {
var times = 0;
var css = compileString('@import "foo:bar/baz";',
importers: [
TestImporter(
expectAsync1((url) {
times++;
if (times == 1) return Uri(path: 'first');
expect(url, equals(Uri.parse('foo:bar/bang')));
return Uri(path: 'second');
}, count: 2),
expectAsync1((url) {
return ImporterResult(
times == 1
? '''
.first {url: "$url"}
@import "bang";
'''
: '.second {url: "$url"}',
indented: false);
}, count: 2))
],
logger: Logger.quiet);
expect(css, equalsIgnoringWhitespace('''
.first { url: "first"; }
.second { url: "second"; }
'''));
});
group("the imported URL", () {
// Regression test for #1137.
test("isn't changed if it's root-relative", () {
compileString('@import "/orange";', importers: [
TestImporter(expectAsync1((url) {
expect(url, equals(Uri.parse("/orange")));
return Uri.parse("u:$url");
}), (url) => ImporterResult('', syntax: Syntax.scss))
]);
});
test("is converted to a file: URL if it's an absolute Windows path", () {
compileString('@import "C:/orange";', importers: [
TestImporter(expectAsync1((url) {
expect(url, equals(Uri.parse("file:///C:/orange")));
return Uri.parse("u:$url");
}), (url) => ImporterResult('', syntax: Syntax.scss))
]);
});
});
test("uses an importer's source map URL", () {
var result = compileStringToResult('@import "orange";',
importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) {
var color = url.path;
return ImporterResult('.$color {color: $color}',
sourceMapUrl: Uri.parse("u:blue"), indented: false);
})
],
sourceMap: true);
expect(result.sourceMap!.urls, contains("u:blue"));
});
test("uses a data: source map URL if the importer doesn't provide one", () {
var result = compileStringToResult('@import "orange";',
importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) {
var color = url.path;
return ImporterResult('.$color {color: $color}', indented: false);
})
],
sourceMap: true);
expect(
result.sourceMap!.urls,
contains(Uri.dataFromString(".orange {color: orange}", encoding: utf8)
.toString()));
});
test("wraps an error in canonicalize()", () {
expect(() {
compileString('@import "orange";', importers: [
TestImporter((url) {
throw "this import is bad actually";
}, expectNever1)
]);
}, throwsA(predicate((error) {
expect(error, const TypeMatcher<SassException>());
expect(
error.toString(), startsWith("Error: this import is bad actually"));
return true;
})));
});
test("wraps an error in load()", () {
expect(() {
compileString('@import "orange";', importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) {
throw "this import is bad actually";
})
]);
}, throwsA(predicate((error) {
expect(error, const TypeMatcher<SassException>());
expect(
error.toString(), startsWith("Error: this import is bad actually"));
return true;
})));
});
test("prefers .message to .toString() for an importer error", () {
expect(() {
compileString('@import "orange";', importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) {
throw FormatException("bad format somehow");
})
]);
}, throwsA(predicate((error) {
expect(error, const TypeMatcher<SassException>());
// FormatException.toString() starts with "FormatException:", but
// the error message should not.
expect(error.toString(), startsWith("Error: bad format somehow"));
return true;
})));
});
test("avoids importer when only load() returns null", () {
expect(() {
compileString('@import "orange";', importers: [
TestImporter((url) => Uri.parse("u:$url"), (url) => null)
]);
}, throwsA(predicate((error) {
expect(error, const TypeMatcher<SassException>());
expect(error.toString(),
startsWith("Error: Can't find stylesheet to import"));
return true;
})));
});
group("currentLoadFromImport is", () {
test("true from an @import", () {
compileString('@import "foo"', importers: [FromImportImporter(true)]);
});
test("false from a @use", () {
compileString('@use "foo"', importers: [FromImportImporter(false)]);
});
test("false from a @forward", () {
compileString('@forward "foo"', importers: [FromImportImporter(false)]);
});
test("false from meta.load-css", () {
compileString('@use "sass:meta"; @include meta.load-css("")',
importers: [FromImportImporter(false)]);
});
});
}