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

Commit

Permalink
Add toContainString and toContainSubstring
Browse files Browse the repository at this point in the history
This adds the new methods from 2.7.0 without the breaking changes.
  • Loading branch information
fredemmott committed Oct 24, 2019
1 parent 734f566 commit a8cceca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/ExpectObj.hack
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ class ExpectObj<T> extends Assert {
$this->assertContains($needle, not_hack_array($this->var), $msg);
}

/**
* Assert: $actual contains the substring $expected
*/
public function toContainSubstring(
string $expected,
string $msg = '',
mixed ...$args
): void where T = string {
$msg = \vsprintf($msg, $args);
$this->assertContains($expected, $this->var, $msg);
}

/**
* Assert: That the KeyedTraversible $key has a key set.
* Note: If $key is a Set, use assertContains.
Expand Down Expand Up @@ -453,6 +465,14 @@ class ExpectObj<T> extends Assert {
$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 {
$msg = \vsprintf($msg, $args);
$this->assertNotContains($expected, not_hack_array($this->var), $msg);
}

/**
* Assert: That the KeyedTraversible $key has a key set.
* Note: If $key is a Set, use assertContains.
Expand Down
9 changes: 9 additions & 0 deletions tests/ExpectObjTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ final class ExpectObjTest extends HackTest {
expect(dict[])->toBeType('KeyedContainer');
expect(array(1, 2, 3))->toContain(2);
expect(array(1, 2, 3))->toNotContain(7);
expect('foo')->toContain('foo');
expect('foo')->toContain('o');
expect('foo')->toNotContain('a');
expect('foo')->toContainSubstring('foo');
expect('foo')->toContainSubstring('o');
expect('foo')->toNotContainSubstring('a');
expect(1)->toAlmostEqual(1);
expect(null)->toAlmostEqual(null);

Expand Down Expand Up @@ -151,6 +157,9 @@ final class ExpectObjTest extends HackTest {
array('toNotContain', dict['x' => 1, 'y' => 2, 'z' => 3], 2),
array('toContain', dict[], 2),

array('toContainSubstring', 'foo', 'a'),
array('toNotContainSubstring', 'foo', 'o'),

array('toContainKey', dict['x' => 1, 'y' => 2, 'z' => 3], '1'),
array('toNotContainKey', dict['x' => 1, 'y' => 2, 'z' => 3], 'y'),
array('toContainKey', dict[], 'a'),
Expand Down

0 comments on commit a8cceca

Please sign in to comment.