Chromium Code Reviews| Index: chrome/test/data/webui/test_api.js |
| diff --git a/chrome/test/data/webui/test_api.js b/chrome/test/data/webui/test_api.js |
| index c9dddc252f351a37714c5fbc80980648d4680103..f5cc07e92253aa17fef52d6ef351901277ee860d 100644 |
| --- a/chrome/test/data/webui/test_api.js |
| +++ b/chrome/test/data/webui/test_api.js |
| @@ -5,20 +5,6 @@ |
| // Library providing basic test framework functionality. |
| (function() { |
| - // Asserts. |
| - // Use the following assertions to verify a condition within a test. |
| - // If assertion fails, the C++ backend will be immediately notified. |
| - // If assertion passes, no notification will be sent to the C++ backend. |
| - function assertBool(test, expected, message) { |
| - if (test !== expected) { |
| - if (message) |
| - message = test + '\n' + message; |
| - else |
| - message = test; |
| - throw new Error(message); |
| - } |
| - } |
| - |
| var old_chrome = chrome; |
| var send_callbacks = {}; |
| @@ -35,34 +21,144 @@ |
| old_chrome.send.apply(old_chrome, args); |
| } |
| + function CallHelper() { |
| + this.__proto__ = CallHelper.prototype; |
| + } |
| + |
| + CallHelper.prototype = { |
| + 'counts_': {}, |
| + 'registerCall': function() { |
| + var caller = arguments.callee.caller; |
| + var caller_name = caller.name; |
| + var caller_caller = caller.caller; |
| + if (caller_caller['isExpect']) { |
| + caller_name = caller_caller.expectName; |
| + caller_caller = caller_caller.caller; |
| + } |
| + var caller_caller_string = caller_caller.toString(); |
| + if (!(caller_caller_string in this.counts_)) |
| + this.counts_[caller_caller_string] = {}; |
| + if (!(caller_name in this.counts_[caller_caller_string])) |
| + this.counts_[caller_caller_string][caller_name] = 0; |
| + ++this.counts_[caller_caller_string][caller_name]; |
| + }, |
| + 'matchedParamsIndex': function(string, index) { |
| + var args = []; |
| + var parencount = 1; |
|
David Tseng
2011/07/08 23:45:11
why not 0?
Sheridan Rawlins
2011/07/14 23:32:31
Because you found the first paren, hence 1.
On 20
|
| + var arg_start = index + 1; |
| + for(index = arg_start; |
| + parencount && index < string.length; |
|
David Tseng
2011/07/08 23:45:11
parenCount
Sheridan Rawlins
2011/07/14 23:32:31
Done.
|
| + ++index) { |
| + if (string[index] == '(') { |
| + ++parencount; |
| + } else if (string[index] == ')') { |
| + --parencount; |
| + } else if (string[index] == ',' && parencount == 1) { |
| + args.push(string.substring(arg_start, index)); |
| + arg_start = index + 1; |
| + } |
| + } |
| + args.push(string.substring(arg_start, index - 1)); |
|
David Tseng
2011/07/08 23:45:11
confused...this is outside of the loop?
Sheridan Rawlins
2011/07/14 23:32:31
Moved inside the loop.
|
| + return [index, args]; |
| + }, |
| + 'getParams': function(caller_, count_) { |
| + var caller = (caller_ == undefined) ? |
| + arguments.callee.caller : caller_; |
| + var caller_name = caller.name; |
| + var caller_caller = caller.caller; |
| + if (caller_caller['isExpect']) { |
|
David Tseng
2011/07/08 23:45:11
Would prefer if we didn't special case this (see b
|
| + caller_name = caller_caller.expectName; |
| + caller_caller = caller_caller.caller; |
|
David Tseng
2011/07/08 23:45:11
Would prefer if we just printed the whole stack tr
Sheridan Rawlins
2011/07/14 23:32:31
We're trying to single out the text of the call. W
|
| + } |
| + var caller_caller_string = caller_caller.toString(); |
| + var count = (count_ == undefined) ? |
| + this.counts_[caller_caller_string][caller_name] : count_; |
| + var search_start = 0; |
| + var args; |
| + for(var i = 0; |
| + i < count && search_start < caller_caller_string.length; |
| + ++i) { |
| + search_start = caller_caller_string.indexOf( |
| + caller_name, search_start); |
| + if (search_start == -1) { |
| + if (i && count_ == undefined) { |
| + return this.getParams(caller, ((count - 1) % i) + 1); |
| + } else { |
| + console.error('bad count ' + count); |
| + return undefined; |
| + } |
| + } |
| + search_start += caller_name.length; |
|
David Tseng
2011/07/08 23:45:11
searchStart
Sheridan Rawlins
2011/07/14 23:32:31
Done.
|
| + var matched = this.matchedParamsIndex(caller_caller_string, |
| + search_start); |
| + args = matched[1]; |
| + search_start = matched[0]; |
| + } |
| + return args; |
| + }, |
| + 'getCall': function() { |
|
David Tseng
2011/07/08 23:45:11
Why don't we just generate the whole stack trace?
Sheridan Rawlins
2011/07/14 23:32:31
we do (it's in the Error and returned to C++)
On
|
| + var caller = arguments.callee.caller; |
| + var caller_name = caller.name; |
| + var caller_caller = caller.caller; |
| + if (caller_caller['isExpect']) { |
| + caller_name = caller_caller.expectName; |
| + caller_caller = caller_caller.caller; |
| + } |
| + return caller_name + '(' + this.getParams(caller) + ')'; |
| + }, |
| + }; |
| + |
| + var helper = new CallHelper(); |
| + |
| + // Asserts. |
| + // Use the following assertions to verify a condition within a test. |
| + // If assertion fails, the C++ backend will be immediately notified. |
| + // If assertion passes, no notification will be sent to the C++ backend. |
| + function assertBool(test, expected, message) { |
| + helper.registerCall(); |
| + if (test !== expected) { |
| + throw new Error('Test Error ' + helper.getCall() + ': ' + test); |
| + } |
| + } |
| + |
| function assertTrue(test, message) { |
| - assertBool(test, true, message); |
| + helper.registerCall(); |
| + if (test !== true) { |
| + throw new Error('Test Error ' + helper.getCall() + ': ' + test); |
| + } |
| } |
| function assertFalse(test, message) { |
| - assertBool(test, false, message); |
| + helper.registerCall(); |
| + if (test !== false) { |
| + throw new Error('Test Error ' + helper.getCall() + ': ' + test); |
| + } |
| } |
| function assertEquals(expected, actual, message) { |
| + helper.registerCall(); |
| if (expected != actual) { |
| - throw new Error('Test Error. Actual: ' + actual + '\nExpected: ' + |
| - expected + '\n' + message); |
| + throw new Error( |
| + 'Test Error ' + helper.getCall() + '\nActual: ' + actual + |
| + '; Expected: ' + expected); |
| } |
| if (typeof expected != typeof actual) { |
| - throw new Error('Test Error' + |
| - ' (type mismatch)\nActual Type: ' + typeof actual + |
| - '\nExpected Type:' + typeof expected + '\n' + message); |
| + throw new Error( |
| + 'Test Error (type mismatch) ' + helper.GetCall() + |
| + '\nActual Type: ' + typeof actual + |
| + '\nExpected Type:' + typeof expected); |
| } |
| } |
| function assertNotReached(message) { |
| + helper.registerCall(); |
| throw new Error(message); |
| } |
| var errors = []; |
| function createExpect(assertFunc) { |
| - return function() { |
| + var expect_func = function() { |
| try { |
| assertFunc.apply(null, arguments); |
| } catch (e) { |
| @@ -70,6 +166,10 @@ |
| errors.push(e); |
| } |
| }; |
| + expect_func.__proto__ = Function.prototype; |
|
David Tseng
2011/07/08 23:45:11
expectFunc (or expectHelper).
Sheridan Rawlins
2011/07/14 23:32:31
Done.
|
| + expect_func.expectName = assertFunc.name.replace(/^assert/, 'expect'); |
| + expect_func.isExpect = true; |
|
David Tseng
2011/07/08 23:45:11
Why don't we just subclass TOString to print out t
Sheridan Rawlins
2011/07/14 23:32:31
We would still need to do the replace somewhere -
|
| + return expect_func; |
| } |
| function runTest(testFunction, testArguments) { |