Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Unified Diff: test/cctest/test-decls.cc

Issue 10872037: Test case for conflicting global declarations across multiple scripts. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments; reordered some tests. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/cctest/test-decls.cc
diff --git a/test/cctest/test-decls.cc b/test/cctest/test-decls.cc
index b55f417de867b2e760d294d2b517a1951f3fdf07..42e7e78d7f6c2015554f9c624621c2685312ac75 100644
--- a/test/cctest/test-decls.cc
+++ b/test/cctest/test-decls.cc
@@ -37,7 +37,8 @@ using namespace v8;
enum Expectations {
EXPECT_RESULT,
- EXPECT_EXCEPTION
+ EXPECT_EXCEPTION,
+ EXPECT_ERROR
};
@@ -95,7 +96,6 @@ class DeclarationContext {
private:
bool is_initialized_;
Persistent<Context> context_;
- Local<String> property_;
int get_count_;
int set_count_;
@@ -139,7 +139,13 @@ void DeclarationContext::Check(const char* source,
HandleScope scope;
TryCatch catcher;
catcher.SetVerbose(true);
- Local<Value> result = Script::Compile(String::New(source))->Run();
+ Local<Script> script = Script::Compile(String::New(source));
+ if (expectations == EXPECT_ERROR) {
+ CHECK(script.IsEmpty());
+ return;
+ }
+ CHECK(!script.IsEmpty());
+ Local<Value> result = script->Run();
CHECK_EQ(get, get_count());
CHECK_EQ(set, set_count());
CHECK_EQ(query, query_count());
@@ -681,3 +687,214 @@ TEST(ExistsInHiddenPrototype) {
EXPECT_RESULT, Number::New(0));
}
}
+
+
+
+class SimpleContext {
+ public:
+ SimpleContext() {
+ context_ = Context::New(0);
+ context_->Enter();
+ }
+
+ virtual ~SimpleContext() {
+ context_->Exit();
+ context_.Dispose();
+ }
+
+ void Check(const char* source,
+ Expectations expectations,
+ v8::Handle<Value> value = Local<Value>()) {
+ HandleScope scope;
+ TryCatch catcher;
+ catcher.SetVerbose(true);
+ Local<Script> script = Script::Compile(String::New(source));
+ if (expectations == EXPECT_ERROR) {
+ CHECK(script.IsEmpty());
+ return;
+ }
+ CHECK(!script.IsEmpty());
+ Local<Value> result = script->Run();
+ if (expectations == EXPECT_RESULT) {
+ CHECK(!catcher.HasCaught());
+ if (!value.IsEmpty()) {
+ CHECK_EQ(value, result);
+ }
+ } else {
+ CHECK(expectations == EXPECT_EXCEPTION);
+ CHECK(catcher.HasCaught());
+ if (!value.IsEmpty()) {
+ CHECK_EQ(value, catcher.Exception());
+ }
+ }
+ }
+
+ private:
+ Persistent<Context> context_;
+};
+
+
+TEST(MultiScriptConflicts) {
+ HandleScope scope;
+
+ { SimpleContext context;
+ context.Check("var x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("var x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ context.Check("const x = 3; x",
+ EXPECT_RESULT, Number::New(3));
+ context.Check("const x = 4; x",
+ EXPECT_RESULT, Number::New(4));
+ context.Check("x = 5; x",
+ EXPECT_RESULT, Number::New(5));
+ context.Check("var x = 6; x",
+ EXPECT_RESULT, Number::New(6));
+ context.Check("this.x",
+ EXPECT_RESULT, Number::New(6));
+ context.Check("function x() { return 7 }; x()",
+ EXPECT_RESULT, Number::New(7));
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("var x = 2; x", // assignment ignored
+ EXPECT_RESULT, Number::New(1));
+ context.Check("const x = 3; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("x = 4; x", // assignment ignored
+ EXPECT_RESULT, Number::New(1));
+ context.Check("var x = 5; x", // assignment ignored
+ EXPECT_RESULT, Number::New(1));
+ context.Check("this.x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("function x() { return 7 }; x",
+ EXPECT_EXCEPTION);
+ }
+
+ i::FLAG_use_strict = true;
+ i::FLAG_harmony_scoping = true;
+
+ { SimpleContext context;
+ context.Check("var x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("this.x",
+ EXPECT_RESULT, Number::New(1));
+ }
+
+ { SimpleContext context;
+ context.Check("let x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ context.Check("x",
+ EXPECT_RESULT, Number::New(2));
+ context.Check("this.x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 3; x",
+ EXPECT_RESULT, Number::New(3));
+ context.Check("x",
+ EXPECT_RESULT, Number::New(3));
+ context.Check("this.x",
+ EXPECT_RESULT, Number::New(3));
+ }
+
+ { SimpleContext context;
+ context.Check("function x() { return 4 }; x()",
+ EXPECT_RESULT, Number::New(4));
+ context.Check("x()",
+ EXPECT_RESULT, Number::New(4));
+ context.Check("this.x()",
+ EXPECT_RESULT, Number::New(4));
+ }
+
+ // TODO(rossberg): All of the below should actually be errors in Harmony.
+
+ { SimpleContext context;
+ context.Check("var x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("let x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("var x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("const x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("function x() { return 1 }; x()",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("let x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("function x() { return 1 }; x()",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("const x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("let x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("var x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("let x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("let x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("let x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("const x = 2; x",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("let x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("function x() { return 2 }; x()",
+ EXPECT_RESULT, Number::New(2));
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("var x = 2; x",
+ EXPECT_RESULT, Number::New(1));
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("let x = 2; x",
+ EXPECT_EXCEPTION);
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("const x = 2; x",
+ EXPECT_RESULT, Number::New(1));
+ }
+
+ { SimpleContext context;
+ context.Check("const x = 1; x",
+ EXPECT_RESULT, Number::New(1));
+ context.Check("function x() { return 2 }; x()",
+ EXPECT_EXCEPTION);
+ }
+}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698