Skip to content

Commit

Permalink
Assert: Introduce strict boolean true/false assertions
Browse files Browse the repository at this point in the history
Fixes #1444.
Closes #1445.
  • Loading branch information
ventuno authored and Krinkle committed Jun 25, 2020
1 parent 91b6995 commit 3e18e05
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/assert/false.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: default
title: false
description: A strict boolean false comparison.
categories:
- assert
---

## `false( actual [, message ] )`

A boolean check, equivalent to JUnit's `assertFalse()`. Passes if the first argument is false.

| name | description |
|--------------------|--------------------------------------|
| `actual` | Expression being tested |
| `message` (string) | A short description of the assertion |

### Description

`false()` requires just one argument. If the argument evaluates to false, the assertion passes; otherwise, it fails.

[`true()`](/assert/true) can be used to explicitly test for a true value.

### Examples

```js
QUnit.test( "false test", function( assert ) {
assert.false( false, "false succeeds" );

assert.false( "not-empty", "not-empty string fails" );
assert.false( "", "empty string fails" );
assert.false( 1, "1 fails" );
assert.false( true, "true fails" );
assert.false( NaN, "NaN fails" );
assert.false( null, "null fails" );
assert.false( undefined, "undefined fails" );
});
```
39 changes: 39 additions & 0 deletions docs/assert/true.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: default
title: true
description: A strict boolean true comparison.
categories:
- assert
---

## `true( actual [, message ] )`

A boolean check, equivalent to JUnit's `assertTrue()`. Passes if the first argument is `true`.

| name | description |
|--------------------|--------------------------------------|
| `actual` | Expression being tested |
| `message` (string) | A short description of the assertion |

### Description

`true()` requires just one argument. If the argument evaluates to true, the assertion passes; otherwise, it fails.

[`false()`](/assert/false) can be used to explicitly test for a false value.

### Examples

```js
QUnit.test( "true test", function( assert ) {
assert.true( true, "true succeeds" );

assert.true( "non-empty", "non-empty string fails" );
assert.true( "", "empty string fails" );
assert.true( 0, "0 fails" );
assert.true( false, "false fails" );
assert.true( NaN, "NaN fails" );
assert.true( null, "null fails" );
assert.true( undefined, "undefined fails" );
});
```

18 changes: 18 additions & 0 deletions src/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,24 @@ class Assert {
} );
}

true( result, message ) {
this.pushResult( {
result: result === true,
actual: result,
expected: true,
message
} );
}

false( result, message ) {
this.pushResult( {
result: result === false,
actual: result,
expected: false,
message
} );
}

equal( actual, expected, message ) {

// eslint-disable-next-line eqeqeq
Expand Down
16 changes: 16 additions & 0 deletions test/main/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ QUnit.test( "notOk", function( assert ) {
assert.notOk( NaN );
} );

QUnit.test( "true", function( assert ) {
function functionThatReturnsTrue() {
return true;
}
assert.true( true );
assert.true( functionThatReturnsTrue() );
} );

QUnit.test( "false", function( assert ) {
function functionThatReturnsFalse() {
return false;
}
assert.false( false );
assert.false( functionThatReturnsFalse() );
} );

QUnit.test( "equal", function( assert ) {
assert.equal( 1, 1 );
assert.equal( "foo", "foo" );
Expand Down

0 comments on commit 3e18e05

Please sign in to comment.