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 / deps / v8 / test / mjsunit / mjsunit.js @ 40c0f755

History | View | Annotate | Download (4.23 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
function MjsUnitAssertionError(message) {
29
  this.message = message;
30
}
31

    
32
MjsUnitAssertionError.prototype.toString = function () {
33
  return this.message;
34
}
35

    
36
/*
37
 * This file is included in all mini jsunit test cases.  The test
38
 * framework expects lines that signal failed tests to start with
39
 * the f-word and ignore all other lines.
40
 */
41

    
42
function fail(expected, found, name_opt) {
43
  var start;
44
  if (name_opt) {
45
    // Fix this when we ditch the old test runner.
46
    start = "Fail" + "ure (" + name_opt + "): ";
47
  } else {
48
    start = "Fail" + "ure:";
49
  }
50
  throw new MjsUnitAssertionError(start + " expected <" + expected + "> found <" + found + ">");
51
}
52

    
53

    
54
function deepEquals(a, b) {
55
  if (a == b) return true;
56
  if ((typeof a) !== 'object' || (typeof b) !== 'object' ||
57
      (a === null) || (b === null))
58
    return false;
59
  if (a.constructor === Array) {
60
    if (b.constructor !== Array)
61
      return false;
62
    if (a.length != b.length)
63
      return false;
64
    for (var i = 0; i < a.length; i++) {
65
      if (i in a) {
66
        if (!(i in b) || !(deepEquals(a[i], b[i])))
67
          return false;
68
      } else if (i in b) {
69
        return false;
70
      }
71
    }
72
    return true;
73
  }
74
  return false;
75
}
76

    
77

    
78
function assertEquals(expected, found, name_opt) {
79
  if (!deepEquals(found, expected)) {
80
    fail(expected, found, name_opt);
81
  }
82
}
83

    
84

    
85
function assertArrayEquals(expected, found, name_opt) {
86
  var start = "";
87
  if (name_opt) {
88
    start = name_opt + " - ";
89
  }
90
  assertEquals(expected.length, found.length, start + "array length");
91
  if (expected.length == found.length) {
92
    for (var i = 0; i < expected.length; ++i) {
93
      assertEquals(expected[i], found[i], start + "array element at index " + i);
94
    }
95
  }
96
}
97

    
98

    
99
function assertTrue(value, name_opt) {
100
  assertEquals(true, value, name_opt);
101
}
102

    
103

    
104
function assertFalse(value, name_opt) {
105
  assertEquals(false, value, name_opt);
106
}
107

    
108

    
109
function assertNaN(value, name_opt) {
110
  if (!isNaN(value)) {
111
    fail("NaN", value, name_opt);
112
  }
113
}
114

    
115

    
116
function assertThrows(code) {
117
  var threwException = true;
118
  try {
119
    eval(code);
120
    threwException = false;
121
  } catch (e) {
122
    // Do nothing.
123
  }
124
  if (!threwException) assertTrue(false, "did not throw exception");
125
}
126

    
127

    
128
function assertInstanceof(obj, type) {
129
  if (!(obj instanceof type)) {
130
    assertTrue(false, "Object <" + obj + "> is not an instance of <" + type + ">");
131
  }
132
}
133

    
134

    
135
function assertDoesNotThrow(code) {
136
  try {
137
    eval(code);
138
  } catch (e) {
139
    assertTrue(false, "threw an exception");
140
  }
141
}
142

    
143

    
144
function assertUnreachable(name_opt) {
145
  // Fix this when we ditch the old test runner.
146
  var message = "Fail" + "ure: unreachable"
147
  if (name_opt) {
148
    message += " - " + name_opt;
149
  }
150
  throw new MjsUnitAssertionError(message);
151
}