1/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 *
3 * The contents of this file are subject to the Netscape Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/NPL/
7 *
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
12 *
13 * The Original Code is Mozilla Communicator client code, released March
14 * 31, 1998.
15 *
16 * The Initial Developer of the Original Code is Netscape Communications
17 * Corporation. Portions created by Netscape are
18 * Copyright (C) 1998 Netscape Communications Corporation. All
19 * Rights Reserved.
20 *
21 * Contributor(s):
22 * Rob Ginda rginda@netscape.com
23 */
24
25var FAILED = "FAILED!: ";
26var STATUS = "STATUS: ";
27var BUGNUMBER = "BUGNUMBER: ";
28var SECT_PREFIX = 'Section ';
29var SECT_SUFFIX = ' of test -';
30var VERBOSE = false;
31var callStack = new Array();
32
33/*
34 * The test driver searches for such a phrase in the test output.
35 * If such phrase exists, it will set n as the expected exit code.
36 */
37function expectExitCode(n)
38{
39
40    print('--- NOTE: IN THIS TESTCASE, WE EXPECT EXIT CODE ' + n + ' ---');
41
42}
43
44/*
45 * Statuses current section of a test
46 */
47function inSection(x)
48{
49
50    return SECT_PREFIX + x + SECT_SUFFIX;
51
52}
53
54/*
55 * Some tests need to know if we are in Rhino as opposed to SpiderMonkey
56 */
57function inRhino()
58{
59  return (typeof defineClass == "function");
60}
61
62/*
63 * Report a failure in the 'accepted' manner
64 */
65function reportFailure (msg)
66{
67    var lines = msg.split ("\n");
68    var l;
69    var funcName = currentFunc();
70    var prefix = (funcName) ? "[reported from " + funcName + "] ": "";
71
72    for (var i=0; i<lines.length; i++)
73        print (FAILED + prefix + lines[i]);
74
75}
76
77/*
78 * Print a non-failure message.
79 */
80function printStatus (msg)
81{
82    var lines = msg.split ("\n");
83    var l;
84
85    for (var i=0; i<lines.length; i++)
86        print (STATUS + lines[i]);
87
88}
89
90/*
91 * Print a bugnumber message.
92 */
93function printBugNumber (num)
94{
95
96    print (BUGNUMBER + num);
97
98}
99
100/*
101 * Compare expected result to actual result, if they differ (in value and/or
102 * type) report a failure.  If description is provided, include it in the
103 * failure report.
104 */
105function reportCompare (expected, actual, description)
106{
107    var expected_t = typeof expected;
108    var actual_t = typeof actual;
109    var output = "";
110
111    if ((VERBOSE) && (typeof description != "undefined"))
112        printStatus ("Comparing '" + description + "'");
113
114    if (expected_t != actual_t)
115        output += "Type mismatch, expected type " + expected_t +
116            ", actual type " + actual_t + "\n";
117    else if (VERBOSE)
118        printStatus ("Expected type '" + actual_t + "' matched actual " +
119                     "type '" + expected_t + "'");
120
121    if (expected != actual)
122        output += "Expected value '" + expected + "', Actual value '" + actual +
123            "'\n";
124    else if (VERBOSE)
125        printStatus ("Expected value '" + actual + "' matched actual " +
126                     "value '" + expected + "'");
127
128    if (output != "")
129    {
130        if (typeof description != "undefined")
131            reportFailure (description);
132        reportFailure (output);
133    }
134
135}
136
137/*
138 * Puts funcName at the top of the call stack.  This stack is used to show
139 * a function-reported-from field when reporting failures.
140 */
141function enterFunc (funcName)
142{
143
144    if (!funcName.match(/\(\)$/))
145        funcName += "()";
146
147    callStack.push(funcName);
148
149}
150
151/*
152 * Pops the top funcName off the call stack.  funcName is optional, and can be
153 * used to check push-pop balance.
154 */
155function exitFunc (funcName)
156{
157    var lastFunc = callStack.pop();
158
159    if (funcName)
160    {
161        if (!funcName.match(/\(\)$/))
162            funcName += "()";
163
164        if (lastFunc != funcName)
165            reportFailure ("Test driver failure, expected to exit function '" +
166                           funcName + "' but '" + lastFunc + "' came off " +
167                           "the stack");
168    }
169
170}
171
172/*
173 * Peeks at the top of the call stack.
174 */
175function currentFunc()
176{
177
178    return callStack[callStack.length - 1];
179
180}
181