Commit b4c9e2e7 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

[unittests] Add TestWithIsolate::RunJS helper method

- Update most callsites to use the new RunJS method
- Update tests to use TestWithNativeContext if possible
- Remove RunJS from test-helpers.cc
- Remove TestWithRandomNumberGenerator from test-utils.h

Change-Id: Ib2a6cc56334dc391ca6a2aeb7780fa324f44f109
Reviewed-on: https://chromium-review.googlesource.com/765373Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#49325}
parent 214f920a
......@@ -31,9 +31,28 @@ TEST(FunctionalTest, HashDoubleZero) {
EXPECT_EQ(h(0.0), h(-0.0));
}
namespace {
inline int64_t GetRandomSeedFromFlag(int random_seed) {
return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
}
} // namespace
template <typename T>
class FunctionalTest : public TestWithRandomNumberGenerator {};
class FunctionalTest : public ::testing::Test {
public:
FunctionalTest()
: rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
virtual ~FunctionalTest() {}
RandomNumberGenerator* rng() { return &rng_; }
private:
RandomNumberGenerator rng_;
DISALLOW_COPY_AND_ASSIGN(FunctionalTest);
};
typedef ::testing::Types<signed char, unsigned char,
short, // NOLINT(runtime/int)
......
......@@ -19,7 +19,7 @@
namespace v8 {
namespace internal {
typedef TestWithContext OptimizingCompileDispatcherTest;
typedef TestWithNativeContext OptimizingCompileDispatcherTest;
namespace {
......@@ -72,8 +72,8 @@ TEST_F(OptimizingCompileDispatcherTest, Construct) {
}
TEST_F(OptimizingCompileDispatcherTest, NonBlockingFlush) {
Handle<JSFunction> fun = Handle<JSFunction>::cast(test::RunJS(
isolate(), "function f() { function g() {}; return g;}; f();"));
Handle<JSFunction> fun =
RunJS<JSFunction>("function f() { function g() {}; return g;}; f();");
BlockingCompilationJob* job = new BlockingCompilationJob(i_isolate(), fun);
OptimizingCompileDispatcher dispatcher(i_isolate());
......
......@@ -11,7 +11,7 @@
namespace v8 {
namespace internal {
class PreParserTest : public TestWithContext {
class PreParserTest : public TestWithNativeContext {
public:
PreParserTest() {}
......@@ -22,16 +22,12 @@ class PreParserTest : public TestWithContext {
TEST_F(PreParserTest, LazyFunctionLength) {
const char* script_source = "function lazy(a, b, c) { } lazy";
Handle<Object> lazy_object = test::RunJS(isolate(), script_source);
Handle<JSFunction> lazy_function = RunJS<JSFunction>(script_source);
Handle<SharedFunctionInfo> shared(
Handle<JSFunction>::cast(lazy_object)->shared(), i_isolate());
Handle<SharedFunctionInfo> shared(lazy_function->shared());
CHECK_EQ(shared->length(), SharedFunctionInfo::kInvalidLength);
const char* get_length_source = "lazy.length";
Handle<Object> length = test::RunJS(isolate(), get_length_source);
CHECK(length->IsSmi());
Handle<Smi> length = RunJS<Smi>("lazy.length");
int32_t value;
CHECK(length->ToInt32(&value));
CHECK_EQ(3, value);
......
......@@ -15,17 +15,6 @@ namespace v8 {
namespace internal {
namespace test {
Handle<Object> RunJS(v8::Isolate* isolate, const char* script) {
return Utils::OpenHandle(
*v8::Script::Compile(
isolate->GetCurrentContext(),
v8::String::NewFromUtf8(isolate, script, v8::NewStringType::kNormal)
.ToLocalChecked())
.ToLocalChecked()
->Run(isolate->GetCurrentContext())
.ToLocalChecked());
}
Handle<String> CreateSource(Isolate* isolate,
ExternalOneByteString::Resource* maybe_resource) {
static const char test_script[] = "(x) { x*x; }";
......
......@@ -40,7 +40,6 @@ class ScriptResource : public v8::String::ExternalOneByteStringResource {
DISALLOW_COPY_AND_ASSIGN(ScriptResource);
};
Handle<Object> RunJS(v8::Isolate* isolate, const char* script);
Handle<String> CreateSource(
Isolate* isolate,
v8::String::ExternalOneByteStringResource* maybe_resource);
......
......@@ -5,6 +5,8 @@
#include "test/unittests/test-utils.h"
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include "src/api.h"
#include "src/base/platform/time.h"
#include "src/flags.h"
#include "src/isolate.h"
......@@ -50,6 +52,15 @@ void TestWithIsolate::TearDownTestCase() {
Test::TearDownTestCase();
}
Local<Value> TestWithIsolate::RunJS(const char* source) {
Local<Script> script =
v8::Script::Compile(
isolate()->GetCurrentContext(),
v8::String::NewFromUtf8(isolate(), source, v8::NewStringType::kNormal)
.ToLocalChecked())
.ToLocalChecked();
return script->Run(isolate()->GetCurrentContext()).ToLocalChecked();
}
TestWithContext::TestWithContext()
: context_(Context::New(isolate())), context_scope_(context_) {}
......@@ -57,25 +68,6 @@ TestWithContext::TestWithContext()
TestWithContext::~TestWithContext() {}
namespace base {
namespace {
inline int64_t GetRandomSeedFromFlag(int random_seed) {
return random_seed ? random_seed : TimeTicks::Now().ToInternalValue();
}
} // namespace
TestWithRandomNumberGenerator::TestWithRandomNumberGenerator()
: rng_(GetRandomSeedFromFlag(::v8::internal::FLAG_random_seed)) {}
TestWithRandomNumberGenerator::~TestWithRandomNumberGenerator() {}
} // namespace base
namespace internal {
TestWithIsolate::~TestWithIsolate() {}
......
......@@ -8,8 +8,12 @@
#include <vector>
#include "include/v8.h"
#include "src/api.h"
#include "src/base/macros.h"
#include "src/base/utils/random-number-generator.h"
#include "src/handles.h"
#include "src/objects-inl.h"
#include "src/objects.h"
#include "src/zone/accounting-allocator.h"
#include "src/zone/zone.h"
#include "testing/gtest-support.h"
......@@ -18,7 +22,8 @@ namespace v8 {
class ArrayBufferAllocator;
// Use v8::internal::TestWithIsolate if you are testing internals,
// aka. directly work with Handles.
class TestWithIsolate : public virtual ::testing::Test {
public:
TestWithIsolate();
......@@ -30,6 +35,8 @@ class TestWithIsolate : public virtual ::testing::Test {
return reinterpret_cast<v8::internal::Isolate*>(isolate());
}
Local<Value> RunJS(const char* source);
static void SetUpTestCase();
static void TearDownTestCase();
......@@ -42,7 +49,8 @@ class TestWithIsolate : public virtual ::testing::Test {
DISALLOW_COPY_AND_ASSIGN(TestWithIsolate);
};
// Use v8::internal::TestWithNativeContext if you are testing internals,
// aka. directly work with Handles.
class TestWithContext : public virtual TestWithIsolate {
public:
TestWithContext();
......@@ -57,25 +65,6 @@ class TestWithContext : public virtual TestWithIsolate {
DISALLOW_COPY_AND_ASSIGN(TestWithContext);
};
namespace base {
class TestWithRandomNumberGenerator : public ::testing::Test {
public:
TestWithRandomNumberGenerator();
virtual ~TestWithRandomNumberGenerator();
RandomNumberGenerator* rng() { return &rng_; }
private:
RandomNumberGenerator rng_;
DISALLOW_COPY_AND_ASSIGN(TestWithRandomNumberGenerator);
};
} // namespace base
namespace internal {
// Forward declarations.
......@@ -91,6 +80,12 @@ class TestWithIsolate : public virtual ::v8::TestWithIsolate {
Isolate* isolate() const {
return reinterpret_cast<Isolate*>(::v8::TestWithIsolate::isolate());
}
template <typename T = Object>
Handle<T> RunJS(const char* source) {
Handle<Object> result =
Utils::OpenHandle(*::v8::TestWithIsolate::RunJS(source));
return Handle<T>::cast(result);
}
base::RandomNumberGenerator* random_number_generator() const;
private:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment