ec43ba9d80ca247bcfc92bed21bb9cbb29aa3f02
[platform/upstream/iotjs.git] / tools / src / js / assert.js
1 // Originally from narwhal.js (http://narwhaljs.org)
2 // Copyright (c) 2009 Thomas Robinson <280north.com>
3 // Copyright (c) 2015 Samsung Electronics Co., Ltd.
4 //
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:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
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.
21
22 /* Copyright 2015-present Samsung Electronics Co., Ltd. and other contributors
23  *
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
27  *
28  *     http://www.apache.org/licenses/LICENSE-2.0
29  *
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.
35  */
36
37 var util = require('util');
38
39
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;
47   } else {
48     this.message = getMessage(this);
49   }
50 }
51
52 util.inherits(AssertionError, Error);
53
54
55 function getMessage(assertion) {
56   return JSON.stringify(assertion, ['actual', 'expected', 'operator']);
57 }
58
59
60 function assert(value, message) {
61   if (!value) {
62     fail(value, true, message, '==');
63   }
64 }
65
66
67 function fail(actual, expected, message, operator) {
68   throw new AssertionError({
69     message: message,
70     actual: actual,
71     expected: expected,
72     operator: operator
73   });
74 }
75
76
77 function equal(actual, expected, message) {
78   if (actual != expected) {
79     fail(actual, expected, message, '==');
80   }
81 }
82
83
84 function notEqual(actual, expected, message) {
85   if (actual == expected) {
86     fail(actual, expected, message, '!=');
87   }
88 }
89
90
91 function strictEqual(actual, expected, message) {
92   if (actual !== expected) {
93     fail(actual, expected, message, '===');
94   }
95 }
96
97
98 function notStrictEqual(actual, expected, message) {
99   if (actual === expected) {
100     fail(actual, expected, message, '!==');
101   }
102 }
103
104
105 function throws(block, expected, message) {
106   var actual;
107
108   try {
109     block();
110   } catch (e) {
111     actual = e;
112   }
113
114   message = (expected && expected.name ? '(' + expected.name + ').' : '.') +
115             (message ? ' ' + message : '.');
116
117   if (!actual) {
118     fail(actual, expected, 'Missing expected exception' + message);
119   }
120   if (!(actual instanceof expected)) {
121     throw actual;
122   }
123 }
124
125
126 function doesNotThrow(block, message) {
127   var actual;
128
129   try {
130     block();
131   } catch (e) {
132     actual = e;
133   }
134
135   message = (message ? ' ' + message : '');
136
137   if (actual) {
138     fail(actual, null, 'Got unwanted exception.' + message);
139   }
140 }
141
142
143 assert.AssertionError = AssertionError;
144 assert.assert = assert;
145 assert.fail = fail;
146 assert.equal = equal;
147 assert.notEqual = notEqual;
148 assert.strictEqual = strictEqual;
149 assert.notStrictEqual = notStrictEqual;
150 assert.throws = throws;
151 assert.doesNotThrow = doesNotThrow;
152
153 module.exports = assert;