Skip to content

Commit 20e1d4d

Browse files
author
amarcu
committed
Fix test warning, improve naming in tests
1 parent 61070f0 commit 20e1d4d

21 files changed

+54
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
composer.lock

examples/dorm/CodegenDorm.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function generate(): void {
4545
$path = $rc->getFileName();
4646
$pos = strrpos($path, '/');
4747
$dir = substr($path, 0, $pos + 1);
48+
$gen_from = 'codegen.php '.$this->getSchemaName().'Schema';
4849

4950
// This generates a file (we pass the file name) that contains the
5051
// class defined above and saves it.
@@ -54,7 +55,7 @@ public function generate(): void {
5455
// of the existing file and merging it if it's partially generated.
5556
codegen_file($dir.$this->getSchemaName().'.php')
5657
->addClass($class)
57-
->setGeneratedFrom(codegen_generated_from_script())
58+
->setGeneratedFrom(codegen_generated_from_script($gen_from))
5859
->save();
5960
}
6061

examples/dorm/CodegenMutator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ public function generate(): void {
5151
$path = $rc->getFileName();
5252
$pos = strrpos($path, '/');
5353
$dir = substr($path, 0, $pos + 1);
54+
$gen_from = 'codegen.php '.$rc->getShortName();
5455

5556
// This generates a file (we pass the file name) that contains the
5657
// class defined above and saves it.
5758
codegen_file($dir.$name.'.php')
5859
->addClass($class)
5960
->setIsStrict(true)
60-
->setGeneratedFrom(codegen_generated_from_script())
61+
->setGeneratedFrom(codegen_generated_from_script($gen_from))
6162
->save();
6263
}
6364

examples/dorm/demo/DormUser.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* This file is partially generated. Only make modifications between BEGIN
44
* MANUAL SECTION and END MANUAL SECTION designators.
55
*
6-
* To re-generate this file run
7-
* /home/ubuntu/hack-codegen/examples/dorm/codegen.php
6+
* To re-generate this file run codegen.php DormUserSchema
87
*
98
*
10-
* @partially-generated SignedSource<<f9e8b0c880da41854d491905f3a7314e>>
9+
* @partially-generated SignedSource<<609ad77761b889b1c3dd2a45c5aa296a>>
1110
*/
1211

1312
final class DormUser {

examples/dorm/demo/DormUserMutator.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
* This file is partially generated. Only make modifications between BEGIN
44
* MANUAL SECTION and END MANUAL SECTION designators.
55
*
6-
* To re-generate this file run
7-
* /home/ubuntu/hack-codegen/examples/dorm/codegen.php
6+
* To re-generate this file run codegen.php DormUserSchema
87
*
98
*
10-
* @partially-generated SignedSource<<f9c5059806c0ec22c4ba40846c277eb9>>
9+
* @partially-generated SignedSource<<aed81933eb3016547905627d230a9d5a>>
1110
*/
1211

1312
final class DormUserMutator {

lib/Filesystem.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static function createTemporaryFile(
1717
): string {
1818
$fname = tempnam(sys_get_temp_dir(), $prefix);
1919
if ($cleanup) {
20-
register_shutdown_function(array('Filesystem', 'remove'), $fname);
20+
register_shutdown_function(array(Filesystem::class, 'remove'), $fname);
2121
}
2222
return $fname;
2323
}

src/CodegenClass.php

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function getExtends(): ?string {
5151
return $this->extendsClass;
5252
}
5353

54+
5455
public function setConstructor(CodegenConstructor $constructor): this {
5556
$this->constructor = $constructor;
5657
return $this;

src/CodegenFile.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ public function render(): string {
228228

229229
$old_content = $this->loadExistingFiles();
230230

231-
$doc_block = $this->docBlock;
232-
if ($this->generatedFrom !== null) {
233-
$doc_block = $doc_block. $this->generatedFrom->render() ."\n";
231+
$doc_block = (string)$this->docBlock;
232+
$gen_from = $this->generatedFrom;
233+
if ($gen_from !== null) {
234+
if ($doc_block && !Str::endsWith($doc_block, "\n")) {
235+
$doc_block .= "\n";
236+
}
237+
$doc_block = $doc_block.$gen_from->render()."\n";
234238
}
235239

236240
if (PartiallyGeneratedCode::containsManualSection($content)) {

src/CodegenFunctionBase.php

+5
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function __construct(
3838
$this->name = $name;
3939
}
4040

41+
public function setName(string $name): this {
42+
$this->name = $name;
43+
return $this;
44+
}
45+
4146
public function setIsAsync(bool $value = true): this {
4247
$this->isAsync = $value;
4348
return $this;

src/CodegenGeneratedFrom.php

-4
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@ function codegen_generated_from_class(string $class): CodegenGeneratedFrom {
3434
return new CodegenGeneratedFrom("Generated from $class");
3535
}
3636

37-
function codegen_generated_from_string(string $string): CodegenGeneratedFrom {
38-
return new CodegenGeneratedFrom("Generated from $string");
39-
}
40-
4137
function codegen_generated_from_method(
4238
string $class,
4339
string $method

src/CodegenMemberVar.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ public function setType(string $type, ...$args): this {
7777
* arrays, etc, and it will generate the code to render those values.
7878
*/
7979
public function setValue(mixed $value): this {
80-
return $this->setLiteralValue(strip_hh_prefix(var_export($value, true)));
80+
$val_str = var_export($value, true);
81+
$val_str = $val_str === 'NULL' ? 'null' : $val_str;
82+
return $this->setLiteralValue(strip_hh_prefix($val_str));
8183
}
8284

8385
/**

src/HackBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public function addMultilineCall(
7474
/**
7575
* Add a Map with the specified pairs.
7676
*
77-
* @param ConstMap<Tk, Tv> $map The Map or Map to output. Note that
77+
* @param \ConstMap<Tk, Tv> $map The Map or Map to output. Note that
7878
* regardless of which type is passed in, a Map will be output. This is
7979
* useful for sorting the map for readability, as long as you don't need
8080
* the sorting in actual code.
@@ -95,7 +95,7 @@ public function addMap<Tk, Tv>(
9595
/**
9696
* Add an ImmMap with the specified pairs.
9797
*
98-
* @param ConstMap<Tk, Tv> $map The Map or Map to output. Note that
98+
* @param \ConstMap<Tk, Tv> $map The Map or Map to output. Note that
9999
* regardless of which type is passed in, an ImmMap will be output. This is
100100
* useful for sorting the map for readability, as long as you don't need
101101
* the sorting in actual code.

test/CodegenClassTestCase.codegen

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
!@#$%codegentest:testClassDeclarationWithGenerics
33
class ClassWithGenerics
44
<
5-
Tent as Facebook\HackCodegen\IEnt,
5+
Tent as Ixyz,
66
T,
77
Tstory as EntCreationStory<Tent>
88
> {

test/CodegenClassTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testLongClassDeclarationWithInterfaces() {
8989

9090
public function testClassDeclarationWithGenerics() {
9191
$generics_decl = Map {
92-
'Tent' => IEnt::class,
92+
'Tent' => 'Ixyz',
9393
'T' => "",
9494
'Tstory' => "EntCreationStory<Tent>",
9595
};

test/CodegenExpectedFile.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static function getPath(string $class_name): string {
3737
$ref = new \ReflectionClass($class_name);
3838
$source_file = $ref->getFileName();
3939
// Get classname without namespace
40-
$class_name = $ref->getShortName();
40+
$class_name = $ref->getShortName();
4141
return dirname($source_file).'/'.$class_name.self::EXTENSION;
4242
}
4343

test/CodegenFunctionTestCase.codegen

+6-6
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ function getExpensive() {
2525
}
2626

2727
!@#$%codegentest:testMixedBuiltInAndUserAttributes
28-
<<__Override, Facebook\HackCodegen\DataProvider('providerFunc'), Facebook\HackCodegen\TestsBypassVisibility>>
28+
<<__Override, DataProvider('providerFunc'), TestsBypassVisibility>>
2929
function getOverridedBypassVisibilityAndUseDataProvider() {
3030
}
3131

3232
!@#$%codegentest:testMixedBuiltInAndUserAttributesAsync
33-
<<__Override, Facebook\HackCodegen\DataProvider('providerFunc'), Facebook\HackCodegen\TestsBypassVisibility>>
33+
<<__Override, DataProvider('providerFunc'), TestsBypassVisibility>>
3434
async function genOverridedBypassVisibilityAndUseDataProvider() {
3535
}
3636

3737
!@#$%codegentest:testMixedUserAttributes
38-
<<Facebook\HackCodegen\DataProvider('providerFunc'), Facebook\HackCodegen\TestsBypassVisibility>>
38+
<<DataProvider('providerFunc'), TestsBypassVisibility>>
3939
function getBypassVisibilityAndUseDataProvider() {
4040
}
4141

@@ -67,13 +67,13 @@ function getName(): string {
6767
return $name;
6868
}
6969

70-
!@#$%codegentest:testSingleUserAttributeWitArgument
71-
<<Facebook\HackCodegen\DataProvider('providerFunc')>>
70+
!@#$%codegentest:testSingleUserAttributeWithArgument
71+
<<DataProvider('providerFunc')>>
7272
function getUseDataProvider() {
7373
}
7474

7575
!@#$%codegentest:testSingleUserAttributeWithoutArgument
76-
<<Facebook\HackCodegen\TestsBypassVisibility>>
76+
<<TestsBypassVisibility>>
7777
function getTestsBypassVisibility() {
7878
}
7979

test/CodegenFunctionTestCase.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,40 @@ public function testOverrideMemoizedAsync() {
6969

7070
public function testSingleUserAttributeWithoutArgument() {
7171
$code = codegen_function('getTestsBypassVisibility')
72-
->setUserAttribute(TestsBypassVisibility::class)
72+
->setUserAttribute('TestsBypassVisibility')
7373
->render();
7474
self::assertUnchanged($code);
7575
}
7676

77-
public function testSingleUserAttributeWitArgument() {
77+
public function testSingleUserAttributeWithArgument() {
7878
$code = codegen_function('getUseDataProvider')
79-
->setUserAttribute(DataProvider::class, "'providerFunc'")
79+
->setUserAttribute('DataProvider', "'providerFunc'")
8080
->render();
8181
self::assertUnchanged($code);
8282
}
8383

8484
public function testMixedUserAttributes() {
8585
$code = codegen_function('getBypassVisibilityAndUseDataProvider')
86-
->setUserAttribute(DataProvider::class, "'providerFunc'")
87-
->setUserAttribute(TestsBypassVisibility::class)
86+
->setUserAttribute('DataProvider', "'providerFunc'")
87+
->setUserAttribute('TestsBypassVisibility')
8888
->render();
8989
self::assertUnchanged($code);
9090
}
9191

9292
public function testMixedBuiltInAndUserAttributes() {
9393
$code = codegen_function('getOverridedBypassVisibilityAndUseDataProvider')
9494
->setIsOverride(true)
95-
->setUserAttribute(DataProvider::class, "'providerFunc'")
96-
->setUserAttribute(TestsBypassVisibility::class)
95+
->setUserAttribute('DataProvider', "'providerFunc'")
96+
->setUserAttribute('TestsBypassVisibility')
9797
->render();
9898
self::assertUnchanged($code);
9999
}
100100

101101
public function testMixedBuiltInAndUserAttributesAsync() {
102102
$code = codegen_function('genOverridedBypassVisibilityAndUseDataProvider')
103103
->setIsOverride(true)
104-
->setUserAttribute(DataProvider::class, "'providerFunc'")
105-
->setUserAttribute(TestsBypassVisibility::class)
104+
->setUserAttribute('DataProvider', "'providerFunc'")
105+
->setUserAttribute('TestsBypassVisibility')
106106
->setIsAsync()
107107
->render();
108108
self::assertUnchanged($code);

test/CodegenMethodTestCase.codegen

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getName(): string {
2222

2323
!@#$%codegentest:testManualSection
2424
public function genProprietorName(): string {
25-
/* BEGIN MANUAL SECTION Facebook\HackCodegen\EntBlindPig::genProprietorName */
25+
/* BEGIN MANUAL SECTION MyClass::genProprietorName */
2626
// insert your code here
2727
/* END MANUAL SECTION */
2828
}

test/CodegenMethodTestCase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function testManualSection() {
5555
->setBody('// insert your code here')
5656
->setManualBody();
5757

58-
codegen_class(EntBlindPig::class)->addMethod($method);
58+
codegen_class('MyClass')->addMethod($method);
5959
$code = $method->render();
6060

6161
self::assertUnchanged($code);

test/CodegenTraitTestCase.codegen

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
*/
66
trait DemoInternal {
77

8-
require extends Facebook\HackCodegen\EntSchema;
9-
require implements Facebook\HackCodegen\IEntSchemaBase;
8+
require extends RequiredClass;
9+
require implements RequiredInterface;
1010

11-
use EntProvisionalMode;
11+
use DemoTrait;
1212
// Generated from Whatever::Method()
1313
use WhateverTrait;
1414
use Useless;

test/CodegenTraitTestCase.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public function testDocblock() {
2929

3030
public function testDemo() {
3131
$code = codegen_trait('DemoInternal')
32-
->addRequireClass(EntSchema::class)
33-
->addRequireInterface(IEntSchemaBase::class)
34-
->addTrait(codegen_uses_trait('EntProvisionalMode'))
32+
->addRequireClass('RequiredClass')
33+
->addRequireInterface('RequiredInterface')
34+
->addTrait(codegen_uses_trait('DemoTrait'))
3535
->addTrait(
3636
codegen_uses_trait('WhateverTrait')
3737
->setGeneratedFrom(codegen_generated_from_method("Whatever", "Method"))

0 commit comments

Comments
 (0)