FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hack-codegen/test/CodegenExpectedFile.php at master · Chipcius/hack-codegen · GitHub
Chipcius
/
hack-codegen
Public
forked from
hhvm/hack-codegen
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
hack-codegen
/
test
/
CodegenExpectedFile.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
297 lines (265 loc) · 8.3 KB
Breadcrumbs
hack-codegen
/
test
/
CodegenExpectedFile.php
Copy path
File metadata and controls
297 lines (265 loc) · 8.3 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
<
?
hh
//
strict
/*
* 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
;
use
namespace HH\Lib\{Str
,
Vec}
;
/**
* The main purposes of this class are to:
* 1) Serialize a Map<string,string> into a file.
* Those files have the extension .codegen, they are basically used
* for testing, similarly to sql3 files used by shim.
* 2) Prompt the user to accept/refuse any change between an old
* Map<string,string> and a new one.
*
* We call the keys of our Map<string,string> a token. It's simply
* a unique identifier (usually the name of the test function) for the value
* (usually the expected output of that test function).
*
* The main (only) user is CodegenAssertUnchanged that you can use in your
* tests (see docblock).
*/
final
class
CodegenExpectedFile
{
const
string
EXTENSION
=
'
.codegen
'
;
const
string
SEPARATOR
=
'
!@#$%codegentest:
'
;
/**
* Given a class name, return the uri where the codegen file should
* be written (the uri where the class is defined -php+codegen).
*/
public
static
function
getPath
(
string
$class_name
):
string
{
$ref
=
new
\ReflectionClass
(
$class_name
);
$source_file
=
$ref
->
getFileName();
//
Get classname without namespace
$class_name
=
$ref
->
getShortName();
return
\
dirname
(
$source_file
)
.
'
/
'
.
$class_name
.
self
::
EXTENSION
;
}
/**
* Users of CodegenExpectedFile can use whatever token they want,
* but a common case if to use the name of the test function.
* This returns the name of the first function that starts with 'test'
* in the current stack trace.
*/
public
static
function
findToken
():
string
{
$token
=
null
;
//
Get caller function name
$stack
=
\
debug_backtrace
();
foreach
(
$stack
as
$function
) {
$function_name
=
$function
[
'
function
'
];
if
(
Str
\
starts_with
(
$function_name
,
'
test
'
)) {
$token
=
$function_name
;
break
;
}
}
invariant
(
$token
!==
null
,
'
Test framework was unable to find a function starting with
'
.
'
"test" when looking through the stack.
'
,
);
return
$token
;
}
//
//
Read/Write codegen file
//
/**
* Parse a existing codegen file, returns the Map<string,string>.
*/
public
static
function
parseFile
(
string
$file_name
):
Map
<
string
,
string
> {
$map
=
Map
{};
if
(
!
\
file_exists
(
$file_name
)) {
return
$map
;
}
$lines
=
\
file
(
$file_name
);
invariant
(
$lines
!==
false
,
'
Fail to open the file %s for reading
'
,
$file_name
,
);
$generated
=
$lines
[
0
];
$lines
=
Vec
\
drop
(
$lines
,
1
);
invariant
(
\
rtrim
(
$generated
)
===
'
@
'
.
'
generated
'
,
'
Codegen test record file should start with a generated tag
'
,
);
$token
=
null
;
$expected
=
'
'
;
foreach
(
$lines
as
$line
) {
if
(
Str
\
starts_with
(
$line
,
self
::
SEPARATOR
)) {
if
(
$token
!==
null
) {
//
We always add 1 newline at the end
$expected
=
\
substr
(
$expected
,
0
,
-
1
);
$map
->
set(
$token
,
$expected
);
}
//
Format is separator:token\n
$token
=
\
substr
(\
rtrim
(
$line
), \
strlen
(
self
::
SEPARATOR
));
$expected
=
'
'
;
continue
;
}
$expected
.=
self
::
unescapeTokens(
$line
);
}
if
(
$token
!==
null
) {
//
We always add 1 newline at the end
$expected
=
\
substr
(
$expected
,
0
,
-
1
);
$map
->
set(
$token
,
$expected
);
}
return
$map
;
}
/**
* Create or override a codegen file with a merge of an old
* Map<string,string> and a new one.
* User is prompted for all mismatched values.
*/
final
public
static
function
writeExpectedFile
(
string
$file_name
,
Map
<
string
,
string
>
$new_expected
,
Map
<
string
,
string
>
$old_expected
,
):
void
{
$final_expected
=
Map
{};
foreach
(
$new_expected
as
$token
=>
$new_value
) {
if
(
$old_expected
->
contains(
$token
)) {
$old_value
=
$old_expected
->
at(
$token
);
if
(
$new_value
===
$old_value
) {
//
No change, keep it
$final_expected
->
set(
$token
,
$new_value
);
}
else
{
//
Prompt user to accept the change
$update_it
=
self
::
promptForUpdate(
$token
,
$old_value
,
$new_value
);
if
(
$update_it
===
true
) {
$final_expected
->
set(
$token
,
$new_value
);
}
else
{
$final_expected
->
set(
$token
,
$old_value
);
}
}
}
else
{
//
Prompt user to accept the new value
$add_it
=
self
::
promptForAdd(
$token
,
$new_value
);
if
(
$add_it
===
true
) {
$final_expected
->
set(
$token
,
$new_value
);
}
}
}
foreach
(
$old_expected
as
$token
=>
$old_value
) {
if
(
$new_expected
->
contains(
$token
)) {
//
We already made the decision
continue
;
}
//
Prompt user to remove the old value
$remove_it
=
self
::
promptForRemove(
$token
,
$old_value
);
if
(
$remove_it
===
false
) {
$final_expected
->
set(
$token
,
$old_value
);
}
}
self
::
writeFile(
$file_name
,
$final_expected
);
}
/**
* Create or override a codegen file with a new Map<string,string>.
*/
public
static
function
writeFile
(
string
$file_name
,
Map
<
string
,
string
>
$map
,
):
void
{
if
(
!
\
file_exists
(
$file_name
)) {
\
touch
(
$file_name
);
\
chmod
(
$file_name
,
0666
);
}
$file
=
\
fopen
(
$file_name
,
'
w
'
);
invariant
(
$file
!==
false
,
'
Fail to open the file %s for writing
'
,
$file_name
,
);
//
Sorting is important or we would have merge conflict
//
in the generated file all the time
$tokens
=
Vec
\
sort
(
$map
->
keys());
\
fwrite
(
$file
,
'
@
'
.
"
generated
\n
"
);
foreach
(
$tokens
as
$token
) {
$value
=
self
::
escapeTokens(
$map
->
at(
$token
));
//
Format is separator:token\n
\
fwrite
(
$file
,
self
::
SEPARATOR
.
$token
.
"
\n
"
);
//
We always add 1 newline at the end
\
fwrite
(
$file
,
$value
.
"
\n
"
);
}
\
fclose
(
$file
);
}
//
//
Stdout/Stdin helpers
//
/**
* Helper to display any message on stdout
*/
final
public
static
function
displayf
(
SprintfFormatString
$format
,
mixed
...
$args
):
void
{
$message
=
\
vsprintf
(
$format
,
$args
);
self
::
displayRaw(
'
gentest>
'
.
$message
.
"
\n
"
);
}
final
public
static
function
displayRaw
(
string
$message
):
void
{
\
fwrite
(
\STDERR
,
$message
);
}
private
static
function
promptForAdd
(
string
$token
,
string
$value
):
bool
{
self
::
displayf(
'
A new test %s was added, expected value is:
'
,
$token
);
self
::
displayRaw(
$value
.
"
\n
"
);
return
self
::
prompt(
'
Do you accept the new expected value ?
'
);
}
private
static
function
promptForUpdate
(
string
$token
,
string
$old_value
,
string
$new_value
,
):
bool
{
self
::
displayf(
'
Change of expected value for test %s:
'
,
$token
);
self
::
displayRaw(
_Private
\
difference_render_fast
(
$old_value
,
$new_value
));
return
self
::
prompt(
'
Do you accept to update the expected value ?
'
);
}
private
static
function
promptForRemove
(
string
$token
,
string
$value
):
bool
{
self
::
displayf(
'
An old test %s was removed, expected value was:
'
,
$token
);
self
::
displayRaw(
$value
.
"
\n
"
);
return
self
::
prompt(
'
Do you accept to remove the expected value ?
'
);
}
/**
* Helper to ask the user for validation on stdin
*/
private
static
function
prompt
(
string
$message
):
bool
{
while
(
true
) {
self
::
displayf(
'
%s (y/N)
'
,
$message
);
self
::
displayRaw(
'
>
'
);
$result
=
\
strtolower
(\
trim
(\
fgets
(
\STDIN
)));
if
(
$result
===
'
y
'
) {
return
true
;
}
if
(
$result
===
'
'
||
$result
===
'
n
'
) {
return
false
;
}
}
invariant_violation
(
'
For Hack
'
);
}
//
//
Signature helpers
//
/**
* Escape the tokens that carry signatures, so that when writing those to
* the .codegen file, it doesn't seem like that's the file signature.
*/
final
private
static
function
escapeTokens
(
string
$s
):
string
{
return
Str
\
replace_every
(
$s
,
dict
[
'
@
'
.
'
generated
'
=>
'
@-generated
'
,
'
@
'
.
'
partially-generated
'
=>
'
@-partially-generated
'
,
],
);
}
final
private
static
function
unescapeTokens
(
string
$s
):
string
{
return
Str
\
replace_every
(
$s
,
dict
[
'
@-generated
'
=>
'
@
'
.
'
generated
'
,
'
@-partially-generated
'
=>
'
@
'
.
'
partially-generated
'
,
],
);
}
}
Back
|
FazBrowse Home
|
New Git URL