Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

Overhaul assert module #48

Merged
merged 1 commit into from
Sep 22, 2019
Merged
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The following core Node.js modules (v8.7.0+) have been implemented:

| Node Module | v10 & v12 support |
| ------------------------------------------------------------ | ------------------ |
| [assert](https://nodejs.org/api/assert.html) | |
| [assert](https://nodejs.org/api/assert.html) | :heavy_check_mark: |
| [buffer](https://nodejs.org/api/buffer.html) | :heavy_check_mark: |
| [child_process](https://nodejs.org/api/child_process.html) | :heavy_check_mark: |
| [cluster](https://nodejs.org/api/cluster.html) | |
Expand Down
69 changes: 31 additions & 38 deletions app/current/src/main/scala/io/scalajs/nodejs/Assert.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.scalajs.nodejs

import com.thoughtworks.enableIf
import io.scalajs.nodejs.events.IEventEmitter

import scala.scalajs.js
import scala.scalajs.js.annotation.JSImport
import scala.scalajs.js.|

/**
* The assert module provides a simple set of assertion tests that can be used to test invariants. The module is
Expand All @@ -23,13 +25,7 @@ trait Assert extends IEventEmitter {
*/
def apply(expression: js.Any, message: String = js.native): Unit = js.native

/**
* Tests for deep equality between the actual and expected parameters. Primitive values are compared with the equal
* comparison operator ( == ). Only enumerable "own" properties are considered. The deepEqual() implementation does
* not test object prototypes, attached symbols, or non-enumerable properties. This can lead to some potentially
* surprising results.
* @example assert.deepEqual(actual, expected[, message])
*/
@deprecated("Use assert.deepStrictEqual() instead.", "stability 0")
def deepEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native

/**
Expand All @@ -39,14 +35,10 @@ trait Assert extends IEventEmitter {
*/
def deepStrictEqual(actual: js.Any, expected: js.Any, message: String): Unit = js.native

/**
* Asserts that the function block does not throw an error. See assert.throws() for more details.
* When assert.doesNotThrow() is called, it will immediately call the block function. If an error is thrown
* and it is the same type as that specified by the error parameter, then an AssertionError is thrown. If the
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
* @example assert.doesNotThrow(block[, error][, message])
*/
def doesNotThrow(block: js.Function, error: js.Any, message: String): Unit = js.native
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def doesNotReject(asyncFn: js.Function | js.Promise[_],
error: js.RegExp | js.Function = js.native,
message: String = js.native): Unit = js.native

/**
* Asserts that the function block does not throw an error. See assert.throws() for more details.
Expand All @@ -55,28 +47,26 @@ trait Assert extends IEventEmitter {
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
* @example assert.doesNotThrow(block[, error][, message])
*/
def doesNotThrow(block: js.Function, message: String): Unit = js.native
def doesNotThrow(block: js.Function, error: js.RegExp | js.Function = js.native, message: String = js.native): Unit =
js.native

/**
* Asserts that the function block does not throw an error. See assert.throws() for more details.
* When assert.doesNotThrow() is called, it will immediately call the block function. If an error is thrown
* and it is the same type as that specified by the error parameter, then an AssertionError is thrown. If the
* error is of a different type, or if the error parameter is undefined, the error is propagated back to the caller.
* @example assert.doesNotThrow(block[, error][, message])
* @see https://nodejs.org/api/assert.html#assert_assert_equal_actual_expected_message
*/
def doesNotThrow(block: js.Function, error: js.Any = js.native): Unit = js.native
@deprecated("Use assert.strictEqual() instead.", "stability 0")
def equal(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native

/**
* Tests shallow, coercive equality between the actual and expected parameters using the equal comparison operator ( == ).
* @example assert.equal(actual, expected[, message])
* @see https://nodejs.org/api/assert.html#assert_assert_fail_message
*/
def equal(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native
def fail(message: String): Unit = js.native

/**
* Throws an AssertionError. If message is falsy, the error message is set as the values of actual and expected
* separated by the provided operator. Otherwise, the error message is the value of message.
* @example assert.fail(actual, expected, message, operator)
* @see https://nodejs.org/api/assert.html#assert_assert_fail_message
*/
def fail(message: js.Error): Unit = js.native

@deprecated("Use assert.fail([message]) or other assert functions instead.", "Node.js v10.0.0")
def fail(actual: js.Any, expected: js.Any, message: String, operator: String): Unit = js.native

/**
Expand All @@ -85,10 +75,7 @@ trait Assert extends IEventEmitter {
*/
def ifError(value: js.Any): Unit = js.native

/**
* Tests for any deep inequality. Opposite of assert.deepEqual().
* @example assert.notDeepEqual(actual, expected[, message])
*/
@deprecated("Use assert.notDeepStrictEqual() instead.", "stability 0")
def notDeepEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native

/**
Expand All @@ -97,10 +84,7 @@ trait Assert extends IEventEmitter {
*/
def notDeepStrictEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native

/**
* Tests shallow, coercive inequality with the not equal comparison operator ( != ).
* @example assert.notEqual(actual, expected[, message])
*/
@deprecated("Use assert.notStrictEqual() instead.", "stability 0")
def notEqual(actual: js.Any, expected: js.Any, message: String = js.native): Unit = js.native

/**
Expand All @@ -127,13 +111,22 @@ trait Assert extends IEventEmitter {
* of the message parameter. If the message parameter is undefined, a default error message is assigned.
* @example assert.throws(block[, error][, message])
*/
def throws(block: js.Function, error: js.Any, message: String = js.native): Unit = js.native
def throws(block: js.Function,
error: js.RegExp | js.Function | js.Object | Error,
message: String = js.native): Unit = js.native

@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
def rejects(asyncFn: js.Function | js.Promise[_],
error: js.RegExp | js.Function | js.Object | Error = js.native,
message: String = js.native): Unit = js.native
}

/**
* Assert Singleton
*/
@js.native
@JSImport("assert", JSImport.Namespace)
object Assert extends Assert
object Assert extends Assert {
@enableIf(io.scalajs.nodejs.CompilerSwitches.gteNodeJs10)
val strict: Assert = js.native
}
17 changes: 17 additions & 0 deletions app/nodejs-v10/src/test/scala/nodejs/assertion/AssertTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package nodejs.assertion

import org.scalatest.FunSpec
import io.scalajs.nodejs.{ Assert => NodeAssert }

import scala.scalajs.js

class AssertTest extends FunSpec {
it("have strict from v9.9.0") {
assert(NodeAssert.strict !== js.undefined)
}

it("have rejects/doesNotReject from v10.0.0") {
assert(NodeAssert.strict.rejects _ !== js.undefined)
assert(NodeAssert.strict.doesNotReject _ !== js.undefined)
}
}