📄 test-decls.cc.svn-base
字号:
1, // (re-)declaration EXPECT_EXCEPTION); // x has already been declared! }}class AbsentPropertyContext: public DeclarationContext { protected: virtual v8::Handle<Boolean> Has(Local<String> key) { return False(); }};TEST(Absent) { HandleScope scope; { AbsentPropertyContext context; context.Check("var x; x", 1, // access 2, // declaration + initialization 2, // declaration + initialization EXPECT_RESULT, Undefined()); } { AbsentPropertyContext context; context.Check("var x = 0; x", 1, // access 2, // declaration + initialization 2, // declaration + initialization EXPECT_RESULT, Number::New(0)); } { AbsentPropertyContext context; context.Check("function x() { }; x", 1, // access 1, // declaration 0, EXPECT_RESULT); } { AbsentPropertyContext context; context.Check("const x; x", 1, // access 2, // declaration + initialization 2, // declaration + initializetion EXPECT_RESULT, Undefined()); } { AbsentPropertyContext context; context.Check("const x = 0; x", 1, // access 2, // declaration + initialization 2, // declaration + initialization EXPECT_RESULT, Undefined()); // SB 0 - BUG 1213579 } { AbsentPropertyContext context; context.Check("if (false) { var x = 0 }; x", 1, // access 1, // declaration 1, // declaration + initialization EXPECT_RESULT, Undefined()); }}class AppearingPropertyContext: public DeclarationContext { public: enum State { DECLARE, INITIALIZE_IF_ASSIGN, UNKNOWN }; AppearingPropertyContext() : state_(DECLARE) { } protected: virtual v8::Handle<Boolean> Has(Local<String> key) { switch (state_) { case DECLARE: // Force declaration by returning that the // property is absent. state_ = INITIALIZE_IF_ASSIGN; return False(); case INITIALIZE_IF_ASSIGN: // Return that the property is present so we only get the // setter called when initializing with a value. state_ = UNKNOWN; return True(); default: ASSERT(state_ == UNKNOWN); break; } // Do the lookup in the object. return v8::Local<Boolean>(); } private: State state_;};TEST(Appearing) { HandleScope scope; { AppearingPropertyContext context; context.Check("var x; x", 1, // access 1, // declaration 2, // declaration + initialization EXPECT_RESULT, Undefined()); } { AppearingPropertyContext context; context.Check("var x = 0; x", 1, // access 2, // declaration + initialization 2, // declaration + initialization EXPECT_RESULT, Number::New(0)); } { AppearingPropertyContext context; context.Check("function x() { }; x", 1, // access 1, // declaration 0, EXPECT_RESULT); } { AppearingPropertyContext context; context.Check("const x; x", 0, 1, // declaration 2, // declaration + initialization EXPECT_EXCEPTION); // x has already been declared! } { AppearingPropertyContext context; context.Check("const x = 0; x", 0, 1, // declaration 2, // declaration + initialization EXPECT_EXCEPTION); // x has already been declared! }}class ReappearingPropertyContext: public DeclarationContext { public: enum State { DECLARE, DONT_DECLARE, INITIALIZE, UNKNOWN }; ReappearingPropertyContext() : state_(DECLARE) { } protected: virtual v8::Handle<Boolean> Has(Local<String> key) { switch (state_) { case DECLARE: // Force the first declaration by returning that // the property is absent. state_ = DONT_DECLARE; return False(); case DONT_DECLARE: // Ignore the second declaration by returning // that the property is already there. state_ = INITIALIZE; return True(); case INITIALIZE: // Force an initialization by returning that // the property is absent. This will make sure // that the setter is called and it will not // lead to redeclaration conflicts (yet). state_ = UNKNOWN; return False(); default: ASSERT(state_ == UNKNOWN); break; } // Do the lookup in the object. return v8::Local<Boolean>(); } private: State state_;};TEST(Reappearing) { HandleScope scope; { ReappearingPropertyContext context; context.Check("const x; var x = 0", 0, 2, // var declaration + const initialization 4, // 2 x declaration + 2 x initialization EXPECT_EXCEPTION); // x has already been declared! }}class ExistsInPrototypeContext: public DeclarationContext { protected: virtual v8::Handle<Boolean> Has(Local<String> key) { // Let it seem that the property exists in the prototype object. return True(); } // Use the prototype as the holder for the interceptors. virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) { return function->PrototypeTemplate(); }};TEST(ExistsInPrototype) { HandleScope scope; // Sanity check to make sure that the holder of the interceptor // really is the prototype object. { ExistsInPrototypeContext context; context.Check("this.x = 87; this.x", 0, 0, 0, EXPECT_RESULT, Number::New(87)); } { ExistsInPrototypeContext context; context.Check("var x; x", 0, 0, 1, // declaration EXPECT_RESULT, Undefined()); } { ExistsInPrototypeContext context; context.Check("var x = 0; x", 0, 0, 1, // declaration EXPECT_RESULT, Number::New(0)); } { ExistsInPrototypeContext context; context.Check("const x; x", 0, 0, 1, // declaration EXPECT_RESULT, Undefined()); } { ExistsInPrototypeContext context; context.Check("const x = 0; x", 0, 0, 1, // declaration EXPECT_RESULT, Number::New(0)); }}class AbsentInPrototypeContext: public DeclarationContext { protected: virtual v8::Handle<Boolean> Has(Local<String> key) { // Let it seem that the property is absent in the prototype object. return False(); } // Use the prototype as the holder for the interceptors. virtual Local<ObjectTemplate> GetHolder(Local<FunctionTemplate> function) { return function->PrototypeTemplate(); }};TEST(AbsentInPrototype) { HandleScope scope; { AbsentInPrototypeContext context; context.Check("if (false) { var x = 0; }; x", 0, 0, 1, // declaration EXPECT_RESULT, Undefined()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -