| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/stringprintf.h" | |
| 6 #include "base/string_piece.h" | |
| 7 #include "chrome/test/v8_unit_test.h" | |
| 8 | |
| 9 V8UnitTest::V8UnitTest() {} | |
| 10 | |
| 11 V8UnitTest::~V8UnitTest() {} | |
| 12 | |
| 13 void V8UnitTest::SetUp() { | |
| 14 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | |
| 15 global->Set(v8::String::New("log"), | |
| 16 v8::FunctionTemplate::New(&V8UnitTest::Log)); | |
| 17 context_ = v8::Context::New(NULL, global); | |
| 18 } | |
| 19 | |
| 20 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, | |
| 21 const std::string& value) { | |
| 22 v8::Context::Scope context_scope(context_); | |
| 23 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()), | |
| 24 v8::String::New(value.c_str(), value.length())); | |
| 25 } | |
| 26 | |
| 27 void V8UnitTest::ExecuteScriptInContext(const base::StringPiece& script_source, | |
| 28 const base::StringPiece& script_name) { | |
| 29 v8::Context::Scope context_scope(context_); | |
| 30 v8::HandleScope handle_scope; | |
| 31 v8::Handle<v8::String> source = v8::String::New(script_source.data(), | |
| 32 script_source.size()); | |
| 33 v8::Handle<v8::String> name = v8::String::New(script_name.data(), | |
| 34 script_source.size()); | |
| 35 | |
| 36 v8::TryCatch try_catch; | |
| 37 v8::Handle<v8::Script> script = v8::Script::Compile(source, name); | |
| 38 // Ensure the script compiled without errors. | |
| 39 if (script.IsEmpty()) { | |
| 40 FAIL() << ExceptionToString(&try_catch); | |
| 41 } | |
| 42 v8::Handle<v8::Value> result = script->Run(); | |
| 43 // Ensure the script ran without errors. | |
| 44 if (result.IsEmpty()) { | |
| 45 FAIL() << ExceptionToString(&try_catch); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 std::string V8UnitTest::ExceptionToString(v8::TryCatch* try_catch) { | |
| 50 std::string str; | |
| 51 v8::HandleScope handle_scope; | |
| 52 v8::String::Utf8Value exception(try_catch->Exception()); | |
| 53 v8::Handle<v8::Message> message = try_catch->Message(); | |
| 54 if (message.IsEmpty()) { | |
| 55 str.append(base::StringPrintf("%s\n", *exception)); | |
| 56 } else { | |
| 57 v8::String::Utf8Value filename(message->GetScriptResourceName()); | |
| 58 int linenum = message->GetLineNumber(); | |
| 59 int colnum = message->GetStartColumn(); | |
| 60 str.append(base::StringPrintf( | |
| 61 "%s:%i:%i %s\n", *filename, linenum, colnum, *exception)); | |
| 62 v8::String::Utf8Value sourceline(message->GetSourceLine()); | |
| 63 str.append(base::StringPrintf("%s\n", *sourceline)); | |
| 64 } | |
| 65 return str; | |
| 66 } | |
| 67 | |
| 68 void V8UnitTest::TestFunction(const std::string& function_name) { | |
| 69 v8::Context::Scope context_scope(context_); | |
| 70 v8::HandleScope handle_scope; | |
| 71 | |
| 72 v8::Handle<v8::Value> functionProperty = | |
| 73 context_->Global()->Get(v8::String::New(function_name.c_str())); | |
| 74 ASSERT_FALSE(functionProperty.IsEmpty()); | |
| 75 ASSERT_TRUE(functionProperty->IsFunction()); | |
| 76 v8::Handle<v8::Function> function = | |
| 77 v8::Handle<v8::Function>::Cast(functionProperty); | |
| 78 | |
| 79 v8::TryCatch try_catch; | |
| 80 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL); | |
| 81 // The test fails if an exception was thrown. | |
| 82 if (result.IsEmpty()) { | |
| 83 FAIL() << ExceptionToString(&try_catch); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 // static | |
| 88 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { | |
| 89 std::string message; | |
| 90 bool first = true; | |
| 91 for (int i = 0; i < args.Length(); i++) { | |
| 92 v8::HandleScope handle_scope; | |
| 93 if (first) { | |
| 94 first = false; | |
| 95 } else { | |
| 96 message += " "; | |
| 97 } | |
| 98 v8::String::Utf8Value str(args[i]); | |
| 99 message += *str; | |
| 100 } | |
| 101 std::cout << message << "\n"; | |
| 102 return v8::Undefined(); | |
| 103 } | |
| OLD | NEW |