Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assert: Introduce strict boolean assertions. #1445

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 boolean check. Passes if the first argument is false.
categories:
- assert
---

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

A boolean check, inverse of `true()` equivalent to Chai's `assert.isFalse()`, and JUnit's `assertFalse()`. Passes if the first argument is false.

| name | description |
|--------------------|--------------------------------------|
| state | Expression being tested |
| message (string) | A short description of the assertion |
Copy link
Member

Choose a reason for hiding this comment

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

missing backticks :)


### Description

`false()` requires just one argument. If the argument evaluates to false, the assertion passes; otherwise, it fails. If a second message argument is provided, it will be displayed in place of the result.
Krinkle marked this conversation as resolved.
Show resolved Hide resolved

[`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( "", "empty string fails" );
assert.false( NaN, "NaN fails" );
assert.false( null, "null fails" );
assert.false( undefined, "undefined fails" );
assert.false( true, "true fails" );
assert.false( 1, "1 fails" );
assert.false( "not-empty", "not-empty string fails" );
});
```
41 changes: 41 additions & 0 deletions docs/assert/true.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
layout: default
title: true
description: A boolean check, equivalent to Chai's assert.isTrue() and JUnit's assertTrue(). Passes if the first argument is true.
categories:
- assert
redirect_from:
- "/true/"
Copy link
Member

Choose a reason for hiding this comment

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

Many files contain this for compat with URLs from an older iteration of the API doc site (which was based on WordPress and jquer-wp-content). Not needed for new pages. I'll omit this while landing.

---

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

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

| name | description |
|--------------------|--------------------------------------|
| `state` | Expression being tested |
Copy link
Member

Choose a reason for hiding this comment

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

This is a left-over from ok(), for which in turn I actually don't know where it originates, but in general we call this the actual value. I'll use that here as well when landing the commit.

| `message` (string) | A short description of the assertion |

### Description

The most basic assertion in QUnit, `true()` requires just one argument. If the argument evaluates to true, the assertion passes; otherwise, it fails. If a second message argument is provided, it will be displayed in place of the result.

[`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( false, "false fails" );
assert.true( 0, "0 fails" );
assert.true( NaN, "NaN fails" );
assert.true( "", "empty string fails" );
assert.true( null, "null fails" );
assert.true( undefined, "undefined fails" );
});
```

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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