The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / test / mjsunit.js @ 63a9cd38

History | View | Annotate | Download (4.49 KB)

1
// Copyright 2008 the V8 project authors. All rights reserved.
2
// Redistribution and use in source and binary forms, with or without
3
// modification, are permitted provided that the following conditions are
4
// met:
5
//
6
//     * Redistributions of source code must retain the above copyright
7
//       notice, this list of conditions and the following disclaimer.
8
//     * Redistributions in binary form must reproduce the above
9
//       copyright notice, this list of conditions and the following
10
//       disclaimer in the documentation and/or other materials provided
11
//       with the distribution.
12
//     * Neither the name of Google Inc. nor the names of its
13
//       contributors may be used to endorse or promote products derived
14
//       from this software without specific prior written permission.
15
//
16
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
//
28
//
29

    
30
/*jslint
31
    evil: true
32
*/
33

    
34
function MjsUnitAssertionError(message) {
35
  this.message = message;
36
}
37

    
38
MjsUnitAssertionError.prototype.toString = function () {
39
  return this.message;
40
};
41

    
42
/*
43
 * This file is included in all mini jsunit test cases.  The test
44
 * framework expects lines that signal failed tests to start with
45
 * the f-word and ignore all other lines.
46
 */
47

    
48
function fail (expected, found, name_opt) {
49
  var start;
50
  if (name_opt) {
51
    // Fix this when we ditch the old test runner.
52
    start = "Fail" + "ure (" + name_opt + "): ";
53
  } else {
54
    start = "Fail" + "ure:";
55
  }
56
  throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">");
57
};
58

    
59

    
60
function deepEquals (a, b) {
61
  if (a == b) {
62
      return true;
63
  }
64
  if ((typeof a) !== 'object' || (typeof b) !== 'object' ||
65
      (a === null) || (b === null)) {
66
    return false;
67
  }
68
  if (a.constructor === Array) {
69
    if (b.constructor !== Array) {
70
      return false;
71
    }
72
    if (a.length != b.length) {
73
      return false;
74
    }
75
    for (var i = 0; i < a.length; i++) {
76
      if (i in a) {
77
        if (!(i in b) || !(deepEquals(a[i], b[i]))) {
78
          return false;
79
        }
80
      } else if (i in b) {
81
        return false;
82
      }
83
    }
84
    return true;
85
  }
86
  return false;
87
};
88

    
89

    
90
exports.assertEquals = function (expected, found, name_opt) {
91
  if (!deepEquals(found, expected)) {
92
    fail(expected, found, name_opt);
93
  }
94
};
95

    
96

    
97
exports.assertArrayEquals = function (expected, found, name_opt) {
98
  var start = "";
99
  if (name_opt) {
100
    start = name_opt + " - ";
101
  }
102
  exports.assertEquals(expected.length, found.length, start + "array length");
103
  if (expected.length == found.length) {
104
    for (var i = 0; i < expected.length; ++i) {
105
      exports.assertEquals(expected[i], found[i], start + "array element at index " + i);
106
    }
107
  }
108
};
109

    
110

    
111
exports.assertTrue = function (value, name_opt) {
112
  exports.assertEquals(true, value, name_opt);
113
};
114

    
115

    
116
exports.assertFalse = function (value, name_opt) {
117
  exports.assertEquals(false, value, name_opt);
118
};
119

    
120

    
121
exports.assertNaN = function (value, name_opt) {
122
  if (!isNaN(value)) {
123
    fail("NaN", value, name_opt);
124
  }
125
};
126

    
127

    
128
exports.assertThrows = function (code) {
129
  var threwException = true;
130
  try {
131
    eval(code);
132
    threwException = false;
133
  } catch (e) {
134
    // Do nothing.
135
  }
136
  if (!threwException) { 
137
    exports.assertTrue(false, "did not throw exception");
138
  }
139
};
140

    
141

    
142
exports.assertInstanceof = function (obj, type) {
143
  if (!(obj instanceof type)) {
144
    exports.assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">");
145
  }
146
};
147

    
148

    
149
exports.assertDoesNotThrow = function (code) {
150
  try {
151
    eval(code);
152
  } catch (e) {
153
    exports.assertTrue(false, "threw an exception");
154
  }
155
};
156

    
157

    
158
exports.assertUnreachable = function (name_opt) {
159
  // Fix this when we ditch the old test runner.
160
  var message = "Fail" + "ure: unreachable";
161
  if (name_opt) {
162
    message += " - " + name_opt;
163
  }
164
  throw new MjsUnitAssertionError(message);
165
};