FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hack-codegen/tests/CodegenClassTest.hack at main · hhvm/hack-codegen · GitHub
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
hhvm
/
hack-codegen
Public archive
Notifications
You must be signed in to change notification settings
Fork
92
Star
348
Code
Issues
5
Pull requests
2
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
hack-codegen
/
tests
/
CodegenClassTest.hack
Copy path
More file actions
More file actions
Latest commit
History
History
History
353 lines (309 loc) · 11 KB
Breadcrumbs
hack-codegen
/
tests
/
CodegenClassTest.hack
Copy path
File metadata and controls
353 lines (309 loc) · 11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace
Facebook\HackCodegen
;
final
class
CodegenClassTest
extends
CodegenBaseTest
{
public
function
testDocblock
():
void
{
$code
=
$this
->
getCodegenFactory()
->
codegenClass(
'
TestDocblock
'
)
->
setDocBlock(
'
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
'
.
'
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
'
.
'
Ut enim ad minim veniam, quis nostrud exercitation ullamco
'
.
"
laboris nisi ut aliquip ex ea commodo consequat.
\n
"
.
"
Understood?
\n
"
.
'
Yes!
'
,
)
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testExtendsAndFinal
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
NothingHere
'
)
->
setExtends(
'
NothingHereBase
'
)
->
addInterface(
$cgf
->
codegenImplementsInterface(
'
JustOneInterface
'
))
->
setIsFinal()
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testInterfacesAndAbstract
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
NothingHere
'
)
->
addInterface(
$cgf
->
codegenImplementsInterface(
'
INothing
'
))
->
addInterface(
$cgf
->
codegenImplementsInterface(
'
IMeh
'
)
->
setGeneratedFrom(
$cgf
->
codegenGeneratedFromMethod(
'
Foo
'
,
'
Bar
'
)),
)
->
setIsAbstract()
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testMultipleInterfaces
():
void
{
$interfaces
=
Vector
{
'
IHarryPotter
'
,
'
IHermioneGranger
'
,
'
IRonWeasley
'
};
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
JKRowling
'
)
->
addInterfaces(
$cgf
->
codegenImplementsInterfaces(
$interfaces
))
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testLongClassDeclaration
():
void
{
//
The class declaration is just long enough (82 chars) to make it wrap
$code
=
$this
->
getCodegenFactory()
->
codegenClass(
'
ClassWithReallyLongName
'
)
->
setExtends(
'
NowThisIsTheParentClassWithALongNameItSelf
'
)
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testLongClassDeclarationWithInterfaces
():
void
{
$interfaces
=
Vector
{
'
InterfaceUno
'
,
'
InterfaceDos
'
,
'
InterfaceTres
'
};
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
ClassWithReallyReallyLongName
'
)
->
setExtends(
'
NowThisIsTheParentClassWithALongNameItSelf
'
)
->
addInterfaces(
$cgf
->
codegenImplementsInterfaces(
$interfaces
))
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testClassDeclarationWithGenerics
():
void
{
$generics_decl
=
vec
[
'
Tent as Ixyz
'
,
'
T
'
,
'
Tstory as EntCreationStory<Tent>
'
];
$code
=
$this
->
getCodegenFactory()
->
codegenClass(
'
ClassWithGenerics
'
)
->
addGenerics(
$generics_decl
)
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testDemo
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
Demo
'
)
->
addTrait(
$cgf
->
codegenUsesTrait(
'
EntProvisionalMode
'
))
->
addTrait(
$cgf
->
codegenUsesTrait(
'
WhateverTrait
'
)
->
setGeneratedFrom(
$cgf
->
codegenGeneratedFromMethod(
'
Whatever
'
,
'
Method
'
),
),
)
->
addTrait(
$cgf
->
codegenUsesTrait(
'
Useless
'
))
->
addConstant(
$cgf
->
codegenClassConstant(
'
MAX_SIZE
'
)
->
setValue(
256
,
HackBuilderValues
::
export()),
)
->
addConstant(
$cgf
->
codegenClassConstant(
'
DEFAULT_NAME
'
)
->
setValue(
'
MyEnt
'
,
HackBuilderValues
::
export())
->
setDocBlock(
'
Default name of Ent.
'
),
)
->
addConstant(
$cgf
->
codegenClassConstant(
'
PI
'
)
->
setValue(
3.1415
,
HackBuilderValues
::
export()),
)
->
setHasManualMethodSection()
->
setHasManualDeclarations()
->
addProperty(
$cgf
->
codegenProperty(
'
text
'
)
->
setProtected()
->
setType(
'
string
'
),
)
->
addProperty(
$cgf
->
codegenProperty(
'
id
'
)
->
setType(
'
?int
'
)
->
setValue(
12345
,
HackBuilderValues
::
export()),
)
->
setConstructor(
$cgf
->
codegenConstructor()
->
addParameter(
'
string $text
'
)
->
setBody(
'
$this->text = $text;
'
),
)
->
addMethod(
$cgf
->
codegenMethod(
'
getText
'
)
->
setIsFinal()
->
setReturnType(
'
string
'
)
->
setBody(
'
return $this->text;
'
),
)
->
addMethod(
$cgf
->
codegenMethod(
'
genX
'
)
->
setProtected()
->
setDocBlock(
'
This is a 76 characters comment to test the splitting
'
.
'
based on indentation.
'
,
)
->
setReturnType(
'
Awaitable<int>
'
)
->
setManualBody()
->
setBody(
'
// your code here
'
),
)
->
setIsFinal()
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testLongGeneratedFrom
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
Demo
'
)
->
addMethod(
$cgf
->
codegenMethod(
'
getRawIntEnumCustomTest
'
)
->
setGeneratedFrom(
$cgf
->
codegenGeneratedFromMethodWithKey(
'
EntTestFieldGettersCodegenSchema
'
,
'
getFieldSpecification
'
,
'
RawIntEnumCustomTest
'
,
),
),
)
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testConstructorWrapperFuncDefault
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
TestWrapperFunc
'
)
->
setDocBlock(
'
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed
'
.
'
do eiusmod tempor incididunt ut labore et dolore magna aliqua.
'
.
'
Ut enim ad minim veniam, quis nostrud exercitation ullamco
'
.
"
laboris nisi ut aliquip ex ea commodo consequat.
\n
"
.
"
Understood?
\n
"
.
'
Yes!
'
,
)
->
addConstructorWrapperFunc()
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testConstructorWrapperFunc
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
TestWrapperFunc
'
)
->
addProperty(
$cgf
->
codegenProperty(
'
text
'
)
->
setPrivate()
->
setType(
'
string
'
),
)
->
addProperty(
$cgf
->
codegenProperty(
'
hack
'
)
->
setType(
'
?bool
'
)
->
setValue(
false
,
HackBuilderValues
::
export()),
)
->
setConstructor(
$cgf
->
codegenConstructor()
->
addParameter(
'
string $text, ?bool $hack
'
)
->
setBody(
'
$this->text = $text;
'
),
)
->
addConstructorWrapperFunc()
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
/*
* When current class has no constructor specified but its parent class does,
* we need to specify the parameters of the wrapper function explictly
* e.g. parent class StrangeParent has the following constructor
* function __construct(string $text) {
* // whatever
* }
*/
public
function
testConstructorWrapperFuncWithExplicitParams
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
TestWrapperFunc
'
)
->
setExtends(
'
StrangeParent
'
)
->
addConstructorWrapperFunc(
vec
[
'
string $text
'
])
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testExtendsGeneric
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
Foo
'
)
->
setExtendsf(
'
X<%s>
'
,
'
Y
'
)
->
render();
expect
(
$code
)
->
toContainSubstring(
'
extends X<Y>
'
);
}
public
function
testGenericsWithSubtypeConstraints
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
GenericsTestClass
'
)
->
addGenericSubtypeConstraint(
'
T
'
,
'
U
'
)
->
render();
expect
(
$code
)
->
toContainSubstring(
'
T as U
'
);
}
public
function
testGenericsWithSuperTypeConstraints
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
GenericsTestClass
'
)
->
addGenericSupertypeConstraint(
'
T
'
,
'
U
'
)
->
render();
expect
(
$code
)
->
toContainSubstring(
'
T super U
'
);
}
public
function
testGenericsWithConstraints
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
GenericsTestClass
'
)
->
addGenericSubtypeConstraint(
'
Tk
'
,
'
Tv
'
)
->
addGenericSupertypeConstraint(
'
Tu
'
,
'
Sp
'
)
->
addGenericSubtypeConstraint(
'
Tt
'
,
'
Xx
'
)
->
addGeneric(
'
Tsingle
'
)
->
render();
expect
(
$code
)
->
toContainSubstring(
'
Tk as Tv
'
);
expect
(
$code
)
->
toContainSubstring(
'
Tu super Sp
'
);
expect
(
$code
)
->
toContainSubstring(
'
Tt as Xx
'
);
expect
(
$code
)
->
toContainSubstring(
'
Tsingle
'
);
}
public
function
testXHPClassWithAttributes
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$code
=
$cgf
->
codegenClass(
'
a
'
)
->
setIsXHP()
->
addConstant(
$cgf
->
codegenClassConstant(
'
BETWEEN_CONSTS
'
)
->
setType(
'
string
'
)
->
setValue(
'
'
,
HackBuilderValues
::
export()),
)
->
addProperty(
$cgf
->
codegenProperty(
'
andProps
'
)
->
setType(
'
null
'
))
->
addXhpAttribute(
$cgf
->
codegenXHPAttribute(
'
href
'
)
->
setType(
'
string
'
)
->
setInlineComment(
'
The web is a magical place where a string with a set
'
.
'
structure can be the key to visiting some remote place
'
.
'
on the internet where you can find content made by other people.
'
,
)
->
setDecorator(
XHPAttributeDecorator
::
REQUIRED
),
)
->
addXhpAttribute(
$cgf
->
codegenXHPAttribute(
'
target
'
)
->
setType(
'
string
'
)
->
setValue(
'
about:blank
'
,
HackBuilderValues
::
export()),
)
->
addXhpAttribute(
$cgf
->
codegenXHPAttribute(
'
hreflang
'
)
->
setType(
'
string
'
)
->
setDecorator(
XHPAttributeDecorator
::
LATE_INIT
),
)
->
render();
expect_with_context
(
static
::
class
,
$code
)
->
toBeUnchanged();
}
public
function
testThrowsWhenSettingDecoratorWhenDefaultValueIsSet
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$attr
=
$cgf
->
codegenXHPAttribute(
'
explodes
'
)
->
setType(
'
string
'
)
->
setValue(
'
default
'
,
HackBuilderValues
::
export());
expect
(()
==>
$attr
->
setDecorator(
XHPAttributeDecorator
::
LATE_INIT
))
->
toThrow(
InvariantException
::
class
,
'
@lateinit decorator
'
);
}
public
function
testThrowsWhenSettingDefaultValueWhenDecoratorIsSet
():
void
{
$cgf
=
$this
->
getCodegenFactory();
$attr
=
$cgf
->
codegenXHPAttribute(
'
explodes
'
)
->
setType(
'
string
'
)
->
setDecorator(
XHPAttributeDecorator
::
LATE_INIT
);
expect
(()
==>
$attr
->
setValue(
'
default
'
,
HackBuilderValues
::
export()))
->
toThrow(
InvariantException
::
class
,
'
default value
'
);
}
}
Back
|
FazBrowse Home
|
New Git URL