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 0a0b7b8
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( 0, "0 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( 1, "1 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

4 comments on commit 0a0b7b8

@raycohen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This noteworthy change didn't make it into History.md

@Krinkle
Copy link
Member

@Krinkle Krinkle commented on 0a0b7b8 Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@raycohen History is updated upon release. This has not yet been released.

https://github.com/qunitjs/qunit/commits/2.10.1

@raycohen
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I was confused as the commit to prep 2.10.1 came later than this.

@Krinkle
Copy link
Member

@Krinkle Krinkle commented on 0a0b7b8 Jul 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the release notes were backported from the 2.10.x-dev branch. I should have mentioned that in the commit message :)

Please sign in to comment.