Skip to content

Commit

Permalink
feat: add Expectation::isTruthy()/isFalsy()
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Apr 12, 2023
1 parent d5f5ac1 commit 3aa3209
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ Assert::that(false)->isTrue(); // fail
Assert::that(true)->isFalse(); // fail
Assert::that(false)->isFalse(); // pass

// boolean (==)
Assert::that(1)->isTruthy(); // pass
Assert::that(new \stdClass())->isTruthy(); // pass
Assert::that('text')->isTruthy(); // pass
Assert::that(null)->isTruthy(); // fail
Assert::that(0)->isFalsy(); // pass
Assert::that(null)->isFalsy(); // pass
Assert::that('')->isFalsy(); // pass
Assert::that(1)->isFalsy(); // fail

// instanceof
Assert::that($object)->isInstanceOf(Some::class);

Expand Down
20 changes: 20 additions & 0 deletions src/Assert/Expectation.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ public function isFalse(string $message = 'Expected (true) to be (false).', arra
return $this;
}

/**
* Asserts the expectation value == true.
*
* @param string $message Available context: {expected}, {actual}
*/
public function isTruthy(string $message = 'Expected "{actual}" to be "truthy".', array $context = []): self
{
return $this->equals(true, $message, $context);
}

/**
* Asserts the expectation value == false.
*
* @param string $message Available context: {expected}, {actual}
*/
public function isFalsy(string $message = 'Expected "{actual}" to be "falsy".', array $context = []): self
{
return $this->equals(false, $message, $context);
}

/**
* Asserts the expectation value and $expected are "the same" using "===".
*
Expand Down
44 changes: 44 additions & 0 deletions tests/ExpectationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,50 @@ public function is_false(): void
;
}

/**
* @test
*/
public function is_truthy(): void
{
$this->assertSuccess(6, function() {
Assert::that(true)->isTruthy();
Assert::that(1)->isTruthy();
Assert::that(100)->isTruthy();
Assert::that(' ')->isTruthy();
Assert::that('foo')->isTruthy();
Assert::that(new \stdClass())->isTruthy();
});

$this
->assertFails('Expected "(false)" to be "truthy".', function() { Assert::that(false)->isTruthy(); })
->assertFails('Expected "(null)" to be "truthy".', function() { Assert::that(null)->isTruthy(); })
->assertFails('Expected "0" to be "truthy".', function() { Assert::that(0)->isTruthy(); })
->assertFails('Expected "" to be "truthy".', function() { Assert::that('')->isTruthy(); })
->assertFails('Expected "0" to be "truthy".', function() { Assert::that('0')->isTruthy(); })
;
}

/**
* @test
*/
public function is_falsy(): void
{
$this->assertSuccess(5, function() {
Assert::that(false)->isFalsy();
Assert::that(null)->isFalsy();
Assert::that(0)->isFalsy();
Assert::that('0')->isFalsy();
Assert::that('')->isFalsy();
});

$this
->assertFails('Expected "(true)" to be "falsy".', function() { Assert::that(true)->isFalsy(); })
->assertFails('Expected " " to be "falsy".', function() { Assert::that(' ')->isFalsy(); })
->assertFails('Expected "1" to be "falsy".', function() { Assert::that(1)->isFalsy(); })
->assertFails('Expected "stdClass" to be "falsy".', function() { Assert::that(new \stdClass())->isFalsy(); })
;
}

/**
* @test
*/
Expand Down

0 comments on commit 3aa3209

Please sign in to comment.