forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodegenFileTestCase.php
More file actions
231 lines (198 loc) · 6.38 KB
/
CodegenFileTestCase.php
File metadata and controls
231 lines (198 loc) · 6.38 KB
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
<?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.
*/
final class CodegenFileTestCase extends CodegenBaseTest {
public function testAutogenerated() {
$code = test_codegen_file('no_file')
->setDocBlock('Completely autogenerated!')
->addClass(
codegen_class('AllAutogenerated')
->addMethod(
codegen_method('getName')
->setReturnType('string')
->setBody('return $this->name;')
)
)
->render();
self::assertUnchanged($code);
}
public static function testGenerateTopLevelFunctions() {
$function = codegen_function('fun')
->setReturnType('int')
->setBody('return 0;');
$code = test_codegen_file('no_file')
->addFunction($function)
->render();
self::assertUnchanged($code);
}
public function testPartiallyGenerated() {
$code = test_codegen_file('no_file')
->addClass(
codegen_class('PartiallyGenerated')
->addMethod(
codegen_method('getSomething')
->setManualBody()
)
)
->addClass(
codegen_class('PartiallyGeneratedLoader')
->setDocBlock('We can put many clases in one file!')
)
->render();
self::assertUnchanged($code);
}
private function saveAutogeneratedFile(?string $fname = null) {
if (!$fname) {
$fname = Filesystem::createTemporaryFile('codegen', true);
}
test_codegen_file($fname)
->setDocBlock('Testing CodegenFile with autogenerated files')
->addClass(
codegen_class('Demo')
->addMethod(codegen_method('getName')->setBody('return "Codegen";'))
)
->save();
return $fname;
}
private function saveManuallyWrittenFile(?string $fname = null) {
if (!$fname) {
$fname = Filesystem::createTemporaryFile('codegen', true);
}
Filesystem::writeFileIfChanged(
$fname,
"<?php\n".
"// Some handwritten code"
);
return $fname;
}
private function savePartiallyGeneratedFile(
?string $fname = null,
bool $extra_method = false) {
if (!$fname) {
$fname = Filesystem::createTemporaryFile('codegen', true);
}
$class = codegen_class('Demo')
->addMethod(
codegen_method('getName')
->setBody('// manual_section_here')
->setManualBody()
);
if ($extra_method) {
$class->addMethod(
codegen_method('extraMethod')->setManualBody()
);
}
test_codegen_file($fname)
->setDocBlock('Testing CodegenFile with partially generated files')
->addClass($class)
->save();
return $fname;
}
public function testSaveAutogenerated() {
$fname = $this->saveAutogeneratedFile();
self::assertUnchanged(Filesystem::readFile($fname));
}
/**
* @expectedException CodegenFileNoSignatureException
*/
public function testClobberManuallyWrittenCode() {
$fname = $this->saveManuallyWrittenFile();
$this->saveAutogeneratedFile($fname);
}
public function testReSaveAutogenerated() {
$fname = $this->saveAutogeneratedFile();
$content0 = Filesystem::readFile($fname);
$this->saveAutogeneratedFile($fname);
$content1 = Filesystem::readFile($fname);
self::assertEquals($content0, $content1);
}
/**
* @expectedException CodegenFileBadSignatureException
*/
public function testSaveModifiedAutogenerated() {
$fname = $this->saveAutogeneratedFile();
$content = Filesystem::readFile($fname);
Filesystem::writeFile($fname, $content.'.');
$this->saveAutogeneratedFile($fname);
}
public function testSavePartiallyGenerated() {
$fname = $this->savePartiallyGeneratedFile();
$content = Filesystem::readFile($fname);
self::assertUnchanged($content);
self::assertTrue(
PartiallyGeneratedSignedSource::hasValidSignature($content)
);
}
public function testReSavePartiallyGenerated() {
$fname = $this->savePartiallyGeneratedFile();
$content0 = Filesystem::readFile($fname);
$this->savePartiallyGeneratedFile($fname);
$content1 = Filesystem::readFile($fname);
self::assertEquals($content0, $content1);
}
/**
* @expectedException CodegenFileBadSignatureException
*/
public function testSaveModifiedWrongPartiallyGenerated() {
$fname = $this->savePartiallyGeneratedFile();
$content = Filesystem::readFile($fname);
Filesystem::writeFile($fname, $content.'.');
$this->saveAutogeneratedFile($fname);
}
private function createAndModifyPartiallyGeneratedFile() {
$fname = $this->savePartiallyGeneratedFile();
$content = Filesystem::readFile($fname);
$new_content = str_replace(
'// manual_section_here',
'return $this->name;',
$content
);
self::assertFalse(
$content == $new_content,
"The manual content wasn't replaced. Please fix the test setup!"
);
Filesystem::writeFile($fname, $new_content);
return $fname;
}
/**
* Test modifying a manual section and saving.
*/
public function testSaveModifiedManualSectionPartiallyGenerated() {
$fname = $this->createAndModifyPartiallyGeneratedFile();
$this->savePartiallyGeneratedFile($fname);
$content = Filesystem::readFile($fname);
self::assertTrue(strpos($content, 'this->name') !== false);
}
/**
* Test modifying a manual section and changing the code generation so
* that the generated part is different too.
*/
public function testSaveModifyPartiallyGenerated() {
$fname = $this->createAndModifyPartiallyGeneratedFile();
$this->savePartiallyGeneratedFile($fname, true);
$content = Filesystem::readFile($fname);
self::assertTrue(strpos($content, 'return $this->name;') !== false);
self::assertTrue(strpos($content, 'function extraMethod()') !== false);
}
public function testNoSignature() {
$code = test_codegen_file('no_file')
->setIsSignedFile(false)
->setDocBlock('Completely autogenerated!')
->addClass(
codegen_class('NoSignature')
->addMethod(
codegen_method('getName')
->setReturnType('string')
->setBody('return $this->name;')
)
)
->render();
self::assertUnchanged($code);
}
}