forked from sass/dart-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdart_api_test.dart
228 lines (189 loc) · 7.89 KB
/
dart_api_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
223
224
225
226
227
228
// 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 'package:package_resolver/package_resolver.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';
import 'package:test_descriptor/test_descriptor.dart' as d;
import 'package:sass/sass.dart';
import 'package:sass/src/exception.dart';
void main() {
// TODO(nweiz): test SASS_PATH when dart-lang/sdk#28160 is fixed.
group("importers", () {
test("is used to resolve imports", () async {
await d.dir("subdir", [d.file("subtest.scss", "a {b: c}")]).create();
await d.file("test.scss", '@import "subtest.scss";').create();
var css = compile(d.path("test.scss"),
importers: [FilesystemImporter(d.path('subdir'))]);
expect(css, equals("a {\n b: c;\n}"));
});
test("are checked in order", () async {
await d
.dir("first", [d.file("other.scss", "a {b: from-first}")]).create();
await d
.dir("second", [d.file("other.scss", "a {b: from-second}")]).create();
await d.file("test.scss", '@import "other";').create();
var css = compile(d.path("test.scss"), importers: [
FilesystemImporter(d.path('first')),
FilesystemImporter(d.path('second'))
]);
expect(css, equals("a {\n b: from-first;\n}"));
});
});
group("loadPaths", () {
test("is used to import file: URLs", () async {
await d.dir("subdir", [d.file("subtest.scss", "a {b: c}")]).create();
await d.file("test.scss", '@import "subtest.scss";').create();
var css = compile(d.path("test.scss"), loadPaths: [d.path('subdir')]);
expect(css, equals("a {\n b: c;\n}"));
});
test("can import partials", () async {
await d.dir("subdir", [d.file("_subtest.scss", "a {b: c}")]).create();
await d.file("test.scss", '@import "subtest.scss";').create();
var css = compile(d.path("test.scss"), loadPaths: [d.path('subdir')]);
expect(css, equals("a {\n b: c;\n}"));
});
test("adds a .scss extension", () async {
await d.dir("subdir", [d.file("subtest.scss", "a {b: c}")]).create();
await d.file("test.scss", '@import "subtest";').create();
var css = compile(d.path("test.scss"), loadPaths: [d.path('subdir')]);
expect(css, equals("a {\n b: c;\n}"));
});
test("adds a .sass extension", () async {
await d.dir("subdir", [d.file("subtest.sass", "a\n b: c")]).create();
await d.file("test.scss", '@import "subtest";').create();
var css = compile(d.path("test.scss"), loadPaths: [d.path('subdir')]);
expect(css, equals("a {\n b: c;\n}"));
});
test("are checked in order", () async {
await d
.dir("first", [d.file("other.scss", "a {b: from-first}")]).create();
await d
.dir("second", [d.file("other.scss", "a {b: from-second}")]).create();
await d.file("test.scss", '@import "other";').create();
var css = compile(d.path("test.scss"),
loadPaths: [d.path('first'), d.path('second')]);
expect(css, equals("a {\n b: from-first;\n}"));
});
});
group("packageResolver", () {
test("is used to import package: URLs", () async {
await d.dir("subdir", [d.file("test.scss", "a {b: 1 + 2}")]).create();
await d
.file("test.scss", '@import "package:fake_package/test";')
.create();
var resolver = SyncPackageResolver.config(
{"fake_package": p.toUri(d.path('subdir'))});
var css = compile(d.path("test.scss"), packageResolver: resolver);
expect(css, equals("a {\n b: 3;\n}"));
});
test("can resolve relative paths in a package", () async {
await d.dir("subdir", [
d.file("test.scss", "@import 'other'"),
d.file("_other.scss", "a {b: 1 + 2}"),
]).create();
await d
.file("test.scss", '@import "package:fake_package/test";')
.create();
var resolver = SyncPackageResolver.config(
{"fake_package": p.toUri(d.path('subdir'))});
var css = compile(d.path("test.scss"), packageResolver: resolver);
expect(css, equals("a {\n b: 3;\n}"));
});
test("doesn't import a package URL from a missing package", () async {
await d
.file("test.scss", '@import "package:fake_package/test_aux";')
.create();
var resolver = SyncPackageResolver.config({});
expect(() => compile(d.path("test.scss"), packageResolver: resolver),
throwsA(const TypeMatcher<SassRuntimeException>()));
});
});
group("import precedence", () {
test("relative imports take precedence over importers", () async {
await d.dir(
"subdir", [d.file("other.scss", "a {b: from-load-path}")]).create();
await d.file("other.scss", "a {b: from-relative}").create();
await d.file("test.scss", '@import "other";').create();
var css = compile(d.path("test.scss"),
importers: [FilesystemImporter(d.path('subdir'))]);
expect(css, equals("a {\n b: from-relative;\n}"));
});
test("the original importer takes precedence over other importers",
() async {
await d.dir(
"original", [d.file("other.scss", "a {b: from-original}")]).create();
await d
.dir("other", [d.file("other.scss", "a {b: from-other}")]).create();
var css = compileString('@import "other";',
importer: FilesystemImporter(d.path('original')),
url: p.toUri(d.path('original/test.scss')),
importers: [FilesystemImporter(d.path('other'))]);
expect(css, equals("a {\n b: from-original;\n}"));
});
test("importers take precedence over load paths", () async {
await d.dir("load-path",
[d.file("other.scss", "a {b: from-load-path}")]).create();
await d.dir(
"importer", [d.file("other.scss", "a {b: from-importer}")]).create();
await d.file("test.scss", '@import "other";').create();
var css = compile(d.path("test.scss"),
importers: [FilesystemImporter(d.path('importer'))],
loadPaths: [d.path('load-path')]);
expect(css, equals("a {\n b: from-importer;\n}"));
});
test("importers take precedence over packageResolver", () async {
await d.dir("package",
[d.file("other.scss", "a {b: from-package-resolver}")]).create();
await d.dir(
"importer", [d.file("other.scss", "a {b: from-importer}")]).create();
await d
.file("test.scss", '@import "package:fake_package/other";')
.create();
var css = compile(d.path("test.scss"),
importers: [
PackageImporter(SyncPackageResolver.config(
{"fake_package": p.toUri(d.path('importer'))}))
],
packageResolver: SyncPackageResolver.config(
{"fake_package": p.toUri(d.path('package'))}));
expect(css, equals("a {\n b: from-importer;\n}"));
});
});
group("charset", () {
group("= true", () {
test("doesn't emit @charset for a pure-ASCII stylesheet", () {
expect(compileString("a {b: c}"), equals("""
a {
b: c;
}"""));
});
test("emits @charset with expanded output", () async {
expect(compileString("a {b: 👭}"), equals("""
@charset "UTF-8";
a {
b: 👭;
}"""));
});
test("emits a BOM with compressed output", () async {
expect(compileString("a {b: 👭}", style: OutputStyle.compressed),
equals("\u{FEFF}a{b:👭}"));
});
});
group("= false", () {
test("doesn't emit @charset with expanded output", () async {
expect(compileString("a {b: 👭}", charset: false), equals("""
a {
b: 👭;
}"""));
});
test("emits a BOM with compressed output", () async {
expect(
compileString("a {b: 👭}",
charset: false, style: OutputStyle.compressed),
equals("a{b:👭}"));
});
});
});
}