Skip to content
This repository has been archived by the owner on Aug 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 21, 2019
1 parent b01e21a commit eb36e64
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
9 changes: 6 additions & 3 deletions src/Constraint/IsType.hack
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ class IsType {
'array' => ($x ==> \is_array($x)),
'object' => ($x ==> \is_object($x)),
'resource' => (
$x ==> ($x is resource) || (
@\get_resource_type(/* HH_FIXME[4110] closed resources fail is resource */ $x) is string
)
$x ==> ($x is resource) ||
(
@\get_resource_type(
/* HH_FIXME[4110] closed resources fail is resource */ $x,
) is string
)
),
'scalar' => ($x ==> \is_scalar($x)),
'callable' => ($x ==> \is_callable($x)),
Expand Down
20 changes: 16 additions & 4 deletions src/ExpectObj.hack
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class ExpectObj<T> extends Assert {
TVal $needle,
string $msg = '',
mixed ...$args
): void where T as Traversable<TVal>{
): void where T as Traversable<TVal> {
$msg = \vsprintf($msg, $args);
$this->assertContains($needle, not_hack_array($this->var), $msg);
}
Expand Down Expand Up @@ -454,15 +454,23 @@ class ExpectObj<T> extends Assert {
* element for which $element == $needle.
* Note: If $needle is an object, === will be used.
*/
public function toNotContain<TVal>(TVal $expected, string $msg = '', mixed ...$args): void where T as Traversable<TVal> {
public function toNotContain<TVal>(
TVal $expected,
string $msg = '',
mixed ...$args
): void where T as Traversable<TVal> {
$msg = \vsprintf($msg, $args);
$this->assertNotContains($expected, not_hack_array($this->var), $msg);
}

/**
* Assert: $actual does not contain the substring $expected
*/
public function toNotContainSubstring(string $expected, string $msg = '', mixed ...$args): void where T = string {
public function toNotContainSubstring(
string $expected,
string $msg = '',
mixed ...$args
): void where T = string {
$msg = \vsprintf($msg, $args);
$this->assertNotContains($expected, not_hack_array($this->var), $msg);
}
Expand All @@ -471,7 +479,11 @@ class ExpectObj<T> extends Assert {
* Assert: That the KeyedTraversible $key has a key set.
* Note: If $key is a Set, use assertContains.
*/
public function toNotContainKey<TKey as arraykey, TVal>(TKey $key, string $msg = '', mixed ...$args): void where T as KeyedContainer<TKey, TVal> {
public function toNotContainKey<TKey as arraykey, TVal>(
TKey $key,
string $msg = '',
mixed ...$args
): void where T as KeyedContainer<TKey, TVal> {
$msg = \vsprintf($msg, $args);
$obj = $this->var;
$this->assertFalse(\array_key_exists($key, $obj), $msg);
Expand Down
3 changes: 2 additions & 1 deletion src/utils.hack
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ function print_type(mixed $value): string {
}

function is_iterable(mixed $value): bool {
return \is_array($value) || (\is_object($value) && ($value is Traversable<_>));
return \is_array($value) ||
(\is_object($value) && ($value is Traversable<_>));
}

function is_type(mixed $value): bool {
Expand Down
20 changes: 12 additions & 8 deletions tests/ExpectObjTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -216,21 +216,23 @@ final class ExpectObjTest extends HackTest {
expect(() ==> $rm->invokeArgs($obj, varray['custom msg']))
->toThrow(ExpectationFailedException::class, 'custom msg');
} else {
expect(() ==> $rm->invokeArgs($obj, varray[$expected, 'custom msg']))->toThrow(
ExpectationFailedException::class,
'custom msg',
);
expect(() ==> $rm->invokeArgs($obj, varray[$expected, 'custom msg']))
->toThrow(ExpectationFailedException::class, 'custom msg');
;
}

// And with funky sprintfification
if ($expected === self::EMPTY_VALUE) {
expect(() ==> $rm->invokeArgs($obj, varray['custom %s %d %f', 'msg', 1, 2.1]))
expect(
() ==> $rm->invokeArgs($obj, varray['custom %s %d %f', 'msg', 1, 2.1]),
)
->toThrow(ExpectationFailedException::class, 'custom msg 1 2.1');
} else {
expect(
() ==>
$rm->invokeArgs($obj, varray[$expected, 'custom %s %d %f', 'msg', 1, 2.1]),
() ==> $rm->invokeArgs(
$obj,
varray[$expected, 'custom %s %d %f', 'msg', 1, 2.1],
),
)->toThrow(ExpectationFailedException::class, 'custom msg 1 2.1');
}
}
Expand Down Expand Up @@ -449,7 +451,9 @@ final class ExpectObjTest extends HackTest {
}

public function testToThrowReturnsException(): void {
$e = expect(() ==> { throw new \Exception("Hello, world"); })->toThrow(\Exception::class);
$e = expect(() ==> {
throw new \Exception("Hello, world");
})->toThrow(\Exception::class);
expect($e->getMessage())->toContainSubstring('Hello, world');
}
}

0 comments on commit eb36e64

Please sign in to comment.