Index: vm/dart_api_impl_test.cc |
=================================================================== |
--- vm/dart_api_impl_test.cc (revision 5432) |
+++ vm/dart_api_impl_test.cc (working copy) |
@@ -2303,6 +2303,43 @@ |
} |
+TEST_CASE(SetField_FunnyValue) { |
+ const char* kScriptChars = |
+ "var top;\n"; |
+ |
+ Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
+ Dart_Handle name = Dart_NewString("top"); |
+ |
+ Dart_Handle args[1]; |
+ bool value; |
+ |
+ // Test that you can set the field to a good value. |
+ EXPECT_VALID(Dart_SetField(lib, name, Dart_True())); |
+ Dart_Handle result = Dart_GetField(lib, name); |
+ EXPECT_VALID(result); |
+ EXPECT(Dart_IsBoolean(result)); |
+ EXPECT_VALID(Dart_BooleanValue(result, &value)); |
+ EXPECT(value); |
+ |
+ // Test that you can set the field to null |
+ EXPECT_VALID(Dart_SetField(lib, name, Dart_Null())); |
+ result = Dart_GetField(lib, name); |
+ EXPECT_VALID(result); |
+ EXPECT(Dart_IsNull(result)); |
+ |
+ // Pass a non-instance handle. |
+ result = Dart_SetField(lib, name, lib); |
+ EXPECT(Dart_IsError(result)); |
+ EXPECT_STREQ("Dart_SetField expects argument 'value' to be of type Instance.", |
+ Dart_GetError(result)); |
+ |
+ // Pass an error handle. The error is contagious. |
+ result = Dart_SetField(lib, name, Api::NewError("myerror")); |
+ EXPECT(Dart_IsError(result)); |
+ EXPECT_STREQ("myerror", Dart_GetError(result)); |
+} |
+ |
+ |
TEST_CASE(FieldAccessOld) { |
const char* kScriptChars = |
"class Fields {\n" |
@@ -2958,6 +2995,149 @@ |
} |
+TEST_CASE(Invoke) { |
+ const char* kScriptChars = |
+ "class BaseMethods {\n" |
+ " inheritedMethod(arg) => 'inherited $arg';\n" |
+ " static nonInheritedMethod(arg) => 'noninherited $arg';\n" |
+ "}\n" |
+ "\n" |
+ "class Methods extends BaseMethods {\n" |
+ " instanceMethod(arg) => 'instance $arg';\n" |
+ " _instanceMethod(arg) => 'hidden instance $arg';\n" |
+ " static staticMethod(arg) => 'static $arg';\n" |
+ " static _staticMethod(arg) => 'hidden static $arg';\n" |
+ "}\n" |
+ "\n" |
+ "topMethod(arg) => 'top $arg';\n" |
+ "_topMethod(arg) => 'hidden top $arg';\n" |
+ "\n" |
+ "Methods test() {\n" |
+ " return new Methods();\n" |
+ "}\n"; |
+ |
+ // Shared setup. |
+ Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
+ Dart_Handle cls = Dart_GetClass(lib, Dart_NewString("Methods")); |
+ EXPECT_VALID(cls); |
+ Dart_Handle instance = Dart_Invoke(lib, Dart_NewString("test"), 0, NULL); |
+ EXPECT_VALID(instance); |
+ Dart_Handle args[1]; |
+ args[0] = Dart_NewString("!!!"); |
+ Dart_Handle result; |
+ Dart_Handle name; |
+ const char* str; |
+ |
+ // Instance method. |
+ name = Dart_NewString("instanceMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ result = Dart_Invoke(instance, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("instance !!!", str); |
+ |
+ // Hidden instance method. |
+ name = Dart_NewString("_instanceMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ result = Dart_Invoke(instance, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("hidden instance !!!", str); |
+ |
+ // Inherited method. |
+ name = Dart_NewString("inheritedMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ result = Dart_Invoke(instance, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("inherited !!!", str); |
+ |
+ // Static method. |
+ name = Dart_NewString("staticMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args))); |
+ result = Dart_Invoke(cls, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("static !!!", str); |
+ |
+ // Hidden static method. |
+ name = Dart_NewString("_staticMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args))); |
+ result = Dart_Invoke(cls, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("hidden static !!!", str); |
+ |
+ // Static non-inherited method. Not found at any level. |
+ name = Dart_NewString("non_inheritedMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(lib, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ |
+ // Top-Level method. |
+ name = Dart_NewString("topMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args))); |
+ result = Dart_Invoke(lib, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("top !!!", str); |
+ |
+ // Hidden top-level method. |
+ name = Dart_NewString("_topMethod"); |
+ EXPECT(Dart_IsError(Dart_Invoke(cls, name, 1, args))); |
+ EXPECT(Dart_IsError(Dart_Invoke(instance, name, 1, args))); |
+ result = Dart_Invoke(lib, name, 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("hidden top !!!", str); |
+} |
+ |
+ |
+TEST_CASE(Invoke_FunnyArgs) { |
+ const char* kScriptChars = |
+ "test(arg) => 'hello $arg';\n"; |
+ |
+ Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
+ Dart_Handle name = Dart_NewString("test"); |
+ |
+ Dart_Handle args[1]; |
+ const char* str; |
+ |
+ // Make sure that valid args yield valid results. |
+ args[0] = Dart_NewString("!!!"); |
+ Dart_Handle result = Dart_Invoke(lib, Dart_NewString("test"), 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("hello !!!", str); |
+ |
+ // Make sure that null is legal. |
+ args[0] = Dart_Null(); |
+ result = Dart_Invoke(lib, Dart_NewString("test"), 1, args); |
+ EXPECT_VALID(result); |
+ result = Dart_StringToCString(result, &str); |
+ EXPECT_STREQ("hello null", str); |
+ |
+ // Pass a non-instance handle. |
+ args[0] = lib; |
+ result = Dart_Invoke(lib, Dart_NewString("test"), 1, args); |
+ EXPECT(Dart_IsError(result)); |
+ EXPECT_STREQ("Dart_Invoke expects argument 0 to be an instance of Object.", |
+ Dart_GetError(result)); |
+ |
+ // Pass an error handle. The error is contagious. |
+ args[0] = Api::NewError("myerror"); |
+ result = Dart_Invoke(lib, Dart_NewString("test"), 1, args); |
+ EXPECT(Dart_IsError(result)); |
+ EXPECT_STREQ("myerror", Dart_GetError(result)); |
+} |
+ |
+ |
TEST_CASE(InvokeDynamic) { |
const char* kScriptChars = |
"class InvokeDynamic {\n" |