FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
hack-codegen/test/CodegenSimpleTester.php at master · Daniel15/hack-codegen · GitHub
Daniel15
/
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
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
hack-codegen
/
test
/
CodegenSimpleTester.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
118 lines (110 loc) · 3.49 KB
Breadcrumbs
hack-codegen
/
test
/
CodegenSimpleTester.php
Copy path
File metadata and controls
118 lines (110 loc) · 3.49 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
<
?
hh
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
namespace
Facebook\HackCodegen
;
require_once
(
'
CodegenExpectedFile.php
'
);
require_once
(
'
CodegenBaseTest.php
'
);
require_once
(
'
TestCodegenConfig.php
'
);
/**
* Runs all the tests found in the test directory (i.e. classes
* that extend CodegenBaseTest).
* For each class, it runs all the methods that start with "test".
* The tests will be checking expected results using the methods
* found in CodegenBaseTest, such as assertUnchanged.
* The tester will show a failure for unexpected cases.
* Tests can also expect an exception as a result, in which case
* the test needs to define a docblock that contains @expectedException
* followed by the exception class.
*/
final
class
CodegenSimpleTester
{
/**
* Run all the tests. Return true if they all succeeded.
*/
public
static
function
run
():
bool
{
$files
=
glob
(
__DIR__
.
'
/*.php
'
);
$fail_count
=
0
;
foreach
(
$files
as
$file
) {
$classes
=
get_declared_classes
();
require_once
(
$file
);
$test_classes
=
array_diff
(
get_declared_classes
(),
$classes
);
foreach
(
$test_classes
as
$class
) {
$ref
=
new
\ReflectionClass
(
$class
);
if
(
$ref
->
isAbstract()) {
continue
;
}
$instance
=
$ref
->
newInstance();
if
(
!
$instance
instanceof
CodegenBaseTest
) {
continue
;
}
echo
sprintf
(
"
Start testing %s
\n
"
,
$class
);
$methods
=
$ref
->
getMethods();
foreach
(
$methods
as
$method
) {
$name
=
$method
->
getName();
if
(
!
Str
::
startsWith(
$name
,
'
test
'
)) {
continue
;
}
$failure
=
self
::
runTest(
$instance
,
$method
);
if
(
$failure
===
null
) {
echo
sprintf
(
"
Test %s \e[0;32mpassed\e[0m
\n
"
,
$name
);
}
else
{
$fail_count
++
;
echo
sprintf
(
"
Test %s \e[0;31mfailed\e[0m: %s
\n
"
,
$name
,
$failure
);
}
}
}
}
if
(
$fail_count
===
0
) {
echo
"
\e[0;32m** All the tests passed! **\e[0m
\n
"
;
return
true
;
}
else
{
echo
sprintf
(
"
\e[0;31m** ERROR: %d test failures **\e[0m
\n
"
,
$fail_count
);
return
false
;
}
}
/**
* Run a test method.
*
*
@return
a string containing the failure message, or null if the test
* passed
*/
private
static
function
runTest
(
CodegenBaseTest
$instance
,
\ReflectionMethod
$method
,
): ?
string
{
$doc_block
=
$method
->
getDocComment();
$expected_exception
=
null
;
if
(
is_string
(
$doc_block
)) {
foreach
(
explode
(
"
\n
"
,
$doc_block
)
as
$line
) {
$find
=
strstr
(
$line
,
'
@expectedException
'
);
if
(
$find
) {
$expected_exception
=
trim
(
substr
(
$find
,
19
));
}
}
}
try
{
$method
->
invoke(
$instance
);
if
(
$expected_exception
) {
return
"
Expected exception
$expected_exception
, nothing was thrown
"
;
}
}
catch
(
\
Exception
$ex
) {
if
(
$expected_exception
===
null
) {
return
$ex
->
getMessage();
}
else
{
$class_name
=
(
new
\ReflectionClass
(
$ex
))
->
getShortName();
if
(
$class_name
!==
$expected_exception
) {
return
"
Expected exception
$expected_exception
,
"
.
get_class
(
$ex
)
.
"
was thrown instead
"
;
}
}
}
return
null
;
}
}
Back
|
FazBrowse Home
|
New Git URL