-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathconstant_values_test.dart
207 lines (183 loc) · 6.36 KB
/
constant_values_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
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'dartdoc_test_base.dart';
import 'src/utils.dart';
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(ConstantValuesTest);
defineReflectiveTests(HiddenConstantsTest);
});
}
// TODO(srawlins): Sort members after a code review for the
// test_reflective_loader migration.
@reflectiveTest
class ConstantValuesTest extends DartdocTestBase {
@override
String get libraryName => 'constant_values';
void test_nonGenericFunctionReference() async {
var library = await bootPackageWithLibrary('''
void func() {}
const aFunc = func;
''');
var aFuncConstant = library.constants.named('aFunc');
expect(aFuncConstant.fullyQualifiedName, equals('constant_values.aFunc'));
expect(aFuncConstant.constantValue, equals('func'));
}
void test_genericFunctionReference() async {
var library = await bootPackageWithLibrary('''
void funcTypeParams<T extends String, U extends num>(
T something, U different) {}
const aFuncParams = funcTypeParams;
''');
var aFuncParamsConstant = library.constants.named('aFuncParams');
expect(aFuncParamsConstant.constantValue, equals('funcTypeParams'));
}
void test_genericFunctionReferenceWithTypeArgs() async {
var library = await bootPackageWithLibrary('''
void funcTypeParams<T extends String, U extends num>(
T something, U different) {}
const aFuncWithArgs = funcTypeParams<String, int>;
''');
var aFuncWithArgs = library.constants.named('aFuncWithArgs');
expect(aFuncWithArgs.constantValue,
equals('funcTypeParams<String, int>'));
}
void test_namedConstructorReference() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F.alternative();
}
const aTearOffNamedConstructor = F.alternative;
''');
var aTearOffNamedConstructor =
library.constants.named('aTearOffNamedConstructor');
expect(aTearOffNamedConstructor.constantValue, equals('F.alternative'));
}
void test_namedConstructorReferenceWithTypeArgs() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F.alternative();
}
const aTearOffNamedConstructorArgs = F<int>.alternative;
''');
var aTearOffNamedConstructorArgs =
library.constants.named('aTearOffNamedConstructorArgs');
expect(aTearOffNamedConstructorArgs.constantValue,
equals('F<int>.alternative'));
}
void test_unnamedConstructorReference() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F();
}
const aTearOffUnnamedConstructor = F.new;
''');
var aTearOffUnnamedConstructor =
library.constants.named('aTearOffUnnamedConstructor');
expect(aTearOffUnnamedConstructor.constantValue, equals('F.new'));
}
void test_unnamedConstructorReferenceWithTypeArgs() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F();
}
const aTearOffUnnamedConstructorArgs = F<String>.new;
''');
var aTearOffUnnamedConstructorArgs =
library.constants.named('aTearOffUnnamedConstructorArgs');
expect(aTearOffUnnamedConstructorArgs.constantValue,
equals('F<String>.new'));
}
void test_unnamedTypedefConstructorReference() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F();
}
const aTearOffUnnamedConstructorTypedef = Fstring.new;
''');
var aTearOffUnnamedConstructorTypedef =
library.constants.named('aTearOffUnnamedConstructorTypedef');
expect(
aTearOffUnnamedConstructorTypedef.constantValue, equals('Fstring.new'));
}
void test_unnamedTypedefConstructorReferenceWithTypeArgs() async {
var library = await bootPackageWithLibrary('''
class F<T> {
F();
}
const aTearOffUnnamedConstructorArgsTypedef = Ft<String>.new;
''');
var aTearOffUnnamedConstructorArgsTypedef =
library.constants.named('aTearOffUnnamedConstructorArgsTypedef');
expect(aTearOffUnnamedConstructorArgsTypedef.constantValue,
equals('Ft<String>.new'));
}
void test_namedParametersInConstInvocationValue_specifiedLast() async {
var library = await bootPackageWithLibrary('''
class C {
const C(int a, int b, {required int c, required int d});
}
const p = C(1, 2, c: 3, d: 4);
''');
var pConst = library.constants.named('p');
expect(pConst.constantValue,
equals('<a href="$linkPrefix/C/C.html">C</a>(1, 2, c: 3, d: 4)'));
}
void test_namedParametersInConstInvocationValue_specifiedAnywhere() async {
var library = await bootPackageWithLibrary('''
class C {
const C(int a, int b, {required int c, required int d});
}
const q = C(1, c: 2, 3, d: 4);
''');
var qConst = library.constants.named('q');
expect(qConst.constantValue,
equals('<a href="$linkPrefix/C/C.html">C</a>(1, c: 2, 3, d: 4)'));
}
void test_namedParametersInConstInvocationValue_specifiedFirst() async {
var library = await bootPackageWithLibrary('''
class C {
const C(int a, int b, {required int c, required int d});
}
const r = C(c: 1, d: 2, 3, 4);
''');
var rConst = library.constants.named('r');
expect(rConst.constantValue,
equals('<a href="$linkPrefix/C/C.html">C</a>(c: 1, d: 2, 3, 4)'));
}
}
@reflectiveTest
class HiddenConstantsTest extends DartdocTestBase {
@override
String get libraryName => 'hidden_constants';
void test_field() async {
var library = await bootPackageWithLibrary('''
/// Some documentation.
class A {
static const int aConst = 12;
}
''');
var aClass = library.classes.named('A');
var aConst = aClass.constantFields.named('aConst');
expect(aConst.hasConstantValueForDisplay, isFalse);
expect(aClass.documentation, equals('Some documentation.'));
}
void test_topLevel() async {
var library = await bootPackageWithLibrary('''
class A {
static const int aConst = 12;
}
static const aTopLevelConst = 37;
''', libraryPreamble: '''
/// Some documentation.
''');
var aConst = library.classes.named('A').constantFields.named('aConst');
expect(aConst.hasConstantValueForDisplay, isFalse);
var aTopLevelConst = library.constants.named('aTopLevelConst');
expect(aTopLevelConst.hasConstantValueForDisplay, isFalse);
expect(library.documentation, equals('Some documentation.'));
}
}