From a065e04d529da1d847b5062a12c46d916408bf32 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 21:46:22 -0500 Subject: update based on https://github.com/mdn/yari/issues/2028 --- .../sdk/low-level_apis/test_assert/index.html | 283 --------------------- 1 file changed, 283 deletions(-) delete mode 100644 files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html (limited to 'files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html') diff --git a/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html b/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html deleted file mode 100644 index 5f7537ec42..0000000000 --- a/files/zh-cn/mozilla/add-ons/sdk/low-level_apis/test_assert/index.html +++ /dev/null @@ -1,283 +0,0 @@ ---- -title: test/assert -slug: Mozilla/Add-ons/SDK/Low-Level_APIs/test_assert -translation_of: Archive/Add-ons/Add-on_SDK/Low-Level_APIs/test_assert ---- -

{{LegacyAddonsNotice}}{{AddonSidebar}}

- -
-

Unstable

-
- -

实现在 CommonJS Unit Testing specification version 1.1 其中定义的断言接口。

- -

Usage

- -

To use the assert module, write a set of unit tests following the instructions in the unit testing tutorial. Each test will be passed an Assert object when you run the tests using jpm test. You can use this object to make assertions about your program's state.

- -

For example:

- -
var a = 1;
-
-exports["test value of a"] = function(assert) {
-  assert.ok(a == 1, "test that a is 1");
-}
-
-require("sdk/test").run(exports);
- -

Globals

- -

Constructors

- -

Assert(logger)

- -

Create a new Assert object. This function is only called by the unit test framework, and not by unit tests themselves.

- -
Parameters
- -

logger : object
- Object used to log the results of assertions.

- -

Assert

- -

An object used to make assertions about a program's state in order to implement unit tests.

- -

The Assert object's interface is defined by the CommonJS Unit Testing specification, version 1.1.

- -

Methods

- -

ok(guard, message)

- -

Tests whether an expression evaluates to true.

- -
assert.ok(a == 1, "test that a is equal to one");
- -

This is equivalent to:

- -
assert.equal(a == 1, true, "test that a is equal to one");
- -
Parameters
- -

guard : expression
- The expression to evaluate.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

equal(actual, expected, message)

- -

Tests shallow, coercive equality with ==:

- -
assert.equal(1, 1, "test that one is one");
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

notEqual(actual, expected, message)

- -

Tests that two objects are not equal, using !=:

- -
assert.notEqual(1, 2, "test that one is not two");
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

deepEqual(actual, expected, message)

- -

Tests that two objects have a deep equality relation. Deep equality is defined in the CommonJS specification for Assert, item 7, which is quoted here:

- -
-
    -
  1. All identical values are equivalent, as determined by ===.
  2. -
  3. If the expected value is a Date object, the actual value is equivalent if it is also a Date object that refers to the same time.
  4. -
  5. Other pairs that do not both pass typeof value == "object", equivalence is determined by ==.
  6. -
  7. For all other Object pairs, including Array objects, equivalence is determined by having the same number of owned properties (as verified with Object.prototype.hasOwnProperty.call), the same set of keys (although not necessarily the same order), equivalent values for every corresponding key, and an identical "prototype" property. Note: this accounts for both named and indexed properties on Arrays.
  8. -
-
- -
assert.deepEqual({ a: "foo" }, { a: "foo" }, "equivalent objects");
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

notDeepEqual(actual, expected, message)

- -

Tests that two objects do not have a deep equality relation, using the negation of the test for deep equality:

- -
assert.notDeepEqual({ a: "foo" }, Object.create({ a: "foo" }),
-                    "object's inherit from different prototypes");
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

strictEqual(actual, expected, message)

- -

Tests that two objects are equal, using the strict equality operator ===:

- -
// This test will pass, because "==" will perform type conversion
-exports["test coercive equality"] = function(assert) {
-  assert.equal(1, "1", "test coercive equality between 1 and '1'");
-}
-
-// This test will fail, because the types are different
-exports["test strict equality"] = function(assert) {
-  assert.strictEqual(1, "1", "test strict equality between 1 and '1'");
-}
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

notStrictEqual(actual, expected, message)

- -

Tests that two objects are not equal, using the negation of the strict equality operator ===:

- -
// This test will fail, because "==" will perform type conversion
-exports["test coercive equality"] = function(assert) {
-  assert.notEqual(1, "1", "test coercive equality between 1 and '1'");
-}
-
-// This test will pass, because the types are different
-exports["test strict equality"] = function(assert) {
-  assert.notStrictEqual(1, "1", "test strict equality between 1 and '1'");
-}
- -
Parameters
- -

actual : object
- The actual result.

- -

expected : object
- The expected result.

- -

message : string
- Optional message to log, providing extra information about the test.

- -

throws(block, error, message)

- -

Assert that a block of code throws the expected exception.

- -

This method takes an optional Error argument:

- - - - -

For example, suppose we define two different custom exceptions:

- -
function MyError(message) {
-  this.name = "MyError";
-  this.message = message || "Default Message";
-}
-
-MyError.prototype = new Error();
-MyError.prototype.constructor = MyError;
-
-function AnotherError(message) {
-  this.name = "AnotherError";
-  this.message = message || "Default Message";
-    console.log(this.message);
-}
-
-AnotherError.prototype = new Error();
-AnotherError.prototype.constructor = AnotherError;
- -

We can check the type of exception by passing a function as the Error argument:

- -
exports["test exception type 1 expected to pass"] = function(assert) {
-  assert.throws(function() {
-    throw new MyError("custom message");
-  },
-  MyError,
-  "test throwing a specific exception");
-}
-
-exports["test exception type 2 expected to fail"] = function(assert) {
-  assert.throws(function() {
-    throw new MyError("custom message");
-  },
-  AnotherError,
-  "test throwing a specific exception");
-}
- -

We can check the message by passing a regular expression:

- -
exports["test exception message 1 expected to pass"] = function(assert) {
-  assert.throws(function() {
-    throw new MyError("custom message");
-  },
-  /custom message/,
-  "test throwing a specific message");
-}
-
-exports["test exception message 2 expected to pass"] = function(assert) {
-  assert.throws(function() {
-    throw new AnotherError("custom message");
-  },
-  /custom message/,
-  "test throwing a specific exception");
-}
-
-exports["test exception message 3 expected to fail"] = function(assert) {
-  assert.throws(function() {
-    throw new MyError("another message");
-  },
-  /custom message/,
-  "test throwing a specific message");
-}
- -
Parameters
- -

block : block
- The block of code to test.

- -

error : function|RegExp
- Either a constructor function returning the type of exception expected, or a regular expression expected to match the exception's message property.

- -

message : string
- Optional message to log, providing extra information about the test.

-- cgit v1.2.3-54-g00ecf