Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
hackfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
fredemmott committed Oct 5, 2018
1 parent fab6419 commit 069f2ee
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 107 deletions.
30 changes: 6 additions & 24 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,12 @@
final class ParserTest extends \Facebook\HackTest\HackTest {
public function getExamplePatterns(): array<(string, string)> {
return [
tuple(
'/foo',
"['/foo']",
),
tuple(
'/foo/{bar}',
"['/foo/', {bar}]",
),
tuple(
'/foo/[{bar}]',
"['/foo/', ?[{bar}]]",
),
tuple(
"/foo/{bar:\\d+}",
"['/foo/', {bar: #\\d+#}]",
),
tuple(
'/foo/{bar:[0-9]+}',
"['/foo/', {bar: #[0-9]+#}]",
),
tuple(
'/foo/{bar:[0-9]{1,3}}',
"['/foo/', {bar: #[0-9]{1,3}#}]",
),
tuple('/foo', "['/foo']"),
tuple('/foo/{bar}', "['/foo/', {bar}]"),
tuple('/foo/[{bar}]', "['/foo/', ?[{bar}]]"),
tuple("/foo/{bar:\\d+}", "['/foo/', {bar: #\\d+#}]"),
tuple('/foo/{bar:[0-9]+}', "['/foo/', {bar: #[0-9]+#}]"),
tuple('/foo/{bar:[0-9]{1,3}}', "['/foo/', {bar: #[0-9]{1,3}#}]"),
];
}

Expand Down
144 changes: 81 additions & 63 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@
use namespace HH\Lib\Dict;

final class RouterTest extends \Facebook\HackTest\HackTest {
const keyset<string> MAP = keyset[
'/foo',
'/foo/',
'/foo/bar',
'/foo/bar/{baz}',
'/foo/{bar}',
'/foo/{bar}/baz',
'/foo/{bar}{baz:.+}',
'/food/{noms}',
'/bar/{herp:\\d+}',
'/bar/{herp}',
'/unique/{foo}/bar',
'/optional_suffix_[foo]',
'/optional_suffix[/]',
'/optional_suffixes/[herp[/derp]]',
'/manual/en/{LegacyID}.php',
];
const keyset<string>
MAP = keyset[
'/foo',
'/foo/',
'/foo/bar',
'/foo/bar/{baz}',
'/foo/{bar}',
'/foo/{bar}/baz',
'/foo/{bar}{baz:.+}',
'/food/{noms}',
'/bar/{herp:\\d+}',
'/bar/{herp}',
'/unique/{foo}/bar',
'/optional_suffix_[foo]',
'/optional_suffix[/]',
'/optional_suffixes/[herp[/derp]]',
'/manual/en/{LegacyID}.php',
];

public function expectedMatches(
): array<(string, string, dict<string, string>)> {
Expand All @@ -44,7 +45,11 @@ public function expectedMatches(
tuple('/foo/herp', '/foo/{bar}', dict['bar' => 'herp']),
tuple('/foo/=%3Efoo', '/foo/{bar}', dict['bar' => '=>foo']),
tuple('/foo/herp/baz', '/foo/{bar}/baz', dict['bar' => 'herp']),
tuple('/foo/herp/derp', '/foo/{bar}{baz:.+}', dict['bar' => 'herp', 'baz' => '/derp']),
tuple(
'/foo/herp/derp',
'/foo/{bar}{baz:.+}',
dict['bar' => 'herp', 'baz' => '/derp'],
),
tuple('/food/burger', '/food/{noms}', dict['noms' => 'burger']),
tuple('/bar/123', '/bar/{herp:\\d+}', dict['herp' => '123']),
tuple('/bar/derp', '/bar/{herp}', dict['herp' => 'derp']),
Expand All @@ -55,8 +60,16 @@ public function expectedMatches(
tuple('/optional_suffix', '/optional_suffix[/]', dict[]),
tuple('/optional_suffix/', '/optional_suffix[/]', dict[]),
tuple('/optional_suffixes/', '/optional_suffixes/[herp[/derp]]', dict[]),
tuple('/optional_suffixes/herp', '/optional_suffixes/[herp[/derp]]', dict[]),
tuple('/optional_suffixes/herp/derp', '/optional_suffixes/[herp[/derp]]', dict[]),
tuple(
'/optional_suffixes/herp',
'/optional_suffixes/[herp[/derp]]',
dict[],
),
tuple(
'/optional_suffixes/herp/derp',
'/optional_suffixes/[herp[/derp]]',
dict[],
),
tuple(
'/manual/en/foo.php',
'/manual/en/{LegacyID}.php',
Expand All @@ -75,10 +88,18 @@ public function testCanGetExpatchedMatchesWithResolvers(): void {
}

public function getAllResolvers(
): array<(string, (function(dict<HttpMethod, dict<string, string>>): IResolver<string>))> {
): array<
(
string,
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>),
)
> {
return [
tuple('simple regexp', $map ==> new SimpleRegexpResolver($map)),
tuple('prefix matching', $map ==> PrefixMatchingResolver::fromFlatMap($map)),
tuple(
'prefix matching',
$map ==> PrefixMatchingResolver::fromFlatMap($map),
),
];
}

Expand All @@ -101,7 +122,8 @@ public function expectedMatchesWithResolvers(
<<DataProvider('getAllResolvers')>>
public function testMethodNotAllowedResponses(
string $_name,
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>) $factory
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>)
$factory,
): void {
$map = dict[
HttpMethod::GET => dict[
Expand All @@ -117,17 +139,18 @@ public function testMethodNotAllowedResponses(

$router = $this->getRouter()->setResolver($factory($map));

list($responder, $_data) = $router->routeRequest(HttpMethod::HEAD, 'getonly');
list($responder, $_data) =
$router->routeRequest(HttpMethod::HEAD, 'getonly');
expect($responder)->toBeSame('getonly');
expect(
() ==> $router->routeRequest(HttpMethod::GET, 'headonly')
)->toThrow(MethodNotAllowedException::class);
expect(
() ==> $router->routeRequest(HttpMethod::HEAD, 'postonly'),
)->toThrow(MethodNotAllowedException::class);
expect(
() ==> $router->routeRequest(HttpMethod::GET, 'postonly'),
)->toThrow(MethodNotAllowedException::class);
expect(() ==> $router->routeRequest(HttpMethod::GET, 'headonly'))->toThrow(
MethodNotAllowedException::class,
);
expect(() ==> $router->routeRequest(HttpMethod::HEAD, 'postonly'))->toThrow(
MethodNotAllowedException::class,
);
expect(() ==> $router->routeRequest(HttpMethod::GET, 'postonly'))->toThrow(
MethodNotAllowedException::class,
);
}

<<DataProvider('expectedMatches')>>
Expand All @@ -150,19 +173,19 @@ public function testAllResolvers(
string $expected_responder,
dict<string, string> $expected_data,
): void {
list($responder, $data) = $this->getRouter()->setResolver(
$resolver,
)->routeRequest(HttpMethod::GET, $in);
list($responder, $data) = $this->getRouter()
->setResolver($resolver)
->routeRequest(HttpMethod::GET, $in);
expect($responder)->toBeSame($expected_responder);
expect(dict($data))->toBeSame($expected_data);

list($responder, $data) = $resolver->resolve(HttpMethod::GET, $in);
expect($responder)->toBeSame($expected_responder);
expect($data)->toBeSame(dict($data));

list($responder, $data) = $this->getRouter()->setResolver(
$resolver,
)->routeRequest(HttpMethod::HEAD, $in);
list($responder, $data) = $this->getRouter()
->setResolver($resolver)
->routeRequest(HttpMethod::HEAD, $in);
expect($responder)->toBeSame($expected_responder);
expect(dict($data))->toBeSame($expected_data);
}
Expand All @@ -174,10 +197,8 @@ public function testPsr7Support(
dict<string, string> $_expected_data,
): void {
$router = $this->getRouter();
list($direct_responder, $direct_data) = $router->routeRequest(
HttpMethod::GET,
$path,
);
list($direct_responder, $direct_data) =
$router->routeRequest(HttpMethod::GET, $path);

/* HH_FIXME[2049] no HHI for Diactoros */
$psr_request = new ServerRequest(
Expand All @@ -189,30 +210,28 @@ public function testPsr7Support(
/* headers = */ [],
);
list($psr_responder, $psr_data) = $router->routePsr7Request($psr_request);
expect( $psr_responder,
)->toBeSame(
$direct_responder );
expect( $psr_data,
)->toBePHPEqual(
$direct_data );
expect($psr_responder)->toBeSame($direct_responder);
expect($psr_data)->toBePHPEqual($direct_data);
}

<<DataProvider('getAllResolvers')>>
public function testNotFound(
string $_resolver_name,
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>) $factory,
): void {
(function(dict<HttpMethod, dict<string, string>>): IResolver<string>)
$factory,
): void {
$router = $this->getRouter()->setResolver($factory(dict[]));
expect(
() ==> $router->routeRequest(HttpMethod::GET, '/__404'),
)->toThrow(NotFoundException::class);

$router = $this->getRouter()->setResolver($factory(dict[
HttpMethod::GET => dict['/foo' => '/foo'],
]));
expect(
() ==> $router->routeRequest(HttpMethod::GET, '/__404'),
)->toThrow(NotFoundException::class);
expect(() ==> $router->routeRequest(HttpMethod::GET, '/__404'))->toThrow(
NotFoundException::class,
);

$router = $this->getRouter()
->setResolver($factory(dict[
HttpMethod::GET => dict['/foo' => '/foo'],
]));
expect(() ==> $router->routeRequest(HttpMethod::GET, '/__404'))->toThrow(
NotFoundException::class,
);
}

public function testMethodNotAllowed(): void {
Expand All @@ -232,8 +251,7 @@ private function typecheckCovariantTResponder(
): void {}


private function getRouter(
): TestRouter<string> {
private function getRouter(): TestRouter<string> {
return new TestRouter(dict(self::MAP));
}
}
30 changes: 10 additions & 20 deletions tests/UriBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ public function testLiteral(): void {
$parts = (new UriPattern())
->literal('/foo')
->getParts();
expect( (new UriBuilder($parts))->getPath(),
)->toBeSame(
'/foo' );
expect((new UriBuilder($parts))->getPath())->toBeSame('/foo');
}

public function testStringParameter(): void {
Expand All @@ -32,9 +30,7 @@ public function testStringParameter(): void {
$path = (new UriBuilder($parts))
->setString('foo', 'derp')
->getPath();
expect( $path,
)->toBeSame(
'/herp/derp' );
expect($path)->toBeSame('/herp/derp');
}

public function testParameterAsFirstPart(): void {
Expand All @@ -44,9 +40,7 @@ public function testParameterAsFirstPart(): void {
$path = (new UriBuilder($parts))
->setString('herp', 'derp')
->getPath();
expect( $path,
)->toBeSame(
'/derp' );
expect($path)->toBeSame('/derp');
}

public function testIntParameter(): void {
Expand All @@ -57,9 +51,7 @@ public function testIntParameter(): void {
$path = (new UriBuilder($parts))
->setInt('post_id', 123)
->getPath();
expect( $path,
)->toBeSame(
'/post/123' );
expect($path)->toBeSame('/post/123');
}

public function testEnumParameter(): void {
Expand All @@ -69,9 +61,7 @@ public function testEnumParameter(): void {
$path = (new UriBuilder($parts))
->setEnum(TestStringEnum::class, 'foo', TestStringEnum::BAR)
->getPath();
expect( $path,
)->toBeSame(
'/'.TestStringEnum::BAR );
expect($path)->toBeSame('/'.TestStringEnum::BAR);
}

public function testIntAsString(): void {
Expand All @@ -84,19 +74,19 @@ public function testIntAsString(): void {
public function testSetIncorrectEnumType(): void {
expect(() ==> {
$parts = (new UriPattern())
->enum(TestStringEnum::class, 'foo')
->getParts();
->enum(TestStringEnum::class, 'foo')
->getParts();
$path = (new UriBuilder($parts))
->setEnum(TestIntEnum::class, 'foo', TestIntEnum::BAR);
->setEnum(TestIntEnum::class, 'foo', TestIntEnum::BAR);
})->toThrow(InvariantException::class);
}

public function testSetTwice(): void {
expect(() ==> {
$parts = (new UriPattern())->int('foo')->getParts();
(new UriBuilder($parts))
->setInt('foo', 123)
->setInt('foo', 123);
->setInt('foo', 123)
->setInt('foo', 123);
})->toThrow(InvariantException::class);
}

Expand Down

0 comments on commit 069f2ee

Please sign in to comment.