/
function_test.dart
157 lines (136 loc) · 5.13 KB
/
function_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
// Copyright 2018 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:test/test.dart';
import 'package:sass/sass.dart';
import 'package:sass/src/exception.dart';
main() {
test(
"new Callable() throws a SassFormatException if the argument list is "
"invalid", () {
expect(() => Callable("foo", "arg", (_) => sassNull),
throwsA(const TypeMatcher<SassFormatException>()));
});
test(
"new AsyncCallable() throws a SassFormatException if the argument list "
"is invalid", () {
expect(() => AsyncCallable("foo", "arg", (_) async => sassNull),
throwsA(const TypeMatcher<SassFormatException>()));
});
test("passes an argument to a custom function and uses its return value", () {
var css = compileString('a {b: foo(bar)}', functions: [
Callable("foo", r"$arg", expectAsync1((arguments) {
expect(arguments, hasLength(1));
expect(arguments.first.assertString().text, equals("bar"));
return SassString("result", quotes: false);
}))
]);
expect(css, equalsIgnoringWhitespace("a { b: result; }"));
});
test("runs a function asynchronously", () async {
var css = await compileStringAsync('a {b: foo(bar)}', functions: [
AsyncCallable("foo", r"$arg", expectAsync1((arguments) async {
expect(arguments, hasLength(1));
expect(arguments.first.assertString().text, equals("bar"));
await pumpEventQueue();
return SassString("result", quotes: false);
}))
]);
expect(css, equalsIgnoringWhitespace("a { b: result; }"));
});
test("passes no arguments to a custom function", () {
expect(
compileString('a {b: foo()}', functions: [
Callable("foo", "", expectAsync1((arguments) {
expect(arguments, isEmpty);
return sassNull;
}))
]),
isEmpty);
});
test("passes multiple arguments to a custom function", () {
expect(
compileString('a {b: foo(x, y, z)}', functions: [
Callable("foo", r"$arg1, $arg2, $arg3", expectAsync1((arguments) {
expect(arguments, hasLength(3));
expect(arguments[0].assertString().text, equals("x"));
expect(arguments[1].assertString().text, equals("y"));
expect(arguments[2].assertString().text, equals("z"));
return sassNull;
}))
]),
isEmpty);
});
test("gracefuly handles a custom function throwing", () {
expect(() {
compileString('a {b: foo()}',
functions: [Callable("foo", "", (arguments) => throw "heck")]);
}, throwsA(const TypeMatcher<SassException>()));
});
test("gracefuly handles a custom function returning null", () {
expect(() {
compileString('a {b: foo()}',
functions: [Callable("foo", "", (arguments) => null)]);
}, throwsA(const TypeMatcher<SassException>()));
});
test("supports default argument values", () {
var css = compileString('a {b: foo()}', functions: [
Callable("foo", r"$arg: 1", expectAsync1((arguments) {
expect(arguments, hasLength(1));
expect(arguments.first.assertNumber().value, equals(1));
return arguments.first;
}))
]);
expect(css, equalsIgnoringWhitespace("a { b: 1; }"));
});
test("supports argument lists", () {
var css = compileString('a {b: foo(1, 2, 3)}', functions: [
Callable("foo", r"$args...", expectAsync1((arguments) {
expect(arguments, hasLength(1));
var list = arguments[0] as SassArgumentList;
expect(list.asList, hasLength(3));
expect(list.asList[0].assertNumber().value, equals(1));
expect(list.asList[1].assertNumber().value, equals(2));
expect(list.asList[2].assertNumber().value, equals(3));
return arguments.first;
}))
]);
expect(css, equalsIgnoringWhitespace("a { b: 1, 2, 3; }"));
});
test("supports keyword arguments", () {
var css = compileString(r'a {b: foo($bar: 1)}', functions: [
Callable("foo", r"$args...", expectAsync1((arguments) {
expect(arguments, hasLength(1));
var list = arguments[0] as SassArgumentList;
expect(list.asList, hasLength(0));
expect(list.keywords, contains("bar"));
expect(list.keywords["bar"].assertNumber().value, equals(1));
return list.keywords["bar"];
}))
]);
expect(css, equalsIgnoringWhitespace("a { b: 1; }"));
});
group("are dash-normalized", () {
test("when defined with dashes", () {
expect(
compileString('a {b: foo_bar()}', functions: [
Callable("foo-bar", "", expectAsync1((arguments) {
expect(arguments, isEmpty);
return sassNull;
}))
]),
isEmpty);
});
test("when defined with underscores", () {
expect(
compileString('a {b: foo-bar()}', functions: [
Callable("foo_bar", "", expectAsync1((arguments) {
expect(arguments, isEmpty);
return sassNull;
}))
]),
isEmpty);
});
});
}