|
5 | 5 | use namespace HH\Lib\{C, Math, Str}; |
6 | 6 | use function Facebook\FBExpect\expect; |
7 | 7 | use type Slack\Hack\JsonSchema\Validator; |
8 | | -use type Slack\Hack\JsonSchema\Codegen\{Codegen, IJsonSchemaCodegenConfig}; |
| 8 | +use type Slack\Hack\JsonSchema\Codegen\{Codegen, IJsonSchemaCodegenConfig, TSchema}; |
9 | 9 | use type Facebook\HackTest\HackTest; |
10 | 10 |
|
11 | 11 | abstract class BaseCodegenTestCase extends HackTest { |
12 | 12 |
|
13 | 13 | const string CODEGEN_ROOT = __DIR__.'/examples/codegen'; |
14 | 14 | const string CODEGEN_NS = 'Slack\\Hack\\JsonSchema\\Tests\\Generated'; |
15 | 15 |
|
| 16 | + const type TOptions = shape( |
| 17 | + ?'sanitize_string' => Codegen::TSanitizeStringConfig, |
| 18 | + ?'json_schema_codegen_config' => IJsonSchemaCodegenConfig, |
| 19 | + ?'refs' => Codegen::TValidatorRefsConfig, |
| 20 | + ?'defaults' => Codegen::TValidatorDefaultsConfig, |
| 21 | + ?'discard_additional_properties' => bool, |
| 22 | + ); |
| 23 | + |
16 | 24 | public function assertUnchanged(string $_value, ?string $_token = null): void { |
17 | 25 | self::markTestSkipped("assertUnchanged doesn't work in hacktest yet"); |
18 | 26 |
|
@@ -61,17 +69,38 @@ public static function getCodeGenPath(string $file): string { |
61 | 69 | public static function getBuilder( |
62 | 70 | string $json_filename, |
63 | 71 | string $name, |
64 | | - shape( |
65 | | - ?'sanitize_string' => Codegen::TSanitizeStringConfig, |
66 | | - ?'json_schema_codegen_config' => IJsonSchemaCodegenConfig, |
67 | | - ?'refs' => Codegen::TValidatorRefsConfig, |
68 | | - ?'defaults' => Codegen::TValidatorDefaultsConfig, |
69 | | - ?'discard_additional_properties' => bool, |
70 | | - ) $options = shape(), |
| 72 | + this::TOptions $options = shape(), |
| 73 | + ): shape( |
| 74 | + 'path' => string, |
| 75 | + 'codegen' => Codegen, |
| 76 | + ) { |
| 77 | + $codegen_config = self::getConfig($name, $options); |
| 78 | + $codegen = Codegen::forPath(__DIR__."/examples/{$json_filename}", $codegen_config); |
| 79 | + |
| 80 | + return shape( |
| 81 | + 'path' => $codegen_config['validator']['file'], |
| 82 | + 'codegen' => $codegen, |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + public static function getBuilderForSchema( |
| 87 | + TSchema $schema, |
| 88 | + string $name, |
| 89 | + this::TOptions $options = shape(), |
71 | 90 | ): shape( |
72 | 91 | 'path' => string, |
73 | 92 | 'codegen' => Codegen, |
74 | 93 | ) { |
| 94 | + $codegen_config = self::getConfig($name, $options); |
| 95 | + $codegen = Codegen::forSchema(Shapes::toDict($schema), $codegen_config, __DIR__.'/examples/'); |
| 96 | + |
| 97 | + return shape( |
| 98 | + 'path' => $codegen_config['validator']['file'], |
| 99 | + 'codegen' => $codegen, |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + private static function getConfig(string $name, this::TOptions $options): Codegen::TCodegenConfig { |
75 | 104 | $path = self::getCodeGenPath("{$name}.php"); |
76 | 105 | $validator_config = shape( |
77 | 106 | 'namespace' => self::CODEGEN_NS, |
@@ -108,12 +137,7 @@ public static function getBuilder( |
108 | 137 | $codegen_config['jsonSchemaCodegenConfig'] = $json_schema_codegen_config; |
109 | 138 | } |
110 | 139 |
|
111 | | - $codegen = Codegen::forPath(__DIR__."/examples/{$json_filename}", $codegen_config); |
112 | | - |
113 | | - return shape( |
114 | | - 'path' => $path, |
115 | | - 'codegen' => $codegen, |
116 | | - ); |
| 140 | + return $codegen_config; |
117 | 141 | } |
118 | 142 |
|
119 | 143 | public static function benchmark(string $label, (function(): void) $callback): void { |
|
0 commit comments