1 // Originally from narwhal.js (http://narwhaljs.org)
2 // Copyright (c) 2009 Thomas Robinson <280north.com>
3 // Copyright (c) 2015 Samsung Electronics Co., Ltd.
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the 'Software'), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
24 * Licensed under the Apache License, Version 2.0 (the "License");
25 * you may not use this file except in compliance with the License.
26 * You may obtain a copy of the License at
28 * http://www.apache.org/licenses/LICENSE-2.0
30 * Unless required by applicable law or agreed to in writing, software
31 * distributed under the License is distributed on an "AS IS" BASIS
32 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33 * See the License for the specific language governing permissions and
34 * limitations under the License.
37 var util = require('util');
40 function AssertionError(options) {
41 this.name = 'AssertionError';
42 this.actual = options.actual;
43 this.expected = options.expected;
44 this.operator = options.operator;
45 if (options.message) {
46 this.message = options.message;
48 this.message = getMessage(this);
52 util.inherits(AssertionError, Error);
55 function getMessage(assertion) {
56 return JSON.stringify(assertion, ['actual', 'expected', 'operator']);
60 function assert(value, message) {
62 fail(value, true, message, '==');
67 function fail(actual, expected, message, operator) {
68 throw new AssertionError({
77 function equal(actual, expected, message) {
78 if (actual != expected) {
79 fail(actual, expected, message, '==');
84 function notEqual(actual, expected, message) {
85 if (actual == expected) {
86 fail(actual, expected, message, '!=');
91 function strictEqual(actual, expected, message) {
92 if (actual !== expected) {
93 fail(actual, expected, message, '===');
98 function notStrictEqual(actual, expected, message) {
99 if (actual === expected) {
100 fail(actual, expected, message, '!==');
105 function throws(block, expected, message) {
114 message = (expected && expected.name ? '(' + expected.name + ').' : '.') +
115 (message ? ' ' + message : '.');
118 fail(actual, expected, 'Missing expected exception' + message);
120 if (!(actual instanceof expected)) {
126 function doesNotThrow(block, message) {
135 message = (message ? ' ' + message : '');
138 fail(actual, null, 'Got unwanted exception.' + message);
143 assert.AssertionError = AssertionError;
144 assert.assert = assert;
146 assert.equal = equal;
147 assert.notEqual = notEqual;
148 assert.strictEqual = strictEqual;
149 assert.notStrictEqual = notStrictEqual;
150 assert.throws = throws;
151 assert.doesNotThrow = doesNotThrow;
153 module.exports = assert;